Python 如何禁用窗口?

Python 如何禁用窗口?,python,events,window,tkinter,Python,Events,Window,Tkinter,我正在Tkinter/Python中创建一个十个问题的多项选择题测验。基本上在父窗口中有13个按钮-帮助按钮、用户详细信息、问题1-10和“结束”按钮。每个按钮都会打开一个包含问题的新窗口,选项包括复选按钮和单选按钮,以及一个“回车”按钮,该按钮链接到一段代码,该代码将计算正确答案,如果条件为真,则为分数加1。用户选择答案并按下“回车”后,按钮将被禁用。但是,一旦用户退出此窗口,他们就能够重新回答相同的问题,这显然会导致在全局变量分数中增加多个分数。用户回答问题后,如何禁用问题按钮/窗口?如何使

我正在Tkinter/Python中创建一个十个问题的多项选择题测验。基本上在父窗口中有13个按钮-帮助按钮、用户详细信息、问题1-10和“结束”按钮。每个按钮都会打开一个包含问题的新窗口,选项包括复选按钮和单选按钮,以及一个“回车”按钮,该按钮链接到一段代码,该代码将计算正确答案,如果条件为真,则为分数加1。用户选择答案并按下“回车”后,按钮将被禁用。但是,一旦用户退出此窗口,他们就能够重新回答相同的问题,这显然会导致在全局变量分数中增加多个分数。用户回答问题后,如何禁用问题按钮/窗口?如何使“显示分数”按钮仅在所有问题都已回答时激活

我使用了一个类来定义每个按钮,然后为每个按钮定义单独的类,所以我不确定这是否会导致问题(我是面向对象的新手)。谢谢

我知道缩进是不正确的,我必须为这个网站格式化它

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

如果它在之后创建了一个新窗口,那么您可以销毁它

window.destroy()
“窗口”的变化取决于您所称的tkinter。传统知识

window = tkinter. TK
=之前的内容就是您用其替换“窗口”的内容。
希望有帮助:)

请向我们展示相关代码,以便我们能更好地帮助您。如果您不关心格式,我们为什么要回答这个问题?了解如何在此网站上使用标记。这真的很简单。首先,将代码转换为使用空格和制表符。接下来,将代码粘贴到问题中。第三,突出显示代码,然后单击类似于
[]
的按钮将其标记为代码。销毁小部件与禁用它非常非常不同。我真的不认为这是问题所在。