无法调用按钮命令:应用程序已被破坏 [英] Cannot invoke button command: application has been destroyed

查看:55
本文介绍了无法调用按钮命令:应用程序已被破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Given below is the code for creating independent windows using Tkinter and 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()

执行时显示:

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

我是 Python 新手,因此不明白它的含义.我哪里出错了?

I am new to Python and hence so not understand what it means. Where am I going wrong?

推荐答案

Remove () from win1.destroy() and win2.destroy().

Remove () from win1.destroy() and win2.destroy().

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

导致win1.destroy方法调用,并使用方法的返回值作为回调,而不是方法本身.导致主窗口在创建按钮之前销毁.

It cause win1.destroy method call, and use the return value of the method as callback, instead of the method itself.; cause the main window destroy before Button creation.

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

这篇关于无法调用按钮命令:应用程序已被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆