[pygtk] FileChooserDialog can not run twice?

Zhenjiang Lan lan.zhenjiang at gmail.com
Mon Mar 19 12:02:03 WST 2012


Hi all experts,

I'm new in Python and GTK. I have a trouble and wondering if anybody
can help me:

I create a button, connect to a FileChooserDialog. When I click the
button, I can choose a file and print it's name.
But, if I click 'cancel' for the first time, then I click the button
again, it crashed..

Can anyone give me a hand? Thank you!

Here's my codes:
********************************

import pygtk
pygtk.require('2.0')

import gtk

# 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


class choosefile:
	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)
	
class mainwindow:
	def __init__(self):
		window = self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		window.connect("destroy",lambda w: gtk.main_quit())
		window.set_title("gtk test")
		
		button = gtk.Button(label="run")
		button.connect("clicked",self.run)
		window.add(button)
		button.show()
		
		window.show()
		
	def run(self,item):
		fl = choosefile()	
		response = fl.dialog.run()	
		if response == gtk.RESPONSE_OK:
			filename = fl.dialog.get_filename()
		elif response == gtk.RESPONSE_CANCEL:
			print 'Closed, no files selected'
		fl.dialog.destroy()
		print filename
			
wd = mainwindow()
gtk.main()


More information about the pygtk mailing list