1

I'm trying to develop a python GUI that allows the user to select a file using the native file browser (I'm using Ubuntu 12.04) and display it on the interface.

First: how can I call the native file explorer using a python script and get the returned file.

Also, I'm using Qt4 designer to design the interface. Is there an easier GUI dev tool to display images and brows files?

Thanks for reading!

Sam
  • 41
  • 7

2 Answers2

2

Using sub process call call the program

subprocess.call("nautilus --browser", shell=True)

after saving . Fetch the file using file reader .. u can read documentation here

sundar nataraj
  • 8,524
  • 2
  • 34
  • 46
  • This is part of the answer I'm searching, thanks ! I need to find what is the file explorer's name (not nautilus! the one that shows when you clic a "browse" button in an app, or when you try to save something) – Sam Apr 17 '14 at 12:10
  • sorry no need of path..try using command which u use in terminal to open file explorer – sundar nataraj Apr 17 '14 at 12:13
  • It does open nautilus file manager. But I'm searching for the dialogue box that opens when you want to save/open (like when you press ctrl+s) – Sam Apr 17 '14 at 12:57
  • do u want to open a file ...at beggining or u want to choose one when it open..have u seen this linkhttp://askubuntu.com/questions/8580/opening-the-file-browser-from-terminal – sundar nataraj Apr 17 '14 at 13:00
  • I want to browse files and chose one to open it (picture to be more precise) just the way it's done when you go to "File"-->"Open File" – Sam Apr 17 '14 at 13:02
  • use gnome, gnome-open – sundar nataraj Apr 17 '14 at 13:02
  • The answer you gave me show this : http://doc.ubuntu-fr.org/_media/nautilus_raring.png?w=450 I just need to find the name of this i.stack.imgur.com/A2z6q.png and replace "nautilus --browser" with it. – Sam Apr 17 '14 at 13:12
1

I just found this script that does exactly what I want:

from gi.repository import Gtk

class FileChooserWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="FileChooser Example")

        box = Gtk.Box(spacing=6)
        self.add(box)

        button1 = Gtk.Button("Choose File")
        button1.connect("clicked", self.on_file_clicked)
        box.add(button1)

        button2 = Gtk.Button("Choose Folder")
        button2.connect("clicked", self.on_folder_clicked)
        box.add(button2)

    def on_file_clicked(self, widget):
        dialog = Gtk.FileChooserDialog("Please choose a file", self,
            Gtk.FileChooserAction.OPEN,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OPEN, Gtk.ResponseType.OK))

        self.add_filters(dialog)

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

        dialog.destroy()

    def add_filters(self, dialog):
        filter_text = Gtk.FileFilter()
        filter_text.set_name("Text files")
        filter_text.add_mime_type("text/plain")
        dialog.add_filter(filter_text)

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

        filter_any = Gtk.FileFilter()
        filter_any.set_name("Any files")
        filter_any.add_pattern("*")
        dialog.add_filter(filter_any)

    def on_folder_clicked(self, widget):
        dialog = Gtk.FileChooserDialog("Please choose a folder", self,
            Gtk.FileChooserAction.SELECT_FOLDER,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             "Select", Gtk.ResponseType.OK))
        dialog.set_default_size(800, 400)

        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            print("Select clicked")
            print("Folder selected: " + dialog.get_filename())
        elif response == Gtk.ResponseType.CANCEL:
            print("Cancel clicked")

        dialog.destroy()

win = FileChooserWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Sam
  • 41
  • 7