Blame platform-demos/pt_BR/messagedialog.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="messagedialog.py" xml:lang="pt-BR">
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>A message window</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Rafael Ferreira</mal:name>
Packit 1470ea
      <mal:email>rafael.f.f1@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2013</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
  

A message dialog which prints messages on the terminal, depending on your choices.

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
  <title>Code used to generate this example</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>Useful methods for a MessageDialog widget</title>
Packit 1470ea
    

In line 18 the signal "activate" is connected to the callback function message_cb() using widget.connect(signal, callback function). See <link xref="signals-callbacks.py"/> for a more detailed explanation.

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

In the constructor of MessageDialog we could set flags as Gtk.DialogFlags.DESTROY_WITH_PARENT (to destroy the messagedialog window when its parent window is destroyed) or as Gtk.DialogFlags.MODAL (no interaction with other windows of the application).

</item>
Packit 1470ea
    <item>

In the constructor of MessageDialog we could set type as any of Gtk.MessageType.INFO, Gtk.MessageType.WARNING, Gtk.MessageType.QUESTION, Gtk.MessageType.ERROR, Gtk.MessageType.OTHER depending on what type of message we want.

</item>
Packit 1470ea
    <item>

In the constructor of MessageDialog we could set buttons as any of Gtk.ButtonsType.NONE, Gtk.ButtonsType.OK, Gtk.ButtonsType.CLOSE, Gtk.ButtonsType.CANCEL, Gtk.ButtonsType.YES_NO, Gtk.ButtonsType.OK_CANCEL, or any button using add_button() as in Gtk.Dialog.

</item>
Packit 1470ea
    <item>

We could substitute the default image of the MessageDialog with another image using

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
    

where Gtk.STOCK_CAPS_LOCK_WARNING is any image from <link href="http://developer.gnome.org/gtk3/unstable/gtk3-Stock-Items.html">Stock Items</link>. We could also set any image as in the Image widget, as image.set_from_file("filename.png").

</item>
Packit 1470ea
    <item>

format_secondary_text("some secondary message") sets a secondary message. The primary text becomes bold.

</item>
Packit 1470ea
  </list>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
  <title>API References</title>
Packit 1470ea
  

Packit 1470ea
    In this sample we used the following:
Packit 1470ea
  

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>