I l@ve RuBoard Previous Section Next Section

9.10 Building GUI Solutions Independent of the Specific GUI Toolkit

Credit: Magnus Lie Hetland, Alex Martelli

9.10.1 Problem

You want to write a simple GUI that can be deployed on just about any toolkit the end user prefers, including Tkinter, wxPython, Swing with Jython, curses-based text I/O, and many others.

9.10.2 Solution

The anygui package lets you do this. For example, here's an anygui-based GUI implementation of chmod:

import sys, os
from anygui import *

filename = sys.argv[1]  # file whose permissions we study and modify

# main-level stuff
app = Application(  )
win = Window(title='chmod '+filename, size=(280,175))

# headers for CheckBoxes and Labels
types = 'Read Write Execute'.split(  )
people = 'User Group Others'.split(  )

# Create and place CheckBoxes and Labels
cbx = {}
x, y = 10, 0
for p in people:
    lbl = Label(text=p)
    lbl.geometry = x, y+10, 80, 15
    win.add(lbl)
    cbx[p] = {}
    for t in types:
        y += 35
        cbx[p][t] = CheckBox(text=t)
        cbx[p][t].geometry = x, y, 80, 25
        win.add(cbx[p][t])
    x += 90; y = 0

# Set the CheckBoxes' values
def refresh(  ):
    mode, mask = os.stat(filename)[0], 256
    for p in people:
        for t in types:
            cbx[p][t].on = mode & mask
            mask = mask >> 1

# initial setting of checkbox values
refresh(  )

# callbacks for button clicks
def chmod(  ):
    mode, mask = 0, 256
    for p in people:
        for t in types:
            if cbx[p][t].on:
                mode = mode | mask
            mask = mask >> 1
    os.chmod(filename, mode)
    # reset checkbox values
    refresh(  )

def chmod_and_exit(  ):
    chmod(  )
    sys.exit(  )

# Make and add the buttons
opt = Options(y=140, width=80, height=25)
apply = Button(opt, x=10, text='Apply', action=chmod)
cancel = Button(opt, x=100, text='Cancel', action=sys.exit)
ok = Button(opt, x=190, text='OK', action=chmod_and_exit)
win.add(apply, cancel, ok)

# and finally...let 'er rip!
app.run(  )

9.10.3 Discussion

Don't you like how the anydbm standard module lets you access any of several different DBM implementations? Or how xml.sax lets you access any of several XML parsers? Welcome to anygui, a new project designed to be a similar solution for simple GUIs, especially GUI applications that need to be deployable in a wide variety of settings. anygui is absolutely not meant to replace any of the many, wonderful GUI toolkits Python is blessed with, any more than anydbm was ever intended to replace dbm, ndbm, and so on. Rather, anygui is implemented as a frontend that sits on top of any of several backends (which in turn are coded in terms of Tkinter, wxPython, Swing for Jython, and so on) and provides a uniform application-programming interface to a reasonable subset of the toolkits' power. There's even a curses-based, text-oriented GUI simulation backend for emergency cases in which you cannot run a real GUI but still want to deploy an anygui-based application.

At the time of writing, anygui is in early beta stage; you can download it and play with it (with several backends more or less in a running state), but it's not yet stable and solid enough for production work. However, things often change quickly in open source projects with many enthusiastic contributors. You should visit http://www.anygui.org/, download the latest release of anygui and your favorite backends, and see if it is already what you are looking for.

The example in this recipe uses functionality that is small and basic enough to keep running on whatever level of anygui is available at the time you read this, although I tested it only with the newest release at the time of this writing (fresh from the CSV repository) and several backends. Although I suspect a GUI-based chmod is hardly the killer application for anygui, it might prove to be useful for you.

9.10.4 See Also

anygui is available and described at http://www.anygui.org/.

    I l@ve RuBoard Previous Section Next Section