Python + Gtk: File dialog when closed, how to check?

367 Views Asked by At

In my code I use an open Gtk file dialog first. After a file has been chosen, a list of all open windows is obtained, which I realize with the help of Gtk.

The point is that in my current code (see code below), the window of the open-file dialog is also mentioned (last in the list), although it should be closed and therefore non-existent. Even if I put in a 'sleep(5)' after the dialog routine, the dialog window is in the list of windows. Strange is that the dialog window is frozen within the 5 seconds, it should be closed! How can I avoid that the dialog window is in the list? Or is there any means to check if the window is non-existent, something like a wait_for_closed_window routine?

Thanks for any help in advance!

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
1

There are 1 best solutions below

0
On BEST ANSWER

Add the following after screen = Wnck.Screen.get_default()

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

no need for sleep