【发布时间】:2017-12-20 21:04:26
【问题描述】:
我从一个更大的脚本中获得了以下 python 2.7 sn-p 代码。
def RefreshAction():
execfile("SatTracker.py")
def CloseWindow():
window.destroy()
button = tk.Frame(window)
button = tk.Button(text='Refresh Data', width=25,command=lambda:[CloseWindow(),RefreshAction()])
button.pack()
window.mainloop()
所以我的整个代码生成了一个带有图像、文本和按钮的 tkinter 窗口。 现在该按钮应该触发关闭 Tkinter 窗口的命令,然后重新运行整个脚本,这很好。当我再次单击刷新按钮时,会发生一些奇怪的事情。窗口不再关闭,但脚本仍会启动,但出现此错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\python2\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "SatTracker.py", line 294, in <lambda>
button = tk.Button(text='Refresh Data', width=25,command=lambda:[CloseWindow(),RefreshAction()])
File "C:/Users/Peter Kongstad/Dropbox/Geoscience/2. Masters/7. Semester/Delphini-1 - Software Part/GUI/Programming/SatTracker.py", line 291, in CloseWindow
window.destroy()
File "C:\ProgramData\Anaconda3\envs\python2\lib\lib-tk\Tkinter.py", line 1864, in destroy
self.tk.call('destroy', self._w)
TclError: can't invoke "destroy" command: application has been destroyed
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\python2\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "C:/Users/Peter Kongstad/Dropbox/Geoscience/2. Masters/7. Semester/Delphini-1 - Software Part/GUI/Programming/SatTracker.py", line 294, in <lambda>
button = tk.Button(text='Refresh Data', width=25,command=lambda:[CloseWindow(),RefreshAction()])
File "C:/Users/Peter Kongstad/Dropbox/Geoscience/2. Masters/7. Semester/Delphini-1 - Software Part/GUI/Programming/SatTracker.py", line 288, in RefreshAction
execfile("SatTracker.py")
File "SatTracker.py", line 297, in <module>
window.mainloop()
File "C:\ProgramData\Anaconda3\envs\python2\lib\lib-tk\Tkinter.py", line 1129, in mainloop
self.tk.mainloop(n)
KeyboardInterrupt
有效地停止一切。 现在我不一定需要它来关闭旧的 tkinter 窗口,只要当我单击按钮时,脚本会再次运行并开始使用新的更新数据更新当前窗口。
所以无论哪种方式来解决这个问题对我来说都很好。有什么建议吗?
【问题讨论】:
-
你应该为按钮的
command选项定义一个方法,如果它需要调用多个方法,那么你正在做的事情是不受欢迎的。 -
我已经更新了。对于那个很抱歉。我是这个 python 编程的真正菜鸟。所以如果我的方法不好,那是因为我不知道更好。如果你能告诉我一个更好的方法。那么请务必这样做 =)
-
为什么要重启整个脚本?
-
首先要做的是停止使用
lambda。它对某种类型的问题有好处,但这不是那种类型的问题。创建一个适当的函数,以便您可以更轻松地对其进行调试。
标签: python python-2.7 tkinter