#!/usr/bin/python ''' gui-sample.py Jeff Ondich, 28 Feb 2010 This is a sample of a few important Tkinter concepts. -- Creating a root frame window to house the program. -- Putting a Frame in the root window, and sub-Frames in that. -- A few simple controls: a Button, some Label objects, and some Entry objects. -- A menu bar, including one menu item with an accelerator (i.e. a keyboard equivalent). -- A Canvas widget for drawing lines, circles, rectangles, etc. For some pretty good documentation of Tkinter, see http://effbot.org/tkinterbook/. ''' import sys from Tkinter import * class MainFrame(Frame): def __init__(self, parent=None): # Call the constructor for the superclass. Frame.__init__(self, parent) # Fit this frame into the only row and column in the root window. self.grid(row=0, column=0, sticky=N+E+S+W) # This frame consists of one row and two columns. The # right-hand column is twice as wide as the left. self.rowconfigure(0, weight=1) self.columnconfigure(0, weight=1) self.columnconfigure(1, weight=2) # Instantiate a frame for each of the columns, one # to hold the controls and the other to hold a Canvas. self.controlPane = ControlPane(self) self.canvasPane = CanvasPane(self) # self.handleButtonClick() #self.bind_all('', self.addTransaction) #self.bind_all('', self.showPitchers) #self.bind_all('', self.startNewPlayer) #self.bind_all('', self.previousTeam) #self.bind_all('', self.nextTeam) def handleButtonClick(self): formData = self.controlPane.getFormData() self.canvasPane.setColor(formData['red'], formData['green'], formData['blue']) def open(self): sys.stderr.write('open command\n') class ControlPane(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.config(bd=2, relief=SUNKEN) self.grid(row=0, column=0, sticky=N+E+S+W) self.columnconfigure(0, pad=6, weight=1) self.columnconfigure(1, pad=6, weight=3) self.columnconfigure(2, pad=6, weight=1) self.rowconfigure(0, pad=6, weight=1) self.rowconfigure(1, pad=6, weight=1) self.rowconfigure(2, pad=6, weight=1) self.rowconfigure(3, pad=6, weight=1) Label(self, text='Red:').grid(row=0, column=0, sticky=W) self.redEntry = Entry(self) self.redEntry.insert(0, '0') self.redEntry.grid(row=0, column=1, sticky=E+W) Label(self, text='Green:').grid(row=1, column=0, sticky=W) self.greenEntry = Entry(self) self.greenEntry.insert(0, '0') self.greenEntry.grid(row=1, column=1, sticky=E+W) Label(self, text='Blue:').grid(row=2, column=0, sticky=W) self.blueEntry = Entry(self) self.blueEntry.insert(0, '0') self.blueEntry.grid(row=2, column=1, sticky=E+W) self.button = Button(self, command=self.parent.handleButtonClick, text='Go') self.button.grid(row=3, column=6, sticky=W) def getFormData(self): '''Returns a dictionary containing the data entered in the control pane's form.''' formData = {} formData['red'] = 0 s = self.redEntry.get() if s.isdigit() and (0 <= int(s) <= 255): formData['red'] = int(s) formData['green'] = 0 s = self.greenEntry.get() if s.isdigit() and (0 <= int(s) <= 255): formData['green'] = int(s) formData['blue'] = 0 s = self.blueEntry.get() if s.isdigit() and (0 <= int(s) <= 255): formData['blue'] = int(s) return formData class CanvasPane(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.config(bd=2, relief=SUNKEN) self.grid(row=0, column=1, sticky=N+E+S+W) self.columnconfigure(0, pad=6, weight=1) self.rowconfigure(0, pad=6, weight=1) self.canvas = Canvas(self, borderwidth=2) self.canvas.pack() self.rectangleId = self.canvas.create_rectangle(100,100,150,150,fill='black') def setColor(self, red, green, blue): colorString = '#%02x%02x%02x' % (red, green, blue) self.canvas.itemconfig(self.rectangleId, fill=colorString) def quit(*args): '''This function is here to give us an exit function with a signature suitable for use in a "bind" call (see below where we bind Command-x to quit).''' exit() if __name__ == '__main__': # Set up the root frame window. root = Tk() root.title('Tkinter Sample') root.rowconfigure(0, weight=1) root.columnconfigure(0, weight=1) mainFrame = MainFrame(root) # Set up the menu bar with a single File menu. menuBar = Menu(root) root.config(menu=menuBar) fileMenu = Menu(menuBar, tearoff=0) fileMenu.add_command(label='Open', command=mainFrame.open) fileMenu.add_separator() fileMenu.add_command(label='Exit', command=quit, accelerator='Command-x') root.bind('', quit) menuBar.add_cascade(label='File', menu=fileMenu) # Let 'er rip. root.mainloop()