在方法内调用 Python Tkinter wait_window,然后恢复该方法
我有一个主窗口,它通过以下代码行创建一个设置窗口(定义为顶级窗口)作为 wait_window:
main.wait_window( Setup_Panel.setup_panel(main) )
该设置窗口有一种将其变量写入文本文件的方法,但是如果输入文件或输出目录变量为空 我将警告弹出窗口称为设置窗口的 wait_window,该窗口也定义为顶级窗口。方法如下:
def write_to_directory_file(main):
main.execute_command = True
if (main.input_directory_location.get().strip() == ""):
main.wait_window( Error_Box.WarningPopup(main, "Input File Missing") )
elif (main.output_directory_location.get().strip() == ""):
main.wait_window( Error_Box.WarningPopup(main, "Output Directory Missing") )
if execute_command:
directory_file = open("plink.dir", 'w')
directory_file.write("input_file: " + main.input_directory_location.get() + "\n")
directory_file.write("output_directory: " + main.output_directory_location.get() + "\n")
directory_file.write("output_file: " + main.output_file_name.get() + "\n")
directory_file.write("hom_name: " + main.HOM_name.get() + "\n")
enable_parent_window(main.parent_main)
main.destroy()
print "FLAG"
警告弹出窗口有给出的消息和两个按钮,一个是继续,另一个是取消。 如果您按“取消”,警告弹出窗口将被销毁,并将 main.execute_command 设置为 False,以便该方法不会继续。如果您按继续,它应该会销毁弹出窗口并将 main.execute_command 设置为 true,以便该方法无论如何都会恢复并写入目录。我的问题是,当警告弹出窗口被销毁时,它不会立即返回到该方法。相反,它不会返回到该点并打印“FLAG”,直到设置窗口也被销毁。
我将如何对其进行编码,以便在警告弹出窗口被销毁后而不是在销毁警告窗口和设置窗口后直接恢复该方法?唯一调用 .mainloop() 的窗口是主面板窗口。
主面板定义为: main = Tkinter.Tk() 并调用 main.mainloop()
设置面板定义为: setup_main = Tkinter.Toplevel()
警告弹出窗口定义为: warning_main = Tkinter.Toplevel()
任何帮助解决这一问题的帮助将不胜感激,谢谢!
I have a main window which creates a setup window (defined as a top level window) as a wait_window via this line of code:
main.wait_window( Setup_Panel.setup_panel(main) )
That setup window has a method to writes its variables to a text file, however if the input file or output directory variables are blank I call a warning popup window as a wait_window of the setup window which is also defined as a top level window. The method is as follows:
def write_to_directory_file(main):
main.execute_command = True
if (main.input_directory_location.get().strip() == ""):
main.wait_window( Error_Box.WarningPopup(main, "Input File Missing") )
elif (main.output_directory_location.get().strip() == ""):
main.wait_window( Error_Box.WarningPopup(main, "Output Directory Missing") )
if execute_command:
directory_file = open("plink.dir", 'w')
directory_file.write("input_file: " + main.input_directory_location.get() + "\n")
directory_file.write("output_directory: " + main.output_directory_location.get() + "\n")
directory_file.write("output_file: " + main.output_file_name.get() + "\n")
directory_file.write("hom_name: " + main.HOM_name.get() + "\n")
enable_parent_window(main.parent_main)
main.destroy()
print "FLAG"
The warning popup has the message given and two buttons one is Continue and the other is Cancel.
If you press cancel the warning popup will be destroyed and set main.execute_command to False so the method won't continue. If you press continue it should destroy the popup and set main.execute_command to true so that the method will resume and write to the directory anyway. My problem is that when the warning popup is destroyed it doesn't return to the method immediately. Rather it won't return to that point and print "FLAG" until the setup window has also been destroyed.
How would I code it so that it would resume the method directly after the warning popup has been destroyed rather than after I destroy the warning window and the setup window? The only window that is calling .mainloop() is the main panel window.
The main panel is defined as: main = Tkinter.Tk()
and calls main.mainloop()
The setup panel is defined as: setup_main = Tkinter.Toplevel()
The warning popup is defined as: warning_main = Tkinter.Toplevel()
Any help in figuring this out would be appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到了一个问题,就像你所描述的那样。子窗口出现并工作,但调用它的函数在子窗口关闭后尚未恢复。然而当父窗口也关闭后,没有及时恢复的功能终于恢复了。
我发现我不小心为
wait_window()
行设置了错误的参数。我编写了创建子窗口的函数名称,而不是子窗口自己的名称。示例:
出了什么问题:
我的案例的解决方案是什么:
我在寻找问题的解决方案时发现了您的问题。我希望我能帮忙。
I have just encountered a problem just like the one you described. The child window appeared and worked but the function that called it hasn't resumed after the child window was closed. However after the parent window was closed too, the function that hasn't resumed in time finally resumed.
I found out, that I have accidentally set wrong parameter to the
wait_window()
line. I wrote the function name that created the child window instead of the child windows' own name.Example:
What was wrong:
What was the solution in my case:
I found your question while I searched for solution to my problem. I hope I could help.
我发现这个老问题是因为我遇到了类似的问题。
根据我的发现,
wait_window(w)
等待w
被销毁,如w.destroy()
中那样,然后返回正常控制流程序。就我而言,我还实现了自己的对话框窗口,但我没有销毁该窗口。这可能是你的情况。TL/DR:必须销毁 wait_window() 的窗口参数才能继续正常控制流。
I found this old question because I had a similar problem.
According to my findings,
wait_window(w)
waits forw
to be destroyed as inw.destroy()
, then returns normal control flow to the program. In my case, I also implemented my own dialog window, but I didn't destroy the window. It may be your case.TL/DR: The window argument to wait_window() must be destroyed for normal control flow to continue.