如何使用tkinter修复python中的“无法调用按钮命令”

时间:2019-05-21 13:57:41

标签: python button tkinter

我第一次尝试在python中进行tkinter,但是Button命令创建了一个错误

from tkinter import *
RandomWindow = Tk()
Close = RandomWindow.destroy()
RandomButton = Button(RandomWindow, text = "Click to shuffle and select cards", command = Close)
RandomButton.pack()

它应该创建一个带有按钮的窗口,但是我只是收到错误消息

_tkinter.TclError: can't invoke "button" command: application has been destroyed

2 个答案:

答案 0 :(得分:1)

您已经破坏了将RandomWindow.destroy()分配给Close的窗口。

这可能是您的意思:

def Close(): RandomWindow.destroy()

使用它代替Close = RandomWindow.destroy()

答案 1 :(得分:1)

这里:

'Our common stock is quoted on the OTCBB under the symbol UOIP. The reported high and low closing prices for the common stock as reported on the OTCBB are shown below for the periods indicated. The quotations reflect inter-dealer prices, without retail mark-up, markdown or commission, and may not represent actual transactions.'

您实际上是在调用窗口的Close = RandomWindow.destroy() 方法,因此,当您点击下一行时:

destroy

您正在将已经毁坏的窗口传递给按钮,因此出现错误。

您要

RandomButton = Button(RandomWindow, ...)

或更简单:

Close = RandomWindow.destroy # no parens, just reference the method
RandomButton = Button(
    RandomWindow, 
    text="Click to shuffle and select cards", 
    command=Close
)