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 = GtkStatusbar()

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.8 illustrates the result:

Figure 9.8 Statusbar Example

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