Blame platform-demos/ca/statusbar.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="statusbar.py" xml:lang="ca">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">Statusbar (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#display-widgets"/>
Packit 1470ea
    <link type="seealso" xref="signals-callbacks.py"/>
Packit 1470ea
    <link type="next" xref="spinner.py"/>    
Packit 1470ea
    <revision version="0.2" date="2012-06-12" 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>Report messages of minor importance to the user</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>Statusbar</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/statusbar.png"/>
Packit 1470ea
  

This statusbar tells you if you click the button or if you press any key (and which key).

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 Gdk
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
    # a window
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="StatusBar Example", application=app)
Packit 1470ea
        self.set_default_size(200, 100)
Packit 1470ea
Packit 1470ea
        # a label
Packit 1470ea
        label = Gtk.Label(label="Press any key or ")
Packit 1470ea
Packit 1470ea
        # a button
Packit 1470ea
        button = Gtk.Button(label="click me.")
Packit 1470ea
        # connected to a callback
Packit 1470ea
        button.connect("clicked", self.button_clicked_cb)
Packit 1470ea
Packit 1470ea
        # the statusbar
Packit 1470ea
        self.statusbar = Gtk.Statusbar()
Packit 1470ea
        # its context_id - not shown in the UI but needed to uniquely identify
Packit 1470ea
        # the source of a message
Packit 1470ea
        self.context_id = self.statusbar.get_context_id("example")
Packit 1470ea
        # we push a message onto the statusbar's stack
Packit 1470ea
        self.statusbar.push(
Packit 1470ea
            self.context_id, "Waiting for you to do something...")
Packit 1470ea
Packit 1470ea
        # a grid to attach the widgets
Packit 1470ea
        grid = Gtk.Grid()
Packit 1470ea
        grid.set_column_spacing(5)
Packit 1470ea
        grid.set_column_homogeneous(True)
Packit 1470ea
        grid.set_row_homogeneous(True)
Packit 1470ea
        grid.attach(label, 0, 0, 1, 1)
Packit 1470ea
        grid.attach_next_to(button, label, Gtk.PositionType.RIGHT, 1, 1)
Packit 1470ea
        grid.attach(self.statusbar, 0, 1, 2, 1)
Packit 1470ea
Packit 1470ea
        # add the grid to the window
Packit 1470ea
        self.add(grid)
Packit 1470ea
Packit 1470ea
    # callback function for the button clicked
Packit 1470ea
    # if the button is clicked the event is signaled to the statusbar
Packit 1470ea
    # onto which we push a new status
Packit 1470ea
    def button_clicked_cb(self, button):
Packit 1470ea
        self.statusbar.push(self.context_id, "You clicked the button.")
Packit 1470ea
Packit 1470ea
    # event handler
Packit 1470ea
    def do_key_press_event(self, event):
Packit 1470ea
    # any signal from the keyboard is signaled to the statusbar
Packit 1470ea
    # onto which we push a new status with the symbolic name
Packit 1470ea
    # of the key pressed
Packit 1470ea
        self.statusbar.push(self.context_id, Gdk.keyval_name(event.keyval) +
Packit 1470ea
                            " key was pressed.")
Packit 1470ea
        # stop the signal emission
Packit 1470ea
        return True
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 = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)
Packit 1470ea
Packit 1470ea
Packit 1470ea
  <note>

Packit 1470ea
    Gdk.keyval_name(event.keyval) converts the key value event.keyval into a symbolic name. The names and corresponding key values can be found <link href="http://git.gnome.org/browse/gtk+/tree/gdk/gdkkeysyms.h">here</link>, but for instance GDK_KEY_BackSpace becomes the string "BackSpace".
Packit 1470ea
  

</note>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="methods">
Packit 1470ea
  <title>Useful methods for a Statusbar widget</title>
Packit 1470ea
    

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

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

pop(context_id) removes the first message in the statusbar stack with the given context_id.

</item>
Packit 1470ea
    <item>

remove_all(context_id) removes all the messages in the statusbar stack with the given context_id.

</item>
Packit 1470ea
    <item>

remove(context_id, message_id) removes the message with the given message_id in the statusbar stack with the given context_id. The message_id is returned by push(context_id, "the message") when pushing the message on the statusbar.

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

In this sample we used the following:

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

<link href="http://developer.gnome.org/gtk3/unstable/GtkStatusbar.html">GtkStatusbar</link>

</item>
Packit 1470ea
    <item>

<link href="http://developer.gnome.org/gdk/stable/gdk-Keyboard-Handling.html">Gdk - Key Values</link>

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