Python+Gtk:文件对话框关闭时,如何检查?

2024-03-28 14:50:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

提前感谢您的帮助!在

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

Tags: 文件代码fromimportgtk列表getsession