I stumbled on a problem with functions:
from tkinter import *
count = 0
def create_window():
global count
new_window = Toplevel()
count += 1
if count > 2:
destroy_windowroot
print(count)
def destroy_windowroot():
window.destroy()
print("something is going on")
window = Tk()
b = Button(window, text="create new window dont go past 2", command=create_window)
b.pack()
window.mainloop()
this code should create a window every time the button is pressed and counts how many times its clicked so it can close the window when it reach’s a certain number
It does count correctly but for some reason the destroy_windowroot function is not being called
am I missing something?
>Solution :
To call a function, you need to use the function name followed by brackets (parenthesis). For example:
def my_function():
print("Hello from a function")
my_function()
In your case, you need to replace destroy_windowroot with destroy_windowroot().