Student did something like this in my Python class today. Can you spot the mistake? Took me a while.

Output when you run is "hello" then "NameError: name 'button' is not defined". No window appears.

import tkinter as tk
from tkinter import ttk
def do_when_clicked(): # When the button is clicked:
print("hello")
button.destroy()
gui = tk.Tk()
button = ttk.Button(gui, text="Click me", command=do_when_clicked())
button.pack()
gui.mainloop()

@peterrowlett An easy mistake for someone learning to make.

The value assigned to command should not have parentheses after it. It should pass the function itself, without parens. Instead, it calls the function while defining the Button.

Until ttk.Button returns, button doesn't exist, causing the error. If it didn't cause an error, it would assign the output of the function to command, leading to a different error which the button is clicked, about not being able to execute the value.