Python Tkinter : How do you create a toplevel window and destroy the previous window?

标签 python python-3.x tkinter

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

最佳答案

更新版本

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]}的格式将测验问题放入字典中,并在调用问题类时将其用作参数

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

关于Python Tkinter : How do you create a toplevel window and destroy the previous window?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23440402/

相关文章:

python - TKinter 中的阿拉伯语文本

python - Matplotlib 条形图 - 一些条形不可见并且宽度似乎不同

python - 如何将单个 wheel 文件上传到 pypi 服务器?

python - 检索一个国家的城市列表

python - 如何使用 python HTMLParser 库从特定的 div 标签中提取数据?

python - 如何在 pyaudio 播放 .wav 文件时继续计算

python - 绘制包含许多组件的图形时节点大小不正确

python - 如何从字符串中删除所有 IRC 颜色代码

python-3.x - 使用 ruamel.yaml 进行数据转储期间,“CommentedMap”对象没有属性 '_context_manager'

python - 如何在 Tkinter 中实现 Canvas 项目的平滑移动?