tkin中的销毁方法

2024-04-16 09:58:25 发布

您现在位置:Python中文网/ 问答频道 /正文

class Clicked(): 
    dogame=True

    def __init__(self):
        None


    def change(self): 
       self.dogame=False

currentgame=Clicked()
root = Tk()

def quitt(): 
    root.destroy()
    currentgame.change()

qbutton = Button(base, text = "Quit", command = quitt(), foreground = "Red", **kwargs)
qbutton.pack(side = BOTTOM)

这是我要写的游戏代码的一部分。我想知道为什么当我点击q按钮时,它不会破坏窗口。我需要它,这样当我按下按钮时,我也会改变dogame的值,这样我就不能简单地设置命令了=根目录。销毁在


Tags: selfnonefalsetrueinitdefrootchange
2条回答

当您分配command = quitt()时,您正在生成按钮时调用该函数,然后将该函数返回的内容(None)添加到命令调用中。在

相反,请将callable添加到命令:

qbutton = Button(base, text = "Quit", command = quitt, foreground = "Red", **kwargs)

命令需要函数。您已经提供了函数的返回值。在

你是说

qbutton = Button(base, text = "Quit", command = quitt, foreground = "Red", **kwargs)

通过删除quitt中的括号,我们不再计算它。因为函数是python中的一级对象,所以我们可以像传递其他对象一样传递它们。一旦你调用了这个函数,你就传递了它返回的任何内容。在本例中,它隐式地返回None掩盖了错误

注意,您考虑过使用root.destroy;这与在调用语法中使用root.destroy()有显著不同

相关问题 更多 >