简体   繁体   中英

destroy method in tkinter

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)

This is part of the code for a game i am trying to write. I am wondering why is it that when i click on the qbutton it does not destroy the window. I need it so that when i push on the button i also change the value of dogame so i cannot simply set command=root.destroy

Command requires a function. You have provided the return value of a function.

You meant

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

By removing the parentheses from quitt , we are no longer evaluating it. Since functions are first-class objects in python, we can pass them around like anything else. Once you call the function, you're passing whatever it returns. In this case, the fact that it returns None, implicitely, masked the mistake

Note that you considered using root.destroy ; this is notable different from using root.destroy() with the call-syntax

When you assign command = quitt() you are calling that function at the time the button is being built, and then adding what that function returns ( None ) to the command call.

Instead, add the callable to the command:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM