Part 3b

Menu and Status Bar

The Python

Lets examine the code that does the status bar. First in the init function we need to get a context id to use with our push and pop functions. The count variable is initialized in the init function as we only want to assign 0 to it once.

Init Function
self.context_id = self.statusbar.get_context_id("status")
self.status_count = 0

When the View > Push Status> button is pressed the 'on_push_status_activate function will push a message to the status bar and increment the count of how many has been pushed. The first item in the push function is the context ID and the second is the text we want to push.

Push Status Function
def on_push_status_activate(self, menuitem, data=None):
  self.status_count += 1
  self.statusbar.push(self.context_id, "Message number %s" % str(self.status_count))

When the View > Pop Status> button is pressed the 'on_pop_status_activate function will pop the top most message from the status bar. We use the pop function with the context ID to pop an item from the list. Each time we pop a message from the status bar it is removed from the stack, the count is decremented and the next message below the one popped is displayed. While this is cool it is not practical if you need to clear a bunch of messages.

Pop Status Function
def on_pop_status_activate(self, menuitem, data=None):
  self.status_count -= 1
  self.statusbar.pop(self.context_id)

When the View > Clear Status button is pressed the on_clear_status_activate function will clear all the status messages from the status bar. This function uses a while loop to pop each message and decrement the counter until the counter is not greater than 0.

Clear Status Function
  def on_clear_status_activate(self, menuitem, data=None):
    while (self.status_count > 0):
      self.statusbar.pop(self.context_id)
      self.status_count -= 1

The following code is the complete python file. In the same directory as the glade3.glade file was saved save the following python code as glade3.py.

glade3.py
#!/usr/bin/env python

try:
  import pygtk
  pygtk.require('2.0')
except:
  pass
try:
  import gtk
  import gtk.glade
except:
  print('GTK not available')
  sys.exit(1)

class Buglump:

  def __init__(self):
    self.gladefile = "glade3.glade"
    self.builder = gtk.Builder()
    self.builder.add_from_file(self.gladefile)
    self.builder.connect_signals(self)
    self.window = self.builder.get_object("window1")
    self.aboutdialog = self.builder.get_object("aboutdialog")
    self.statusbar = self.builder.get_object("statusbar1")
    self.context_id = self.statusbar.get_context_id("status")
    self.window.show()
    self.status_count = 0

  def on_window1_destroy(self, object, data=None):
    print "quit with cancel"
    gtk.main_quit()

  def on_gtk_quit_activate(self, menuitem, data=None):
    print "quit from menu"
    gtk.main_quit()

  def on_push_status_activate(self, menuitem, data=None):
    self.status_count += 1
    self.statusbar.push(self.context_id, "Message number %s" % str(self.status_count))

  def on_pop_status_activate(self, menuitem, data=None):
    self.status_count -= 1
    self.statusbar.pop(self.context_id)

  def on_clear_status_activate(self, menuitem, data=None):
    while (self.status_count > 0):
      self.statusbar.pop(self.context_id)
      self.status_count -= 1

  def on_gtk_about_activate(self, menuitem, data=None):
    print "help about selected"
    self.response = self.aboutdialog.run()
    self.aboutdialog.hide()

if __name__ == "__main__":
  main = Buglump()
  gtk.main()

Now open up a terminal and cd to the directory where your files are. Type in chmod +x glade3.py to make our file executable.

Now type ./glade3.py and our screen pops up… now click on the View menu and our new submenu items show up.

images/glade-03-07.png

Now when we click on Push Status the status bar is updated with our status message.

images/glade-03-08.png

Now click on View > Pop Status and the current message is popped from the stack. If there is a message below that one it is displayed.

images/glade-03-09.png

Each time you push a message to the status bar the counter is incremented and each time you pop a message to the status bar the counter is decremented. This keeps the counter in sync. So push a few to the status bar then pop them off to see how this works.

I hope programming in Glade3 and Python is starting to click for you now. More to come…

That’s all for this one and we still didn’t even say Hello World