gpt4 book ai didi

python - 如何为复选按钮赋值并使用它们来计算分数?

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:27 25 4
gpt4 key购买 nike

我正在 Tkinter 中编写多项选择测验,并为我的一些问题使用了复选按钮,用户可以在这些问题中选择“所有正确答案”。我已经创建了变量来存储复选按钮的值(开/关值 - 1/0)。但是,计算机似乎不理解变量的值,因此在“计算分数”if 语句中,计算机无法进行比较,因为它无法识别复选按钮的值。

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"))

最佳答案

您的question_5_VarX 变量属于IntVar 类型,因此它们在任何情况下都不会是True。您应该使用 get() 方法检查它们的 value。请注意,该值将是 int,但您可以像使用 bool 值一样使用它。

if question_5_Var2.get() and question_5_Var3.get() and not question_5_Var1.get():

(您可能还想检查第一个答案是否检查。)

关于python - 如何为复选按钮赋值并使用它们来计算分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20607361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com