我正在Tkinter中写一个多项选择quiuz,并在我的一些问题中使用了检查按钮,用户将选择所有正确的答案'。我创建了将存储检查按钮值的变量(开/关值 - 1/0)。但是,计算机似乎并不了解变量的价值,因此计算得分'如果声明计算机无法进行比较,因为它不能识别检查按钮的值。
class Question_5_Window(tk.Toplevel):
'''A simple instruction window'''
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
self.text = tk.Label(self, width=100, height=4, text = "5) What would you do if you were walking to class and you saw a first year crying? Tick all correct answers.")
self.text.pack(side="top", fill="both", expand=True)
question_5_Var1 = IntVar()
question_5_Var2 = IntVar()
question_5_Var3 = IntVar()
A_5 = Checkbutton(self, text = "Keep walking", variable = question_5_Var1, onvalue = 1, offvalue = 0, height=5, width = 20)
A_5.pack()
B_5 = Checkbutton(self, text = "Take them to guidance", variable = question_5_Var2, onvalue = 1, offvalue = 0, height=5, width = 20)
B_5.pack()
C_5 = Checkbutton(self, text = "Talk to them to resolve issue", variable = question_5_Var3, onvalue = 1, offvalue = 0, height=5, width = 20)
C_5.pack()
def calculate_score():
if (question_5_Var2 == True) and (question_5_Var3 == True):
print("calculate score has worked")
else:
print("not worked")
Enter_5 = Button(self, text= "Enter", width=10, command = calculate_score)
Enter_5.pack()
def flash(self):
'''make the window visible, and make it flash temporarily'''
# make sure the window is visible, in case it got hidden
self.lift()
self.deiconify()
# blink the colors
self.after(100, lambda: self.text.configure(bg="black", fg="white"))
self.after(500, lambda: self.text.configure(bg="white", fg="black"))
答案 0 :(得分:2)
您的question_5_VarX
个变量的类型为IntVar
,因此在任何情况下它们都不会是True
。您应该使用value
方法检查他们的get()
。请注意,该值将为int
,但您可以像布尔值一样使用它。
if question_5_Var2.get() and question_5_Var3.get() and not question_5_Var1.get():
(您可能还想检查第一个答案是否未已选中。)