How to open a file selection window in Python with GTK?

1

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

I need to click on 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
    
asked by anonymous 02.02.2018 / 16:23

1 answer

2

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

Since your question, unlike the practices indicated, does not have anything 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: link

Basically, the call to create the dialog 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 run method: the current thread of your program pauses while the dialog is used by the user - after it chooses and confirms a file, check name chosen by calling:

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

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

    
02.02.2018 / 20:27