什么是“wait_window”方法做? [英] What does the"wait_window" method do?

查看:858
本文介绍了什么是“wait_window”方法做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,调用此方法的对象等待在继续自己的循环之前传递为参数的窗口 ...



Misc 类的文档字符串,我们可以观察到:


  def wait_window(self,window = None):
等待直到WIDGET被销毁
如果没有给定参数self被使用。


乍一看,似乎这种方法可以使一个 Toplevel modal,但这不是真的。要使 Toplevel modal,我们必须使用 grab_set()方法。



我已经看到其他解释:


wait_window 似乎不返回,直到给定的小部件通过参数
不被销毁。


从另一个地方:


wait_window(widget) - 创建等待给定的
小部件的本地事件被毁坏这个循环不会影响应用程序的
mainloop。


effbot 文档,我们有:


wait_window 输入一个本地事件循环,直到给定的窗口被破坏为止(通过destroy方法或
显式地通过窗口管理器):

  widget.wait_window(window)
/ pre>

一个窗口等于窗口(本身)?



看起来,调用$ code wait_window后的代码不会执行,直到传递到同一个方法的窗口不被销毁。
在下面的工作示例中,我们可以看到刚才说的内容:

 从tkinter import * 

def on_win_request(parent):
dialog = Toplevel()
parent.wait_window(dialog)
#仅当对话框被销毁时执行
print(

r = Tk()
b = Button(r,text ='New Window',command = lambda:on_win_request(r))
b.pack()
b2 = Button(r,text ='Hello!',command = lambda:print(hello))
b2.pack()
r.mainloop )

小事件循环完成!仅当本地 Toplevel 小部件调用对话框被销毁时才会打印。



所以,在真正的情况下,我应该使用这种方法?

解决方案

像文档一样,它等待直到给定的窗口被破坏。它主要用于模态弹出窗口,虽然它本身不会使窗口模态。它只是暂停当前的代码,直到窗口被破坏。要做一个模态的窗口,你必须做一个抓。



最常见的用法是创建Toplevel的一个实例,使用小部件填充该窗口,然后在执行其他操作之前等待窗口被关闭。 >

例如,您可以禁用(或延迟创建)主GUI,弹出服务条款通知,并等待用户确认服务条款,版权,许可证等。一旦窗口被破坏,您可以完成初始化,或启用一些小部件等。



标准文件对话框是一个完美的例子:你弹出对话框,然后您的代码等待用户选择一个文件,然后使用返回的文件名。在内部,对话框的实现使用 wait_window ,以便在对话框被关闭之前不会返回。


It seems that the object that calls this method waits for the window passed as parameter to be destroyed before continue with its own loop...

From the doc strings of the Misc class, we can observe:

def wait_window(self, window=None):
    """Wait until a WIDGET is destroyed.
    If no parameter is given self is used."""

At first glance, it seems like this method can make a Toplevel modal, but this is not true. To make a Toplevel modal, we have to use the grab_set() method.

I have see around other explanations:

wait_window seems to not return until the given widget passed as parameter is not destroyed.

From another place:

wait_window(widget) - Creates a local event that waits for the given widget to be destroyed. This loop doesn't affect the application's mainloop.

From the effbot documentation, we have:

The wait_window enters a local event loop, and doesn’t return until the given window is destroyed (either via the destroy method, or explicitly via the window manager):

widget.wait_window(window)

What exactly means for a window to wait for window (itself)?

It seems that the code that comes after the call to wait_window is not executed until the window passed to the same method is not destroyed. In the following working example, we can see a proof on what just said:

from tkinter import *

def on_win_request(parent):
    dialog = Toplevel()
    parent.wait_window(dialog)
    # executed only when "dialog" is destroyed
    print("Mini-event loop finished!")

r = Tk()
b = Button(r, text='New Window', command=lambda: on_win_request(r))
b.pack()
b2 = Button(r, text='Hello!', command=lambda: print("hello"))
b2.pack()
r.mainloop()

"Mini-event loop finished!" will be printed only when the local Toplevel widget called dialog is destroyed.

So, in exactly what real circumstances should I use this method?

解决方案

Like the documentation states, it waits until the given window is destroyed. It is mostly used for modal popups, though it doesn't itself make a window modal. It merely pauses the current code until the window is destroyed. To make a modal window you have to do a grab as well.

The most common use is to create an instance of Toplevel, populate that window with widgets, then wait for the window to be dismissed before doing some other action.

For instance, you can disable (or defer creation of) the main GUI, pop up a "terms of service" notice, and wait for the user to acknowledge the terms of service, copyright, license, etc. Once the window is destroyed you can then finish initialization, or enable some widgets, etc.

The standard file dialog is a perfect example: you pop up the dialog, then your code waits for the user to pick a file, then it uses the filename that was returned. Internally, the implementation of the dialog uses wait_window so that it doesn't return until the dialog is dismissed.

这篇关于什么是“wait_window”方法做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆