Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python function not being called

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading