pythontkinter:如何创建顶层窗口并销毁前一个窗口?

2024-10-01 19:31:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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()

Tags: textselfmasterconfigdefbuttonrootlabel
1条回答
网友
1楼 · 发布于 2024-10-01 19:31:36

在我看来,您遇到的程序没有正确检查命令行。它应该是arg[1]的“-name”,但其用法表明它实际上在arg[2]中。使用调试器自己检查这一点

这是一个简单的解决方案,我将留给你去解决

相关问题 更多 >

    热门问题