欢迎光临散文网 会员登陆 & 注册

tkinter.Checkbutton学习笔记

2021-09-13 21:28 作者:灰色de世界  | 我要投稿

import tkinter
from tkinter import IntVar, Checkbutton, Label, StringVar

# 内容:checkbutton多选框


class MainForm():
   def __init__(self):
       # 设置一个页面
       self.root = tkinter.Tk()
       # 设置页面总标题
       self.root.title('啦啦啦专属')
       # 设置logo图标,格式为 .ico
       self.root.iconbitmap(r'OIP-C.ico')
       # 获得屏幕宽度
       self.screen_width = self.root.winfo_screenwidth()
       self.screen_height = self.root.winfo_screenheight()
       x = (self.screen_width - 360) / 2
       y = (self.screen_height - 220) / 2
       # 设置居中窗口
       self.root.geometry("360x220+%d+%d" % (x, y))
       # self.root.geometry('360x220')
       # 禁止修改窗口(固定初始化窗口)
       self.root.resizable(width=0, height=0)
       # 设置背景颜色
       # self.root["background"] = "orange"
       self.data_list = [("魔道祖师", IntVar()), ("灵笼", IntVar()),
                         ("百妖谱", IntVar()), ("暗夜神使", IntVar()),
                         ("月光下的异世界之旅", IntVar()), ("万国志", IntVar())]
       self.label = Label(self.root, text="请选择你喜欢的动漫:",
                          font=("微软雅黑", 12))
       self.label.pack(anchor="w")
       for title, status in self.data_list:
           self.check = Checkbutton(self.root, text=title,
                                    onvalue=1, offvalue=0,
                                    variable=status,
                                    command=self.function)
           self.check.pack(anchor="w")
       # 设置默认选项
       self.data_list[5][1].set(1)
       # 禁止修改
       self.check["state"] = "disable"
       self.content = StringVar()
       self.label_fa = Label(self.root, textvariable=self.content,
                             font=('微软雅黑', 10))
       self.label_fa.pack(anchor="w")
       # 显示页面
       self.root.mainloop()

   def function(self):
       result = "你喜欢的动漫有:"
       for title, status in self.data_list:
           if status.get() == 1:
               result += title + "、"
       self.content.set(result)


def main():
   MainForm()


if __name__ == '__main__':
   main()



运行结果


checkbutton参数


tkinter.Checkbutton学习笔记的评论 (共 条)

分享到微博请遵守国家法律