2014-11-24 56 views
1

在我的代碼中,我首先使用一個打開的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 
+0

我打開這與此相關的另一個問題。它詢問如何檢查顯示的對話框。它們不是窗口,所以它們不會顯示在使用'screen.get_windows()'獲得的窗口列表中,但我無法告訴你實際如何處理對話框。 – Zelphir 2015-09-05 21:09:33

回答

1

screen = Wnck.Screen.get_default()

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

無需添加以下爲sleep