0

I'm new to GTK. I've been looking for an answer to my question but I haven't found anything. The thing is that I want to use GTK FileChooser to select a file. I've been looking at the example available here:

http://pygtk.org/pygtk2tutorial/examples/filechooser.py

Then I used the knowledge that I've learned in the example to add this feature to my program. However, I noticed that when I choose a file, the window hangs. By this I mean that the window to select a file never disappears until I close the whole program.

So I went back to the example, and just waited some time to see if the window was destroyed. And in fact, it didn't. The window to select a file disappears only when the program finishes. Here is the code:

#!/usr/bin/env python
# example filechooser.py

import pygtk
pygtk.require('2.0')

import gtk
import time

# Check for new pygtk: this is new class in PyGtk 2.4
if gtk.pygtk_version < (2,3,90):
   print "PyGtk 2.3.90 or later required for this example"
   raise SystemExit

dialog = gtk.FileChooserDialog("Open..",
                               None,
                               gtk.FILE_CHOOSER_ACTION_OPEN,
                               (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                gtk.STOCK_OPEN, gtk.RESPONSE_OK))
dialog.set_default_response(gtk.RESPONSE_OK)

filter = gtk.FileFilter()
filter.set_name("All files")
filter.add_pattern("*")
dialog.add_filter(filter)

filter = gtk.FileFilter()
filter.set_name("Images")
filter.add_mime_type("image/png")
filter.add_mime_type("image/jpeg")
filter.add_mime_type("image/gif")
filter.add_pattern("*.png")
filter.add_pattern("*.jpg")
filter.add_pattern("*.gif")
filter.add_pattern("*.tif")
filter.add_pattern("*.xpm")
dialog.add_filter(filter)

response = dialog.run()
if response == gtk.RESPONSE_OK:
    print dialog.get_filename(), 'selected'
elif response == gtk.RESPONSE_CANCEL:
    print 'Closed, no files selected'
dialog.destroy()
time.sleep(3)

And in those 3 seconds the window is there, so I guess it is never destroyed and I wonder why. Even if I wait 10 seconds, the window never disappears until the program finishes. I need to know if there's something wrong in the example or what I'm doing wrong since this is not something I would like to happen in my program.

Just in case, I'm using Python 2.7.3 and Debian 7.

Thanks in advance!

Roxy
  • 391
  • 2
  • 14

1 Answers1

0

You can't use GTK+ without a mainloop: the only reason the code works at all now is that dialog.run() happens to run its own internal mainloop for the duration of that call. Afterwards there is no mainloop to handle things like calling the destroy handler of the window.

After you've initialized your app, run gtk.main() without showing the dialog. Then from an appropriate signal handler call dialog.run() and the related code -- for testing purposes you could just add a glib.idle_add(handler_that_runs_dialog) before calling main: that way the handler gets called right after main loop starts running.

Jussi Kukkonen
  • 13,857
  • 1
  • 37
  • 54