3

RE: verifying the existence of a Toplevel()

I'm setting a 15-second time limit for the user as they choose whether they want a random vowel or a random consonant, 9 times, to form a random list of letters. (I'm using a Toplevel containing a GUI timer which I got from someone else's answer).

If they manage it on time, a similar timer appears on the ORIGINAL window, replacing the vowel/consonant buttons (which I have destroyed at this point), and the old SEPARATE-window Toplevel timer is destroyed. However, I want to make the second countdown, since it HAS to start at 30 seconds, start at the right time, rather than ticking away in the background (behind the VOWEL/CONSONANT buttons) during which time the user is still selecting letters.

If none of that made any sense, then this is a basic outline of the code that doesn't work.

# I tried to test if the the Toplevel timer had been destroyed (which happens as soon as the 
# user has finished with the 9 letters). If so, I could then start the NEW 30-second timer.

import tkinter as tk

root = tk.Tk()

test = tk.Label() 


# above: later, will test if a Label() widget counts as a 'child'. If it was the case  
# that only Toplevels counted as 'children', then I could have used the 'root.winfo_children' 
# command to test if the Toplevel() timer had been destroyed, in which case I can start a new 
# 30-second timer on the original window.

test.pack()

extraWindow = tk.Toplevel(root)

extraWindow.destroy() # for below, to TRY and test whether the Toplevel object is destroyed

if not root.winfo_children():
    print("N0") # doesn't happen, because test label is also a 'child' 
    # IMAGINE that this is where I set off the NEW 30-second timer

root.mainloop()

Unfortunately, I have a label on the original window, displaying the list of letters as it develops in the fist 15 seconds, and while the user is coming up with a real word from as many of those letters as possible in the new, 30 seconds. I cannot use winfo_children. Is there something I can do to the effect of winfo_Toplevel?

(EDIT: yes there is; I finally did my homework and found something really obvious that I had missed, so, unfortunately, I answered my own question)

1 Answer 1

8

Whoops I just found an answer to my own question. You can check whether a top level exists using 'tkinter.Toplevel.winfo_exists(my_toplevel_name)'.

If you put this in a print statement, it returns 1 if it does exist, and 0 if it does not.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.