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

更新版本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类时将其用作参数

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值