How to open a Python file selection window with GTK?

Asked

Viewed 2,885 times

1

I have a Python application using GTK graphical user interface. This interface consists of a screen with data input a button, the goal of the program is to save data in spreadsheets in a specific directory.

I need, when clicking the button, to open a screen to choose the directory in which this file will be saved.

def botao_arquivos(widget):
    #comando para abrir a pasta dos arquivos    
    pass
  • 1

    What do you mean by "open a folder"? Display on screen what are the files? Tree-view these files?

  • 1

    @Filipesantos No, it’s not clear. Opening the folder is very vague, by the way, technically speaking, there is no such thing as opening the folder. I think you mean you want to show the contents of the folder by the system file manager.

  • @Filipesantos which graphical interface are you using? Currently we can only do one ls, at most, and return the list of files inside the folder

  • 1

    Aaaah good, you want to open one dialog of save, if I’m not mistaken the graphical interface (of the operating system) gives you some option. By the way, when @Jeffersonquesado asked which graphical interface you are using he spoke the graphical interface of the xD operating system

  • @Filipesantos Here is the starting point for what you want to do (http://python-gtk-3-tutorial.readthedocs.io/en/latest/dialogs.html#filechooserdialog)

  • Now I understand the question.

  • I thought to run a cmd in the terminal by python like Nautilus to open the folder, or xdg-open what they think?

  • 1

    people - it’s not so hard -all frameworks for graphics programs have something like that already ready. I don’t know if before the question was edited it was clear that the AP was using GTK - if it wasn’t, that’s what had to be checked. But since we know that uses GTK just see the documentation there - it would be analogous if it were Tkinter, Qt5, win32com , etc... "open a separate program using Nautilus" would be quite bizarre.

  • @jsbueno this was only clear in the second revision

Show 4 more comments

1 answer

2


The GTK framework has the "Filechooserdialog" which is a complete window to browse the file system, and allows either the choice of an existing file (or directory) ("open" action), or the user to write a new name ("save" action").

Since your question, unlike the practices indicated, has nothing of your code, I also have no way to give an example of use (without writing an entire, functional program).

The Filechooser documentation as used by Python is here: http://python-gtk-3-tutorial.readthedocs.io/en/latest/dialogs.html#filechooserdialog

basically, the call to create the dialogue is:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk as gtk

...

dialog = gtk.FileChooserDialog(
    title="Please choose a file", window=sua_janela_principal_,
    action=gtk.FileChooserAction.OPEN,
    buttons=(gtk.STOCK_CANCEL, gtk.ResponseType.CANCEL,
             gtk.STOCK_OPEN, gtk.ResponseType.OK))
)

After you build the dialog and configure the desired parameters, call the method run: the current thread of your program pauses while the dialog is used by the user - after the user chooses and confirms a file, check which name was chosen by calling:

nome = dialog.get_filename()
dialog.destroy()

In the above url there is a complete program using the dialog with more details (what to do in case the user cancels the action, for example)

  • has to open otherwise, for example a command by terminal in some way that I do it by system, to open a directory defined?

  • I was able to create the screen and open the folder with the reports, but now how do I open it? I have csv and html files. Use some system command to open?

  • the file dialog should give you a filename with the full path. Then Voce uses your program to do whatever you want with that name. In particular to render HTML + CSS files to the user, the most practical way is to open an external browser pointing to your html file - Python has the "webbrowser" module to do this, see its documentation.

  • If you need to take the data that is in this file, you read the data to memory, and program the extraction of the relevant data - with if/Else, regular expressions or the use of specialized modules like beautifulsoup

Browser other questions tagged

You are not signed in. Login or sign up in order to post.