我在Tkinter / Python中创建了一个十问题多项选择测验。基本上在父窗口中有13个按钮 - 帮助按钮,用户详细信息,问题1-10和“结束”按钮。每个按钮打开一个带有问题的新窗口,选项作为检查按钮和单选按钮,以及一个“输入”按钮,该按钮链接到一段代码,该代码将计算正确的答案,如果条件为真,则为分数加1。一旦用户选择了答案并按下“Enter”,该按钮将被禁用。然而,一旦用户退出该窗口,他们就能够回答相同的问题,这显然会导致多个点被添加到全局变量得分中。一旦用户回答了问题,如何禁用问题按钮/窗口?如果所有问题都得到解答,我如何才能激活“显示分数”按钮?
我已经使用了一个类为每个按钮定义了每个按钮和各个类,所以我不确定这是否会导致问题(我是面向对象的新手)。感谢
我知道压言不正确,我想为此网站格式化
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()
答案 0 :(得分:0)
如果它之后会创建一个新窗口,那么你就可以销毁它。
window.destroy()
“窗口”会根据您所说的tkinter而改变。 TK
window = tkinter. TK
无论在=之前是什么,取而代之的是“窗口”。 希望有所帮助:)