from openpyxl import *
from tkinter import *
def inputGetter(str):
print(str, end="")
return input()
class StartPage(Frame):
global app
def say_hi(self):
test = SecondPage(master=root)
app.destroy()
test.mainloop()
def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.QUIT.pack({"side": "left"})
self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi
self.hi_there.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
class SecondPage(Frame):
global app
def say_hi(self):
print('test')
def createWidgets(self):
self.QUIT2 = Button(self)
self.QUIT2["text"] = "QUIT2"
self.QUIT2["fg"] = "red"
self.QUIT2["command"] = self.quit
self.QUIT2.pack({"side": "left"})
self.hi_there = Button(self)
self.hi_there["text"] = "Second",
self.hi_there["command"] = self.say_hi
self.hi_there.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = StartPage(master=root)
app.mainloop()
root.destroy()
出于某种原因,当我按下QUIT小部件时,它可以工作,但是位于SecondPage类中的QUIT2小部件需要我点击它两次。这样做的代码出了什么问题呢?
欢呼,马克答案 0 :(得分:3)
你启动两个主循环,因此需要两次调用'退出'才能离开你的程序。
root
或app
mainloop(在您的示例中不可见,但需要以某种方式查看第一个窗口) StartPage.say_hi
我不确定预期的行为,但它确实导致两个mailoops,并且需要两次调用quit
才能离开这两个。如果您从test.mainloop()
删除StartPage.say_hi
,则问题就会消失。