Search code examples
pythonpathgtkgladefilechooser

Get folder path from FileChooserButton in Python


I'm trying to make a basic file transfer GUI app using Python and Glade, and I've hit a bit of a snag while trying to get the path from the File Chooser Dialog. I've tried everything I know and could find but was unsuccessful.

The .glade file is pretty long so I posted the code here: http://pastebin.com/wsdLMenC

And my Python code looks like this

import datetime
from ftplib import FTP
from magip import *
from gi.repository import Gtk

class FTP():
    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file("ftp.glade")

        go = self.builder.get_object

        self.window = go("window1")
        self.store_magazine = go("store_magazine")
        self.combo_magazine = go("combo_magazine")
        self.store_luni = go("store_luni")
        self.combo_luni = go("combo_luni")
        self.select_folder = go("select_folder")
        self.btn_start = go("btn_start")

        for ip in magazine:
            self.store_magazine.append([magazine[ip]])

        if host in magazine:
            self.combo_magazine.set_active(magip_index[host])

        for luna in luni:
            self.store_luni.append([luni[luna]])

        self.builder.connect_signals(Handlers())
        self.window.show_all()


class Handlers():
    def btn_start_clicked(self, button):
        with open("btn.txt", "a") as muhfile:
            text = "click {}\n".format(datetime.datetime.now())
            muhfile.write(text)


if __name__ == "__main__":
    gui = FTP()
    Gtk.main()

The Handlers class is where I want to put all the signals. Currently there's only one there, to test the START button.

Any help is very much appreciated.

Thank you!


Solution

  • You have to connect to the "file-set" signal. The handler class would look like this:

    class Handlers():
        def btn_start_clicked(self, button):
            with open("btn.txt", "a") as muhfile:
                text = "click {}\n".format(datetime.datetime.now())
                muhfile.write(text)
    
        def directory_set(self, button):
            print(button.get_filename())
    

    and the FileChooserButton in the Glade file like this:

        <child>
          <object class="GtkFileChooserButton" id="select_folder">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="action">select-folder</property>
            <property name="create_folders">False</property>
            <property name="preview_widget_active">False</property>
            <property name="use_preview_label">False</property>
            <property name="title" translatable="yes">Select folder</property>
            <signal name="file-set" handler="directory_set" swapped="no"/>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">3</property>
          </packing>
        </child>