【问题标题】:Python Tkinter: How do you create a toplevel window and destroy the previous window?Python Tkinter:如何创建顶层窗口并销毁前一个窗口?
【发布时间】:2014-05-03 04:50:59
【问题描述】:

我正在使用 tkinter 制作一个测验应用程序,我希望每个问题都在一个新窗口中,但是在我创建一个新窗口之后,我无法弄清楚如何销毁前一个窗口。这是我的代码的粗略简化摘录:

from tkinter import *

class q1:
    def __init__(self, master):
        self.master = master
        Label(self.master, text='What is 3 + 3?').grid()
        self.option_1 = Button(self.master, text='5', command = self.incorrect)
        self.option_1.grid()
        self.option_2 = Button(self.master, text='6', command = self.correct)
        self.option_2.grid()

    def correct(self):
        self.option_1.config(state=DISABLED)
        self.option_2.config(state=DISABLED)
        Label(self.master, text='Correct').grid()
        Button(self.master, text='Next Question', command = self.nextq).grid()

    def incorrect(self):
        self.option_1.config(state=DISABLED)
        self.option_2.config(state=DISABLED)
        Label(self.master, text='Incorrect').grid()
        Button(self.master, text='Next Question', command = self.nextq).grid()

    def nextq(self):
        q2(Toplevel(self.master))

class q2:
    def __init__(self, master):
        self.master = master
        Label(self.master, text='Question 2').grid()

def window():
    root = Tk()
    q1(root)
    root.mainloop()

if __name__ == '__main__':
    window()

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    更新版本

    from Tkinter import *
    import random
    
    class Ask:
        def __init__(self, parent,question,right,wrong):
            self.answer=right
            self.top=top=Toplevel(parent)
            Label(self.top, text=question,width=25).grid()
            a1=random.choice([right,wrong])
            self.option_1 = Button(self.top, text=a1, width=25,command = lambda: self.check(1))
            self.option_1.grid()
            a2=right
            if a1==right:
                a2=wrong
            self.option_2 = Button(self.top, text=a2, width=25, command = lambda: self.check(2))
            self.option_2.grid()
    
        def check(self,pressed):
            if pressed==1:
                ans=self.option_1['text']
            else:
                ans=self.option_2['text']
            if ans==self.answer:
                self.correct()
            else:
                self.incorrect()
    
        def correct(self):
            self.option_1.config(state=DISABLED)
            self.option_2.config(state=DISABLED)
            Label(self.top, text='Correct').grid()
            Button(self.top, text='Next Question', command = self.top.destroy).grid()
    
        def incorrect(self):
            self.option_1.config(state=DISABLED)
            self.option_2.config(state=DISABLED)
            Label(self.top, text='Incorrect').grid()
            Button(self.top, text='Next Question', command = self.top.destroy).grid()
    
    class QUIZ:
        def __init__(self, questionsdict):
            self.root=Tk()
    
            self.root.withdraw()
            self.questions(questionsdict)
            self.root.deiconify()
            c=Button(self.root, text='Click me to exit', command=self.root.destroy, width=30).grid(row=2, sticky='ew')
            Label(self.root, text='Quiz is Complete', width=30, background='red').grid(row=0, sticky='ew')
            mainloop()
    
        def questions(self,questionsdict):
            for k in questionsdict.keys():
                right=questionsdict[k][0]
                wrong=questionsdict[k][1]
                b=Ask(self.root, k, right,wrong )
                self.root.wait_window(b.top)
    

    按照以下格式将测验问题放入字典中:{question:[correctchoice,incorrectchoice]} 并在调用 Question Class 时将其用作参数

    questions={'What is 3+3':['6','8'], 'What is the capital of Alaska': ['Juno','Anchorage']}
    QUIZ(questions)
    

    【讨论】:

    • AttributeError: type object 'q1' has no attribute 'destroy'
    • 嗨@user3500082,如果这个答案已经解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
    • 虽然从技术上讲这可行,但它提供了一些有问题的建议。 Tkinter 并非旨在以这种方式工作。更好的答案是隐藏主窗口而不是销毁它,然后创建和销毁Toplevel 的实例。
    • @Bryan Oakley 进行了修复,因此 main 永远不会关闭/重新打开。由于没有保存或引用任何数据或窗口之间的任何交互或代码部分之间传输变量似乎不需要维护静态根,因为 Tk 确实允许您在没有的情况下销毁/生成主窗口拨打新电话。但你是对的,这不是一个好方法。添加了一些东西并重新编码了我的帖子...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多