Software Design Lecture Notes Fall 2004 For Thursday you should: 1) finish your project proposal ok if it is preliminary, but turn it in on time! 2) prepare for a quiz Exam 2 will be Thursday 28 October Leftover: Homework 7 suggestions A quick intro to GUI programming -------------------------------- Try this: wget http://wb/sd/code/HelloGui.py python HelloGui.py from World import * class Hello(Gui): def __init__(self): Gui.__init__(self) self.ca_width = 400 self.ca_height = 400 self.transforms = [ CanvasTransform(self.ca_width, self.ca_height) ] def setup(self): # setup creates the GUI elements (called widgets) # left frame self.fr(LEFT) self.canvas = self.ca(width=self.ca_width, height=self.ca_height, bg='white') self.endfr() # right frame self.fr(LEFT, fill=BOTH, expand=1) self.fr() self.bu(LEFT, text='Hello', command=self.hello) self.bu(LEFT, text='Quit', command=self.quit) self.endfr() self.endfr() def hello(self): self.create_text([0, 0], 'Hello') if __name__ == '__main__': # create the GUI h = Hello() h.setup() # wait for user events h.mainloop() Key ideas in HelloGui: 1) event-driven programming create a GUI, then enter an infinite loop, waiting for user events and then processing them 2) GUIs are made up of a hierarchy of elements called WIDGETS The widgets in this example include Frame: container that holds other widgets Button: widget that invokes a command when pressed Canvas: big blank widget for drawing graphics 3) I have written methods that are wrappers on tkinter methods. The following are Gui methods inherited by HelloGui fr returns a Frame object bu returns a Button object ca returns a Canvas object ... Documentation of the tkinter objects is available from http://www.python.org/topics/tkinter/ But to understand widget options, you may want to look at the Tcl/Tk documentation at http://dev.scriptics.com/man/tcl8.5/TkCmd/contents.htm You should also look at the examples I have provided in World.py and Gui.py. 4) Next time... callbacks and event bindings!