Pete Ryland

 Home  Projects  Words  Photos  Information  Themes  News  Links  Blog

Pete Ryland's Web Log

I didn't want a blog but they made me do it.

Wed, 11 Oct 2006

EGG! Prototype now available [] (22:46)

As promised, I have started on a higher-level pythonic API for gtk/gnome, built on top of pygtk/pygnome. As I'm too busy right now to package it properly, the prototype is available as a single file called egg20061011.py so if you want to try it out with the following examples, please rename it egg.py.

Well, after a brief amount of hacking about, egg now supports menus, buttons, the about dialog and the start of file selection support. On to the first example:

#!/usr/bin/python

import egg

class ButtonExample(egg.App):
  """A button example for EGG."""

  @egg.button()
  def clickMe(self):
    """Prints a message to standard ouput"""
    print "You pressed my button!"

  def __init__(self):
    egg.App.__init__(self, version="1.0")
    mybutton = self.clickMe.getbutton(self)
    self.set_contents(mybutton)

if __name__ == "__main__":
  a=ButtonExample()
  a()
This example shows the general feel of what I'm aiming at. Notice a few time-saving features:
  • Default menus are provided, with File->Quit and Help->About
  • A default About dialog is provided automatically
  • A button factory (clickMe.getbutton) is created automatically by the egg.button() decorator, and connects the button to the callback
  • The callback doesn't need to receive the widget that caused it as an argument
  • The docstring for the button is used for the tooltip and the callback method's name is used for the button's label string
  • The docstring for the App is used as the AboutDialog comment, and the class's name is used for the application name by default
It's these sort of features that will help get both new and seasoned coders producing applications quickly. The plan is to enforce good GUI practice by using sensible defaults, like the following example shows:
#!/usr/bin/python

import egg

class MenuExample(egg.App):
  """A simple example of how to do menus in EGG!"""

  @egg.menuitem(accel=ord('n'))
  def newWindow(self):
    """Creates a new window"""
    print "My callback was called!"

  @egg.stockmenuitem(stock_id=egg.STOCK_OPEN)
  @egg.fileselector()
  def open(self, filename):
    print "Opening:", filename

  def __init__(self):
    egg.App.__init__(self, version="1.0",
                     menu=[["_File", self.newWindow, self.open, None, self.quit],
                           ["_Help", self.about]]
                    )

if __name__ == "__main__":
  a=MenuExample()
  a()
This shows how to set up menus with egg. Simply create the appropriate methods, decorate them with one of the menuitem decorators, then specify them in the menu parameter of the constructor to egg.App, which takes a special format. Each menu is represented as a list, with the first item being its title, and the following items being either a menuitem method, a submenu array or None for a separator. The top-level menubar is different only in that it doesn't contain a title. There are also decorators for RadioMenuItems and CheckMenuItems. Notice how two decorators have been used on the open method; one creates an intermediate callback that creates a file open dialog and then calls back the open method with the filename if the operation was not cancelled. Notice how the open menu item's label has "..." appended? Egg knows the menuitem will be opening a dialog, so does this automatically. Don't be too scared, though, you can specify a label yourself too!

>

Request served by muriel.pdr.me.uk located in London, UK.

Trademarks owned by their respective owners.

Original content Copyright (c) 1991-2005 Pete Ryland (gpg key).