我正在尝试检查特定的Toplevel是否已被销毁,这是在按下某个按钮后发生的,这样我就可以在程序中执行其他操作(即创建一个新的Toplevel)。
假设在用户关闭初始顶层之后,shell上的输出应该是"N“。这将表明程序理解了最初的顶层不再存在,允许我继续下一阶段的特定if not 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() ),它都不起作用。
有什么建议吗?
发布于 2017-06-15 05:58:19
在检查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()https://stackoverflow.com/questions/44554944
复制相似问题