9.8. Statusbars
Prev | Chapter 9. Miscellaneous Widgets | Next

## 9.8. Statusbars

`Statusbars` are simple widgets used to display a text message. They keep a stack of the messages pushed onto them, so that popping the current message will re-display the previous text message.

In order to allow different parts of an application to use the same statusbar to display messages, the statusbar widget issues Context Identifiers which are used to identify different "users". The message on top of the stack is the one displayed, no matter what context it is in. Messages are stacked in last-in-first-out order, not context identifier order.

A statusbar is created with a call to:

      statusbar = gtk.Statusbar()

A new Context Identifier is requested using a call to the following method with a short textual description of the context:

      context_id = statusbar.get_context_id(context_description)

There are three additional methods that operate on statusbars:

      message_id = statusbar.push(context_id, text)

      statusbar.pop(context_id)

      statusbar.remove(context_id, message_id)

The first, `push`(), is used to add a new message to the `statusbar`. It returns a `message_id`, which can be passed later to the `remove`() method to remove the message with the combination `message_id` and `context_id` from the `statusbar`'s stack.

The `pop`() method removes the message highest in the stack with the given `context_id`.

The statusbar.py example program creates a statusbar and two buttons, one for pushing items onto the statusbar, and one for popping the last item back off. Figure 9.9, “Statusbar Example” illustrates the result:

Figure 9.9. Statusbar Example

The statusbar.py source code is:

        1	#!/usr/bin/env python
        2
        3	# example statusbar.py
        4
        5	import pygtk
        6	pygtk.require('2.0')
        7	import gtk
        8
        9	class StatusbarExample:
       10	    def push_item(self, widget, data):
       11	        buff = " Item %d" % self.count
       12	        self.count = self.count + 1
       13	        self.status_bar.push(data, buff)
       14	        return
       15
       16	    def pop_item(self, widget, data):
       17	        self.status_bar.pop(data)
       18	        return
       19
       20	    def __init__(self):
       21	        self.count = 1
       22	        # create a new window
       23	        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
       24	        window.set_size_request(200, 100)
       25	        window.set_title("PyGTK Statusbar Example")
       26	        window.connect("delete_event", lambda w,e: gtk.main_quit())
       27
       28	        vbox = gtk.VBox(False, 1)
       29	        window.add(vbox)
       30	        vbox.show()
       31
       32	        self.status_bar = gtk.Statusbar()
       33	        vbox.pack_start(self.status_bar, True, True, 0)
       34	        self.status_bar.show()
       35
       36	        context_id = self.status_bar.get_context_id("Statusbar example")
       37
       38	        button = gtk.Button("push item")
       39	        button.connect("clicked", self.push_item, context_id)
       40	        vbox.pack_start(button, True, True, 2)
       41	        button.show()
       42
       43	        button = gtk.Button("pop last item")
       44	        button.connect("clicked", self.pop_item, context_id)
       45	        vbox.pack_start(button, True, True, 2)
       46	        button.show()
       47
       48	        # always display the window as the last step so it all splashes on
       49	        # the screen at once.
       50	        window.show()
       51
       52	def main():
       53	    gtk.main()
       54	    return 0
       55
       56	if __name__ == "__main__":
       57	    StatusbarExample()
       58	    main()

Prev | Up | Next
9.7. Rulers | Home | 9.9. Text Entries