Python + Gtk:文件对话框关闭时如何检查?
问题描述:
在我的代码中,我首先使用一个打开的Gtk文件对话框。选择文件后,将获得所有打开的窗口的列表,这是我在Gtk的帮助下实现的。Python + Gtk:文件对话框关闭时如何检查?
的一点是,在我当前的代码(见下面的代码),打开的文件对话框的窗口也被(在列表的最后)提到,尽管它应该被关闭,因此不存在的。即使我在对话框例程后面输入'sleep(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 = Wnck.Screen.get_default()
while Gtk.events_pending():
Gtk.main_iteration()
无需添加以下为sleep
我打开这与此相关的另一个问题。它询问如何检查显示的对话框。它们不是窗口,所以它们不会显示在使用'screen.get_windows()'获得的窗口列表中,但我无法告诉你实际如何处理对话框。 – Zelphir 2015-09-05 21:09:33