Laura
Laura

Reputation: 53

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

Upvotes: 0

Views: 929

Answers (2)

MrAlexBailey
MrAlexBailey

Reputation: 5289

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)

Upvotes: 0

en_Knight
en_Knight

Reputation: 5381

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

Upvotes: 1

Related Questions