Blame platform-demos/es/filechooserdialog.py.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="filechooserdialog.py" xml:lang="es">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">FileChooserDialog (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#file-selectors"/>
Packit 1470ea
    <link type="seealso" xref="gmenu.py"/>
Packit 1470ea
    <link type="seealso" xref="toolbar_builder.py"/>
Packit 1470ea
    <link type="seealso" xref="textview.py"/>
Packit 1470ea
    <link type="next" xref="combobox.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-08-14" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Marta Maria Casetti</name>
Packit 1470ea
      <email its:translate="no">mmcasetti@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>Un diálogo adecuado para comandos «Abrir» y «Guardar»</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Daniel Mustieles</mal:name>
Packit 1470ea
      <mal:email>daniel.mustieles@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011 - 2017</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Nicolás Satragno</mal:name>
Packit 1470ea
      <mal:email>nsatragno@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2012 - 2013</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Jorge González</mal:name>
Packit 1470ea
      <mal:email>jorgegonz@svn.gnome.org</mal:email>
Packit 1470ea
      <mal:years>2011</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>FileChooserDialog</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/filechooserdialog_save.png"/>
Packit 1470ea
  

Este «FileChooserDialog» guarda un documento de texto, que puede abrirse o escribirse desde cero en un «TextView» (vea a continuación).

Packit 1470ea
  <media type="image" mime="image/png" src="media/filechooserdialog_menu.png"/>
Packit 1470ea
  

También es posible llamar a un «FileChooserDialog» para abrir un documento nuevo.

Packit 1470ea
Packit 1470ea
  <links type="sections"/>
Packit 1470ea
  
Packit 1470ea
  <section id="overview">
Packit 1470ea
  <title>Pasos para recrear el ejemplo</title>
Packit 1470ea
  <steps>
Packit 1470ea
    <item>

Cree un archivo «.ui» para describir un menú de aplicación con elementos «New», «Open», «Save», «Save as», y «Quit». Esto puede hacerse con Glade o en un editor de texto. Consulte <link xref="#xml"/>

</item>
Packit 1470ea
    <item>

Cree un programa en Python para un «Gtk.TextView» con un «Gtk.Buffer» self.buffer, y un self.file que será un «Gio.File» establecido inicialmente a None.

</item>
Packit 1470ea
    <item>

En este programa, también cree las acciones correspondientes a los elementos en el menú de aplicación, conéctelos a funciones de retorno de llamada, e importe el menú en el método do_startup() con un «Gtk.Builder».

</item>
Packit 1470ea
    <item>

Las acciones y funciones de retorno de llamada «New» y «Quit» son bastante directas, consulte <link xref="#code"/>. Consulte la <link xref="signals-callbacks.py"/> para obtener una explicación más detallada de señales y funciones de retorno de llamada.

</item>
Packit 1470ea
    <item>

La devolución de llamada «Open» debe crear y abrir un «Gtk.FileChooserDialog» para «Abrir», conectado con otra función de retorno de llamada por cada uno de los dos botones «Open» y «Cancel» del «FileChooserDialog».

</item>
Packit 1470ea
    <item>

«Save as» funciona básicamente como «Open», pero la función de retorno de llamada del botón «Save» depende en un método más complejo, save_to_file().

</item>
Packit 1470ea
    <item>

«Save» puede reducirse al caso en el que el archivo es None, es decir en el caso en el que self.file es un archivo nuevo, que a su vez es el caso de «Save as»; y al caso en el que el archivo no es None, que a su vez se reduce a save_to_file().

</item>
Packit 1470ea
    <item>

Finalmente, el método save_to_file(): consulte <link xref="#code"/>, líneas 146 - 175.

</item>
Packit 1470ea
  </steps>
Packit 1470ea
  </section>
Packit 1470ea
  
Packit 1470ea
  <section id="xml">
Packit 1470ea
  <title>Archivo XML que crea el menú de aplicación</title>
Packit 1470ea
  <?xml version="1.0"?>
Packit 1470ea
<interface>
Packit 1470ea
  <menu id="appmenu">
Packit 1470ea
    <section>
Packit 1470ea
      <item>
Packit 1470ea
        <attribute name="label">New</attribute>
Packit 1470ea
        <attribute name="action">win.new</attribute>
Packit 1470ea
      </item>
Packit 1470ea
      <item>
Packit 1470ea
        <attribute name="label">Open</attribute>
Packit 1470ea
        <attribute name="action">win.open</attribute>
Packit 1470ea
      </item>
Packit 1470ea
    </section>
Packit 1470ea
    <section>
Packit 1470ea
      <item>
Packit 1470ea
        <attribute name="label">Save</attribute>
Packit 1470ea
        <attribute name="action">win.save</attribute>
Packit 1470ea
      </item>
Packit 1470ea
      <item>
Packit 1470ea
        <attribute name="label">Save As...</attribute>
Packit 1470ea
        <attribute name="action">win.save-as</attribute>
Packit 1470ea
      </item>
Packit 1470ea
    </section>
Packit 1470ea
    <section>
Packit 1470ea
      <item>
Packit 1470ea
        <attribute name="label">Quit</attribute>
Packit 1470ea
        <attribute name="action">app.quit</attribute>
Packit 1470ea
      </item>
Packit 1470ea
    </section>
Packit 1470ea
  </menu>
Packit 1470ea
</interface>
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
  
Packit 1470ea
  <section id="code">
Packit 1470ea
  <title>Código usado para generar este ejemplo</title>
Packit 1470ea
  from gi.repository import Gtk
Packit 1470ea
from gi.repository import Gdk
Packit 1470ea
from gi.repository import Gio
Packit 1470ea
from gi.repository import GObject
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(
Packit 1470ea
            self, title="FileChooserDialog Example", application=app)
Packit 1470ea
        self.set_default_size(400, 400)
Packit 1470ea
Packit 1470ea
        # the actions for the window menu, connected to the callback functions
Packit 1470ea
        new_action = Gio.SimpleAction.new("new", None)
Packit 1470ea
        new_action.connect("activate", self.new_callback)
Packit 1470ea
        self.add_action(new_action)
Packit 1470ea
Packit 1470ea
        open_action = Gio.SimpleAction.new("open", None)
Packit 1470ea
        open_action.connect("activate", self.open_callback)
Packit 1470ea
        self.add_action(open_action)
Packit 1470ea
Packit 1470ea
        save_action = Gio.SimpleAction.new("save", None)
Packit 1470ea
        save_action.connect("activate", self.save_callback)
Packit 1470ea
        self.add_action(save_action)
Packit 1470ea
Packit 1470ea
        save_as_action = Gio.SimpleAction.new("save-as", None)
Packit 1470ea
        save_as_action.connect("activate", self.save_as_callback)
Packit 1470ea
        self.add_action(save_as_action)
Packit 1470ea
Packit 1470ea
        # the file
Packit 1470ea
        self.file = None
Packit 1470ea
Packit 1470ea
        # the textview with the buffer
Packit 1470ea
        self.buffer = Gtk.TextBuffer()
Packit 1470ea
        textview = Gtk.TextView(buffer=self.buffer)
Packit 1470ea
        textview.set_wrap_mode(Gtk.WrapMode.WORD)
Packit 1470ea
Packit 1470ea
        # a scrolled window for the textview
Packit 1470ea
        self.scrolled_window = Gtk.ScrolledWindow()
Packit 1470ea
        self.scrolled_window.set_policy(
Packit 1470ea
            Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
Packit 1470ea
        self.scrolled_window.add(textview)
Packit 1470ea
        self.scrolled_window.set_border_width(5)
Packit 1470ea
Packit 1470ea
        # add the scrolled window to the window
Packit 1470ea
        self.add(self.scrolled_window)
Packit 1470ea
Packit 1470ea
    # callback for new
Packit 1470ea
    def new_callback(self, action, parameter):
Packit 1470ea
        self.buffer.set_text("")
Packit 1470ea
        print("New file created")
Packit 1470ea
Packit 1470ea
    # callback for open
Packit 1470ea
    def open_callback(self, action, parameter):
Packit 1470ea
        # create a filechooserdialog to open:
Packit 1470ea
        # the arguments are: title of the window, parent_window, action,
Packit 1470ea
        # (buttons, response)
Packit 1470ea
        open_dialog = Gtk.FileChooserDialog("Pick a file", self,
Packit 1470ea
                                            Gtk.FileChooserAction.OPEN,
Packit 1470ea
                                           (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Packit 1470ea
                                            Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT))
Packit 1470ea
Packit 1470ea
        # not only local files can be selected in the file selector
Packit 1470ea
        open_dialog.set_local_only(False)
Packit 1470ea
        # dialog always on top of the textview window
Packit 1470ea
        open_dialog.set_modal(True)
Packit 1470ea
        # connect the dialog with the callback function open_response_cb()
Packit 1470ea
        open_dialog.connect("response", self.open_response_cb)
Packit 1470ea
        # show the dialog
Packit 1470ea
        open_dialog.show()
Packit 1470ea
Packit 1470ea
    # callback function for the dialog open_dialog
Packit 1470ea
    def open_response_cb(self, dialog, response_id):
Packit 1470ea
        open_dialog = dialog
Packit 1470ea
        # if response is "ACCEPT" (the button "Open" has been clicked)
Packit 1470ea
        if response_id == Gtk.ResponseType.ACCEPT:
Packit 1470ea
            # self.file is the file that we get from the FileChooserDialog
Packit 1470ea
            self.file = open_dialog.get_file()
Packit 1470ea
            # an empty string (provisionally)
Packit 1470ea
            content = ""
Packit 1470ea
            try:
Packit 1470ea
                # load the content of the file into memory:
Packit 1470ea
                # success is a boolean depending on the success of the operation
Packit 1470ea
                # content is self-explanatory
Packit 1470ea
                # etags is an entity tag (can be used to quickly determine if the
Packit 1470ea
                # file has been modified from the version on the file system)
Packit 1470ea
                [success, content, etags] = self.file.load_contents(None)
Packit 1470ea
            except GObject.GError as e:
Packit 1470ea
                print("Error: " + e.message)
Packit 1470ea
            # set the content as the text into the buffer
Packit 1470ea
            self.buffer.set_text(content, len(content))
Packit 1470ea
            print("opened: " + open_dialog.get_filename())
Packit 1470ea
        # if response is "CANCEL" (the button "Cancel" has been clicked)
Packit 1470ea
        elif response_id == Gtk.ResponseType.CANCEL:
Packit 1470ea
            print("cancelled: FileChooserAction.OPEN")
Packit 1470ea
        # destroy the FileChooserDialog
Packit 1470ea
        dialog.destroy()
Packit 1470ea
Packit 1470ea
    # callback function for save_as
Packit 1470ea
    def save_as_callback(self, action, parameter):
Packit 1470ea
        # create a filechooserdialog to save:
Packit 1470ea
        # the arguments are: title of the window, parent_window, action,
Packit 1470ea
        # (buttons, response)
Packit 1470ea
        save_dialog = Gtk.FileChooserDialog("Pick a file", self,
Packit 1470ea
                                            Gtk.FileChooserAction.SAVE,
Packit 1470ea
                                           (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Packit 1470ea
                                            Gtk.STOCK_SAVE, Gtk.ResponseType.ACCEPT))
Packit 1470ea
        # the dialog will present a confirmation dialog if the user types a file name that
Packit 1470ea
        # already exists
Packit 1470ea
        save_dialog.set_do_overwrite_confirmation(True)
Packit 1470ea
        # dialog always on top of the textview window
Packit 1470ea
        save_dialog.set_modal(True)
Packit 1470ea
        # if self.file has already been saved
Packit 1470ea
        if self.file is not None:
Packit 1470ea
            try:
Packit 1470ea
                # set self.file as the current filename for the file chooser
Packit 1470ea
                save_dialog.set_file(self.file)
Packit 1470ea
            except GObject.GError as e:
Packit 1470ea
                print("Error: " + e.message)
Packit 1470ea
        # connect the dialog to the callback function save_response_cb()
Packit 1470ea
        save_dialog.connect("response", self.save_response_cb)
Packit 1470ea
        # show the dialog
Packit 1470ea
        save_dialog.show()
Packit 1470ea
Packit 1470ea
    # callback function for the dialog save_dialog
Packit 1470ea
    def save_response_cb(self, dialog, response_id):
Packit 1470ea
        save_dialog = dialog
Packit 1470ea
        # if response is "ACCEPT" (the button "Save" has been clicked)
Packit 1470ea
        if response_id == Gtk.ResponseType.ACCEPT:
Packit 1470ea
            # self.file is the currently selected file
Packit 1470ea
            self.file = save_dialog.get_file()
Packit 1470ea
            # save to file (see below)
Packit 1470ea
            self.save_to_file()
Packit 1470ea
        # if response is "CANCEL" (the button "Cancel" has been clicked)
Packit 1470ea
        elif response_id == Gtk.ResponseType.CANCEL:
Packit 1470ea
            print("cancelled: FileChooserAction.SAVE")
Packit 1470ea
        # destroy the FileChooserDialog
Packit 1470ea
        dialog.destroy()
Packit 1470ea
Packit 1470ea
    # callback function for save
Packit 1470ea
    def save_callback(self, action, parameter):
Packit 1470ea
        # if self.file is not already there
Packit 1470ea
        if self.file is not None:
Packit 1470ea
            self.save_to_file()
Packit 1470ea
        # self.file is a new file
Packit 1470ea
        else:
Packit 1470ea
            # use save_as
Packit 1470ea
            self.save_as_callback(action, parameter)
Packit 1470ea
Packit 1470ea
    # save_to_file
Packit 1470ea
    def save_to_file(self):
Packit 1470ea
        # get the content of the buffer, without hidden characters
Packit 1470ea
        [start, end] = self.buffer.get_bounds()
Packit 1470ea
        current_contents = self.buffer.get_text(start, end, False)
Packit 1470ea
        # if there is some content
Packit 1470ea
        if current_contents != "":
Packit 1470ea
            # set the content as content of self.file.
Packit 1470ea
            # arguments: contents, etags, make_backup, flags, GError
Packit 1470ea
            try:
Packit 1470ea
                self.file.replace_contents(current_contents,
Packit 1470ea
                                           None,
Packit 1470ea
                                           False,
Packit 1470ea
                                           Gio.FileCreateFlags.NONE,
Packit 1470ea
                                           None)
Packit 1470ea
                print("saved: " + self.file.get_path())
Packit 1470ea
            except GObject.GError as e:
Packit 1470ea
                print("Error: " + e.message)
Packit 1470ea
        # if the contents are empty
Packit 1470ea
        else:
Packit 1470ea
            # create (if the file does not exist) or overwrite the file in readwrite mode.
Packit 1470ea
            # arguments: etags, make_backup, flags, GError
Packit 1470ea
            try:
Packit 1470ea
                self.file.replace_readwrite(None,
Packit 1470ea
                                            False,
Packit 1470ea
                                            Gio.FileCreateFlags.NONE,
Packit 1470ea
                                            None)
Packit 1470ea
                print("saved: " + self.file.get_path())
Packit 1470ea
            except GObject.GError as e:
Packit 1470ea
                print("Error: " + e.message)
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
        # app action quit, connected to the callback function
Packit 1470ea
        quit_action = Gio.SimpleAction.new("quit", None)
Packit 1470ea
        quit_action.connect("activate", self.quit_callback)
Packit 1470ea
        self.add_action(quit_action)
Packit 1470ea
Packit 1470ea
        # get the menu from the ui file with a builder
Packit 1470ea
        builder = Gtk.Builder()
Packit 1470ea
        try:
Packit 1470ea
            builder.add_from_file("filechooserdialog.ui")
Packit 1470ea
        except:
Packit 1470ea
            print("file not found")
Packit 1470ea
            sys.exit()
Packit 1470ea
        menu = builder.get_object("appmenu")
Packit 1470ea
        self.set_app_menu(menu)
Packit 1470ea
Packit 1470ea
    # callback function for quit
Packit 1470ea
    def quit_callback(self, action, parameter):
Packit 1470ea
        self.quit()
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
  
Packit 1470ea
  <section id="methods">
Packit 1470ea
  <title>Métodos útiles para un «FileChooserDialog»</title>
Packit 1470ea
    

Tenga en cuenta que la acción del «FileChooserDialog» puede ser una de las siguientes: Gtk.FileChooserAction.OPEN (el selector de archivo sólo le permitirá al usuario elegir un archivo existente), Gtk.FileChooserAction.SAVE (se le permitirá al usuario elegir un archivo existente, o introducir uno nuevo), Gtk.FileChooserAction.SELECT_FOLDER se le permitirá al usuario elegir una carpeta existente), o Gtk.FileChooserAction.CREATE_FOLDER (se le permitirá al usuario elegir una carpeta nueva o existente).

Packit 1470ea
    

Además de los métodos usados en el <link xref="#code"/>, existen:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

set_show_hidden(True) se usa para mostrar carpetas y archivos ocultos.

</item>
Packit 1470ea
      <item>

set_select_multiple(True) indica que se pueden seleccionar múltiples archivos. Esto sólo es relevante si el modo es Gtk.FileChooserAction.OPEN o Gtk.FileChooserAction.SELECT_FOLDER.

</item>
Packit 1470ea
      <item>

En el diálogo «Save as», set_current_name(nombre_actual) establece nombre_actual en el selector de archivo, como si lo hubiera introducido el usuario; nombre_actual puede ser algo como Sin nombre.txt. Este método no debe usarse excepto en un diálogo «Save as».

</item>
Packit 1470ea
      <item>

La carpeta actual predeterminada es «elementos recientes». Para establecer otra carpeta use set_current_folder_uri(uri); pero tenga en cuenta que debe usar este método y hacer que el selector de archivo muestre una carpeta específica sólo cuando está ejecutando un comando «Save as» y ya tenga un archivo guardado en algún lado.

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
  <title>Referencias de la API</title>
Packit 1470ea
  

En este ejemplo se usa lo siguiente:

Packit 1470ea
  <list>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkFileChooserDialog.html">GtkFileChooserDialog</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkFileChooser.html">GtkFileChooser</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkWindow.html">GtkWindow</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkTextView.html">GtkTextView</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkTextBuffer.html">GtkTextBuffer</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkScrolledWindow.html">GtkScrolledWindow</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gio/stable/GFile.html">GFile</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gio/stable/GSimpleAction.html">GSimpleAction</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkBuilder.html">GtkBuilder</link>

</item>
Packit 1470ea
  </list>
Packit 1470ea
  </section>
Packit 1470ea
</page>