无法调用按钮命令:应用程序已被破坏

Pra*_*ava 2 python user-interface tk-toolkit tkinter python-2.7

下面给出的是使用Tkinter和Python创建独立窗口的代码:

import Tkinter

Tkinter.NoDefaultRoot()

win1=Tkinter.Tk()
win2=Tkinter.Tk()

Tkinter.Button(win1, text='Woho!',command=win1.destroy()).pack()
Tkinter.Button(win2, text='Woho!',command=win2.destroy()).pack()

win1.mainloop()
Run Code Online (Sandbox Code Playgroud)

执行时显示:

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\eclipse\Python Projects\Project 1\Source1\mod.py", line 8, in <module>
    Tkinter.Button(win1, text='Woho!',command=win1.destroy()).pack()
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2106, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2036, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: can't invoke "button" command:  application has been destroyed
Run Code Online (Sandbox Code Playgroud)

我是Python的新手,因此无法理解它的含义。我要去哪里错了?

fal*_*tru 5

()win1.destroy()和中删除win2.destroy()

Tkinter.Button(win1, text='Woho!',command=win1.destroy()).pack()
Tkinter.Button(win2, text='Woho!',command=win2.destroy()).pack()
                                                      ^^
Run Code Online (Sandbox Code Playgroud)

它导致win1.destroy方法调用,并将方法的返回值用作回调,而不是方法本身。导致在创建Button之前破坏主窗口。

Tkinter.Button(win1, text='Woho!',command=win1.destroy).pack()
Tkinter.Button(win2, text='Woho!',command=win2.destroy).pack()
Run Code Online (Sandbox Code Playgroud)