gpt4 book ai didi

python - 无法调用按钮命令 : application has been destroyed

转载 作者:行者123 更新时间:2023-11-28 21:56:19 25 4
gpt4 key购买 nike

下面给出了使用 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()

执行时显示:

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 的新手,因此不明白它的意思。我哪里错了?

最佳答案

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

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

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

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

关于python - 无法调用按钮命令 : application has been destroyed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21645716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com