Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在tkinter中销毁toplevel后检查它是否存在?_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x 如何在tkinter中销毁toplevel后检查它是否存在?

Python 3.x 如何在tkinter中销毁toplevel后检查它是否存在?,python-3.x,tkinter,Python 3.x,Tkinter,我试图检查某个特定的Toplevel是否已被破坏,这是在按下某个按钮后发生的,这样我就可以在程序中执行其他操作(即创建一个新的Toplevel) 据推测,在用户关闭初始顶级后,shell上的输出应该是“N”。这表明程序理解初始顶层已不存在,允许我在该特定的如果不是t1.winfo_exists():子句(见下文)中进行下一阶段 此输出不会发生。输出没有发生任何变化。我使用了“winfo_exists()”,我找不到我做得不对的地方 from tkinter import * root = Tk

我试图检查某个特定的Toplevel是否已被破坏,这是在按下某个按钮后发生的,这样我就可以在程序中执行其他操作(即创建一个新的Toplevel)

据推测,在用户关闭初始顶级后,shell上的输出应该是“N”。这表明程序理解初始顶层已不存在,允许我在该特定的
如果不是t1.winfo_exists():
子句(见下文)中进行下一阶段

此输出不会发生。输出没有发生任何变化。我使用了“winfo_exists()”,我找不到我做得不对的地方

from tkinter import *

root = Tk()

t1 = Toplevel(root)
t1.title('REG') 

def GetREG():
    global e, reg
    reg = e.get() # find out the user input
    # Destroy the toplevel:
    t1.destroy() # after the user presses the SubmitButton

label = Label(t1, text="Enter your REG:")
label.pack()

e = Entry(t1) # for the user to input their REG
e.pack()

SubmitButton = Button(t1,text='Submit',command=GetREG) # button to submit entry
SubmitButton.pack(side='bottom')

if not t1.winfo_exists(): # TRYING TO CHECK when the does not exist
    # supposedly, this should occur after the SubmitButton is pressed
    # which shold allow me to then carry out the next step in the program
    print("No")

root.mainloop()
当用户破坏一个窗口时,这是否会被认为是不存在的状态?当我用十字“删除”t1顶级时,或者当它通过SubmitButton(在
GetREG()
中使用
t1.destroy()
删除时,它都不起作用


有什么建议吗?

在您检查
t1.winfo_exists()
时,窗口仍然存在,因为您在创建该函数大约一毫秒后调用该函数。tkinter怎么知道您希望
if
语句等待窗口被销毁

如果您想在执行更多代码之前等待它被销毁,您可以使用方法
wait_window
,这类似于
mainloop
,它处理事件直到窗口被销毁

from tkinter import *

root = Tk()
t1 = Toplevel(root)    
...
root.wait_window(t1)

# there is now no need to check, since it's not possible to get
# here until the window has been destroyed.

root.mainloop()