|
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="messagedialog.py" xml:lang="es">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">MessageDialog (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#windows"/>
|
|
Packit |
1470ea |
<link type="next" xref="gmenu.py"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-06-11" 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>Una ventana de mensaje</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>MessageDialog</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/messagedialog.png"/>
|
|
Packit |
1470ea |
Un diálogo de mensajes que imprime mensajes en la terminal, según su elección.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Código usado para generar este ejemplo</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
from gi.repository import Gio
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# constructor for a window (the parent window) with a label
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="GMenu Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(400, 200)
|
|
Packit |
1470ea |
label = Gtk.Label()
|
|
Packit |
1470ea |
label.set_text("This application goes boom!")
|
|
Packit |
1470ea |
self.add(label)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create the message_action (a Gio.SimpleAction) - for the window
|
|
Packit |
1470ea |
message_action = Gio.SimpleAction.new("message", None)
|
|
Packit |
1470ea |
# connect the signal from the action to the function message_cb()
|
|
Packit |
1470ea |
message_action.connect("activate", self.message_cb)
|
|
Packit |
1470ea |
# add the action to the application
|
|
Packit |
1470ea |
app.add_action(message_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for the signal "activate" from the message_action
|
|
Packit |
1470ea |
# in the menu of the parent window
|
|
Packit |
1470ea |
def message_cb(self, action, parameter):
|
|
Packit |
1470ea |
# a Gtk.MessageDialog
|
|
Packit |
1470ea |
messagedialog = Gtk.MessageDialog(parent=self,
|
|
Packit |
1470ea |
flags=Gtk.DialogFlags.MODAL,
|
|
Packit |
1470ea |
type=Gtk.MessageType.WARNING,
|
|
Packit |
1470ea |
buttons=Gtk.ButtonsType.OK_CANCEL,
|
|
Packit |
1470ea |
message_format="This action will cause the universe to stop existing.")
|
|
Packit |
1470ea |
# connect the response (of the button clicked) to the function
|
|
Packit |
1470ea |
# dialog_response()
|
|
Packit |
1470ea |
messagedialog.connect("response", self.dialog_response)
|
|
Packit |
1470ea |
# show the messagedialog
|
|
Packit |
1470ea |
messagedialog.show()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def dialog_response(self, widget, response_id):
|
|
Packit |
1470ea |
# if the button clicked gives response OK (-5)
|
|
Packit |
1470ea |
if response_id == Gtk.ResponseType.OK:
|
|
Packit |
1470ea |
print("*boom*")
|
|
Packit |
1470ea |
# if the button clicked gives response CANCEL (-6)
|
|
Packit |
1470ea |
elif response_id == Gtk.ResponseType.CANCEL:
|
|
Packit |
1470ea |
print("good choice")
|
|
Packit |
1470ea |
# if the messagedialog is destroyed (by pressing ESC)
|
|
Packit |
1470ea |
elif response_id == Gtk.ResponseType.DELETE_EVENT:
|
|
Packit |
1470ea |
print("dialog closed or cancelled")
|
|
Packit |
1470ea |
# finally, destroy the messagedialog
|
|
Packit |
1470ea |
widget.destroy()
|
|
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 quit_cb(self, action, parameter):
|
|
Packit |
1470ea |
self.quit()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create a menu (a Gio.Menu)
|
|
Packit |
1470ea |
menu = Gio.Menu()
|
|
Packit |
1470ea |
# append a menu item with label "Message" and action "app.message"
|
|
Packit |
1470ea |
menu.append("Message", "app.message")
|
|
Packit |
1470ea |
# append a menu item with label "Quit" and action "app.quit"
|
|
Packit |
1470ea |
menu.append("Quit", "app.quit")
|
|
Packit |
1470ea |
# set menu as the menu for the application
|
|
Packit |
1470ea |
self.set_app_menu(menu)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a new simpleaction - for the application
|
|
Packit |
1470ea |
quit_action = Gio.SimpleAction.new("quit", None)
|
|
Packit |
1470ea |
quit_action.connect("activate", self.quit_cb)
|
|
Packit |
1470ea |
self.add_action(quit_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="methods">
|
|
Packit |
1470ea |
<title>Métodos útiles para un widget «MessageDialog»</title>
|
|
Packit |
1470ea |
En la línea 18, la señal «activate» se conecta a la función de retorno de llamada message_cb() usando widget.connect(señal, función de retorno de llamada) . Consulte la <link xref="signals-callbacks.py"/> para obtener una explicación más detallada.
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>En el constructor de un «MessageDialog» se pueden configurar opciones como Gtk.DialogFlags.DESTROY_WITH_PARENT (para destruir la ventana de diálogo cuando se destruya su ventana madre) o Gtk.DialogFlags.MODAL (sin interacción con otras ventanas de la aplicación). </item>
|
|
Packit |
1470ea |
<item>En el constructor del «MessageDialog» se puede elegir entre los tipos Gtk.MessageType.INFO, Gtk.MessageType.WARNING, Gtk.MessageType.QUESTION, Gtk.MessageType.ERROR, Gtk.MessageType.OTHER dependiendo de qué tipo de mensaje quiera. </item>
|
|
Packit |
1470ea |
<item>En el constructor del «MessageDialog» se puede elegir entre los botones Gtk.ButtonsType.NONE, Gtk.ButtonsType.OK, Gtk.ButtonsType.CLOSE, Gtk.ButtonsType.CANCEL, Gtk.ButtonsType.YES_NO, Gtk.ButtonsType.OK_CANCEL , o cualquier botón usando add_button() como en un «Gtk.Dialog». </item>
|
|
Packit |
1470ea |
<item>Se podría sustituir la imagen predeterminada del «MessageDialog» con otra usando
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
image = Gtk.Image()
|
|
Packit |
1470ea |
image.set_from_stock(Gtk.STOCK_CAPS_LOCK_WARNING, Gtk.IconSize.DIALOG)
|
|
Packit |
1470ea |
image.show()
|
|
Packit |
1470ea |
messagedialog.set_image(image)
|
|
Packit |
1470ea |
donde Gtk.STOCK_CAPS_LOCK_WARNING es cualquier imagen de los <link href="http://developer.gnome.org/gtk3/unstable/gtk3-Stock-Items.html">elementos del almacén</link>. También se podría establecer cualquier imagen como en el widget «Image», con image.set_from_file("archivo.png") . </item>
|
|
Packit |
1470ea |
<item>format_secondary_text("algún mensaje secundario") establece un mensaje secundario. El texto primario aparece en negrita. </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/unstable/GtkMessageDialog.html">GtkMessageDialog</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkDialog.html">GtkDialog</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkWindow.html">GtkWindow</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/gio/unstable/GActionMap.html">GActionMap</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gio/stable/GMenu.html">GMenu</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkApplication.html">GtkApplication</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|