Search code examples
pythongtkgtk3filechooser

How to make filename/path from Gtk+ (Python 3.4) FileChooserDialog accessible globally


I've got some code that uses the Gtk+ FileChooserDialog in Python 3.4 to allow a user to select a file.

Then, it's supposed to close the dialog (obviously) and continue executing the code that follows the user choosing a file. However, what happens is that the user selects their file, and the code continues, but the dialog doesn't disappear like it should.

I had this issue previously, and we figured out the reason why it was happening then and resolved it, but now it's happening again and although I know what's causing, I don't know how to actually resolve it.

Here's my code:

from gi.repository import Gtk

class FileChooser():

    def __init__(self):

        global path

        dia = Gtk.FileChooserDialog("Please choose a file", None,
            Gtk.FileChooserAction.OPEN,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OPEN, Gtk.ResponseType.OK))

        self.add_filters(dia)

        response = dia.run()

        if response == Gtk.ResponseType.OK:
            print("Open clicked")
            print("File selected: " + dia.get_filename())
            path = dia.get_filename()
        elif response == Gtk.ResponseType.CANCEL:
            print("Cancel clicked")

        dia.destroy()

    def add_filters(self, dia):
        filter_any = Gtk.FileFilter()
        filter_any.set_name("Any files")
        filter_any.add_pattern("*")
        dia.add_filter(filter_any)

        filter_text = Gtk.FileFilter()
        filter_text.set_name('Text files')
        filter_text.add_mime_type('text/plain')
        dia.add_filter(filter_text)

        filter_py = Gtk.FileFilter()
        filter_py.set_name('Python files')
        filter_py.add_mime_type('text/x-python')
        dia.add_filter(filter_py)

        filter_img = Gtk.FileFilter()
        filter_img.set_name('Image')
        filter_img.add_mime_type('image/*')
        dia.add_filter(filter_img)

dialog = FileChooser()

# path variable will be used after this point

The issue here is that, for reasons unknown to me, if I have the global path declaration in the FileChooser() class' __init__() function, the dialog won't disappear.

If I remove that global path declaration, the dialog goes away, but I get a NameError: name 'path' is not defined later in the program when I try to access the path variable!

I have also tried making path global right at the start of the program, but I still get the NameError.

What can I do to make this variable accessible later in my program, while still making the dialog disappear?


Solution

  • Consider the path variable as an instance to FileChooser(). It provides a logical end to have path accessed by the dialog that is representing the FileChooser().

    class FileChooser():
    
        def __init__(self):
            #Stores your path
            self.path = None
    
            dia = Gtk.FileChooserDialog("Please choose a file", None,
                Gtk.FileChooserAction.OPEN,
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                 Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
    
            self.add_filters(dia)
    
            response = dia.run()
    
            if response == Gtk.ResponseType.OK:
                print("Open clicked")
                print("File selected: " + dia.get_filename())
                self.path = dia.get_filename()
            elif response == Gtk.ResponseType.CANCEL:
                print("Cancel clicked")
    
            dia.destroy()
    

    When you create the object.

    dialog = FileChooser()
    

    You can access it as follows:

    dialog.path