【问题标题】:Python + Gtk: File dialog when closed, how to check?Python + Gtk:关闭时的文件对话框,如何检查?
【发布时间】:2015-01-22 19:43:49
【问题描述】:

在我的代码中,我首先使用了一个打开的 Gtk 文件对话框。选择文件后,将获得所有打开的窗口的列表,这是我在 Gtk 的帮助下实现的。

关键是在我当前的代码中(参见下面的代码),还提到了打开文件对话框的窗口(列表中的最后一个),尽管它应该被关闭,因此不存在。即使我在对话例程之后放入“睡眠(5)”,对话窗口也在窗口列表中。奇怪的是对话窗口在5秒内就被冻结了,应该是关闭了!如何避免对话框窗口在列表中?或者有什么方法可以检查窗口是否不存在,比如 wait_for_closed_window 例程?

提前感谢您的帮助!

from gi.repository import Gtk
from gi.repository import Wnck
from time import sleep

def open_dialog_load_file():

    dialog = Gtk.FileChooserDialog("Open ...", None,
                                   Gtk.FileChooserAction.OPEN,
                                   (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                   Gtk.STOCK_OPEN, Gtk.ResponseType.OK))

    response = dialog.run()
    if response == Gtk.ResponseType.OK:
        session_file = dialog.get_filename()
    elif response == Gtk.ResponseType.CANCEL:
        session_file = ""
    dialog.destroy()
    print session_file

    return session_file


if __name__ == '__main__':    

    open_dialog_load_file()

    sleep(2)

    Gtk.init([])
    screen = Wnck.Screen.get_default()
    screen.force_update()
    list_wnds = screen.get_windows()
    screen = None
    Wnck.shutdown()

    print
    for wnd in list_wnds: 
        print "        " + wnd.get_name()
    print

【问题讨论】:

  • 我已经打开了另一个与此相关的问题。它询问如何检查显示的对话框。它们不是窗口,因此它们不会出现在您使用screen.get_windows() 获得的窗口列表中,但我无法告诉您您实际上是如何处理对话框的。

标签: python pygtk openfiledialog


【解决方案1】:

screen = Wnck.Screen.get_default()之后添加以下内容

while Gtk.events_pending():
    Gtk.main_iteration() 

不需要sleep

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多