因为我看到您已经在使用全局变量,
我将用一个
如何使应用程序成为Tkinter.Tk.塔卡(tkinter.Tk.塔卡在python 3中)。在import Tkinter as tk
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title('Hello world!')
self.data = None
self.helloButton = tk.Button(self, width=12, text='Hello',
command=lambda x='hi': self.say_hi(x))
self.helloButton.grid(row=0, column=1, padx=8, pady=8)
def say_hi(self, x):
self.data = x
self.withdraw()
self.secondWin = tk.Toplevel(self)
self.secondWin.grid()
self.output = tk.Entry(self.secondWin)
self.output.insert(0, x)
self.output.grid()
self.quitButton = tk.Button(self.secondWin, text='Quit', bg='tan',
command=self.close_app)
self.quitButton.grid()
def close_app(self):
self.destroy()
app = Application()
app.mainloop()
变量自我数据可以由您的类的任何方法使用;
因此,您不必使用global关键字。
mainloop使tkinter应用程序“运行”并处理事件。
请注意,我没有销毁第一个窗口,而是使用withdraw方法将其隐藏起来。
这只是你可能感兴趣的另一个选择。您可以使用deiconify使其可见(更多信息here)。第二个窗口是一个顶层小部件,您可以使用它来创建辅助窗口。在
我写了一个更有教育意义的例子here。在