所有检查按钮都设置为相同的tkinter

2024-10-03 11:20:07 发布

您现在位置:Python中文网/ 问答频道 /正文

Gif of problem

在我的代码中,我试图为一周中的每一天创建一个checkbutton,但当我单击一个时,它们都被选中。这是我的密码:

#reminderRepeat is a list of 7 integers
sun = tk.Checkbutton(frame, text = "Sun", bd = 0, variable = reminderRepeat[0])
sun.pack()

mon = tk.Checkbutton(frame, text = "Mon", bd = 0, variable = reminderRepeat[1])
mon.pack()

tue = tk.Checkbutton(frame, text = "Tues", bd = 0, variable = reminderRepeat[2])
tue.pack()

wed = tk.Checkbutton(frame, text = "Wed", bd = 0, variable = reminderRepeat[3])
wed.pack()
thu = tk.Checkbutton(frame, text = "Thurs", bd = 0, variable = reminderRepeat[4])
thu.pack()

fri = tk.Checkbutton(frame, text = "Fri", bd = 0, variable = reminderRepeat[5])
fri.pack()

sat = tk.Checkbutton(frame, text = "Sat", bd = 0, variable = reminderRepeat[6])
sat.pack()

它们在从按钮调用的方法中调用

解决方案:

问题是我已经将reminderRepeat设置为整数列表,但checkbuttons的变量应该是tkinter自己的数据类型IntVar

对于任何想知道它以前是这样的人: reminderRepeat = [0, 0, 0, 0, 0, 0, 0] 现在我把它改成:reminderRepeat = [tk.Intvar(), tk.Intvar(), tk.Intvar(), tk.Intvar(), tk.Intvar(), tk.Intvar(), tk.Intvar()]//call in tkinter loop


Tags: oftextvariableframebdpacktksun