我創造的Tkinter/Python的十個問題選擇題測驗。基本上在父窗口有13個按鈕 - 幫助按鈕,用戶詳細信息,提問1-10和「結束」按鈕。每一個按鈕打開一個新窗口的問題,選項checkbuttons和單選按鈕和「確認」按鈕,鏈接到的一段代碼,將計算出正確的答案,並添加1的比分,如果條件爲真。一旦用戶選擇了答案並按下「Enter」,該按鈕將被禁用。然而,一旦用戶退出這個窗口,他們能夠重新回答被加入到全局變量得分相同的問題這顯然會導致多個點。用戶回答問題後,如何禁用問題按鈕/窗口?我如何才能讓所有問題得到解答後才激活「顯示分數」按鈕?如何禁用窗口?
我用一個類來定義其後每個按鈕和個人類爲每個按鈕,所以我不知道這是否會導致問題(我是新來的面向對象)。由於
我知道壓痕是不正確的,IHAD進行格式化對本網站
from Tkinter import * #Copied from online examples
import tkMessageBox #Copied from online examples
import Tkinter, Tkconstants, tkFileDialog #Copied from online examples
import Tkinter as tk #Copied from online examples
class Example(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.question_1_window = None
self.question_1 = tk.Button(self, text="1", foreground="blue", command=self.show_question_1)
self.question_1.pack(side="left")
def show_question_1(self):
'''show the question window; create it if it doesn't exist'''
if self.question_1_window is None or not self.question_1_window.winfo_exists():
self.question_1_window = Question_1_Window(self)
else:
self.question_1_window.flash()
class Question_1_Window(tk.Toplevel):
'''A simple instruction window'''
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
self.text = tk.Label(self, width=75, height=4, text = "1) Do you have the time to do at least twenty minutes of prefect duty each week?")
self.text.pack(side="top", fill="both", expand=True)
question_1_Var = IntVar() #creating a variable to be assigned to the radiobutton
Yes_1 = Radiobutton(self, text = "Yes", variable = question_1_Var, value=1, height=5, width = 20)
Yes_1.pack() #creating 'yes' option
#Here we are assigning values to each option which will be used in the validation
No_1 = Radiobutton(self, text = "No", variable = question_1_Var, value=2, height=5, width = 20)
No_1.pack() #creating 'no' option
def calculate_score_1():
Enter_1.config(state="disabled")
if (question_1_Var.get() == 1) and not (question_1_Var.get() == 2):
print("calculate score has worked") #test lines
parent.score_per_question[1] = 1
else:
print("not worked") #testlines
Enter_1 = Button(self, text= "Enter", width=10, command = calculate_score_1)
Enter_1.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"))
if __name__ == "__main__":
root = tk.Tk()
root.geometry("800x500") #defining the size of the root
root.title("Prefect Quiz") #defining root title
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
請向我們展示相關代碼,以便我們可以更好地幫助您 – Gogo
如果您不關心格式化,爲什麼我們應該關心回答這個問題?瞭解如何在本網站上使用標記。這真的很簡單。首先,將您的代碼轉換爲空格和製表符。接下來,將您的代碼粘貼到您的問題中。第三,突出顯示代碼,然後點擊看起來像'[]'的按鈕將其標記爲代碼。 –