0

I'm attempting tkinter in python fo the first time but the Button command creates an error

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

It should create a window with a button but I just receive the error message

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

2 Answers 2

1

You already destroyed the window where you assign RandomWindow.destroy() to Close.

Here is what you probably meant:

def Close(): RandomWindow.destroy()

Use that instead of Close = RandomWindow.destroy()

Sign up to request clarification or add additional context in comments.

1 Comment

Python functions and methods are objects (just plain ordinary variables), so your close function is useless.
1

Here:

Close = RandomWindow.destroy() 

you are actually calling the window's destroy method, so when you hit the next line:

RandomButton = Button(RandomWindow, ...)

you are passing an already destroyed window to your button, hence the error.

You want:

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

or even more simply:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.