检查 tkinter 中是否存在 Toplevel() 对象?

Par*_*gar 3 python tkinter

RE:验证 Toplevel() 的存在

我为用户设置了 15 秒的时间限制,因为他们选择要随机元音还是随机辅音 9 次,以形成随机字母列表。(我正在使用包含从其他人的答案中获得的 GUI 计时器的 Toplevel)。

如果他们按时管理,一个类似的计时器会出现在 ORIGINAL 窗口上,替换元音/辅音按钮(此时我已经销毁了),并且旧的 SEPARATE-window Toplevel 计时器被销毁。但是,我想进行第二次倒计时,因为它必须从 30 秒开始,在正确的时间开始,而不是在用户仍在选择字母期间在后台(在 VOWEL/CONSONANT 按钮后面)滴答作响。

如果这些都没有任何意义,那么这是不起作用的代码的基本轮廓。

# 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()
Run Code Online (Sandbox Code Playgroud)

不幸的是,我在原始窗口上有一个标签,显示了在前 15 秒内发展的字母列表,而用户在新的 30 秒内从尽可能多的这些字母中想出了一个真实的单词. 我无法使用winfo_children. 有什么我可以做的winfo_Toplevel吗?

(编辑:是的;我终于做了功课,发现我错过了一些非常明显的东西,所以,不幸的是,我回答了我自己的问题)

Par*_*gar 7

哎呀,我刚刚找到了我自己问题的答案。您可以使用“ tkinter.Toplevel.winfo_exists(my_toplevel_name)”检查是否存在顶级。

如果你把它放在一个打印语句中,如果它存在则返回 1,如果不存在则返回 0。