我已經創建了的Tkinter/Python的十個問題選擇題測驗。我創建了一個類來存儲所有按鈕,然後創建了另外10個類來存儲出現在子窗口中的每個問題,並將問題作爲標籤和單選按鈕/校驗按鈕進行存儲。對於每個問題,當用戶按下「回車」時,程序會將他們的選擇與正確的回答者進行比較,並在必要時增加1分以得分。我如何讓變量'Score'可用於程序中的所有內容(即每個類)?我是否必須在課程之間傳遞分數值?如何在tkinter中的類之間傳遞值?
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
你有任何示例代碼? – Ffisegydd
是的,我已經編輯了問題 – user3056786
@ user3056786我認爲這是要採取一些討論/修改/編輯。誰想要幫忙,也許你可以使用[此聊天室](http://chat.stackoverflow.com/rooms/43338/how-do-i-pass-a-value-between-classes-in-tkinter) 。 – KobeJohn