python中如何向类中传值_如何在tkinter中的类之间传递值?

我用Tkinter/Python创建了一个10个问题的选择题测验。我已经创建了一个类来存储所有的按钮,然后创建了另外十个类来存储每个出现在子窗口中的问题,并将问题作为标签和单选按钮/复选按钮。对于每个问题,当用户按“回车”键时,程序会将他们的选择与正确答案进行比较,并在必要时加1分得分。如何使变量“Score”可用于程序中的所有内容(即每一节课)?我必须在课间传递分数吗?在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.get() == 1) and (question_5_Var3.get() == 1) and not question_5_Var1.get():

print("calculate score has worked")

score = score + 1

else:

print("not worked")

return score

Enter_5 = Button(self, text= "Enter", width=10, command = calculate_score)

Enter_5.pack()

return score

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值