我试图关闭一个窗口,然后在一个类中打开一个新窗口,但是第二个窗口没有打开。
谁能告诉我什么不起作用。
(代码制作的游戏会随机获得2个数字,并要求您将它们加在一起,然后显示一条消息,说明您是对还是错)
代码在其自己的窗口中,而不在类中起作用。
Self.G是主菜单屏幕。
此处的代码:
def start(self):
self.G.destroy()
self.game()
## The game playing ##
def game(self):
def Questions(self):
number1 = random.randrange(1,25)
number2 = random.randrange(1,50)
answer = number1 + number2
prompt = ("Add " + str(number1) + " and " + str(number2))
label1 = Label(self.root, text=prompt, width=len(prompt), bg='yellow')
label1.pack()
return answer
def start(self):
global answer
answer = Questions()
def Submit(answer, entryWidget):
""" Display the Entry text value. """
print(answer)
if entryWidget.get().strip() == "":
messagebox.showerror("Tkinter Entry Widget", "Please enter a number.")
if answer != int(entryWidget.get().strip()):
incorrect = ('Thats incorrect!')
boxInco = Label(root, text=incorrect, width=len(incorrect), bg='orange')
boxInco.pack()
else:
Textcorrect = ('Correct!')
CorrectMessage = Label(root, text=Textcorrect, width=len(Textcorrect), bg='orange')
CorrectMessage.pack()
# creates a Tkinter window
self.root = Tk()
self.root.title("Math Quiz")
self.root["padx"] = 40
self.root["pady"] = 20
# Creates a text frame to hold the text Label and the Entry widget
textFrame = Frame(self.root)
#Creates a Label in textFrame
entryLabel = Label(textFrame)
entryLabel["text"] = "Answer:"
entryLabel.pack(side=LEFT)
# Creates an Entry Widget in textFrame
entryWidget = Entry(textFrame)
entryWidget["width"] = 50
entryWidget.pack(side=LEFT)
textFrame.pack()
#instructions
directions = ('Click start to begin. You will be asked a series of questions.')
instructions = Label(root, text=directions, width=len(directions), bg='orange')
instructions.pack()
Sub = lambda: Submit(answer, entryWidget)
#stopwatch = lambda: start(answer)
# create needed widgets
label = Label(self.root, text='0.0')
btn_submit = Button(self.root, text="Submit", command = Sub)
btn_start = Button(self.root, text="Start", command = start)
btn_submit.pack()
btn_start.pack()
label.pack()
# start the event loop
self.root.mainloop()
目前,它删除了第一个窗口,但第二个窗口什么也不做,没有错误消息,没事。