Package teamwork :: Package widgets :: Module WizardShell
[hide private]
[frames] | no frames]

Source Code for Module teamwork.widgets.WizardShell

  1  #! /usr/env/python 
  2   
  3  """ 
  4  WizardShell provides a GUI wizard framework. 
  5   
  6  WizardShell was derived from AppShell which was itself 
  7  derived from GuiAppD.py, originally created by 
  8  Doug Hellmann (doughellmann@mindspring.com). 
  9   
 10  """ 
 11   
 12  from Tkinter import * 
 13  import Pmw 
 14  import sys, string 
 15  # Added by pynadath 
 16  from types import * 
 17   
18 -class WizardShell(Pmw.MegaWidget):
19 wizversion = '1.0' 20 21 frameWidth = 550 22 frameHeight = 357 23 padx = 5 24 pady = 5 25 panes = 4 26 27 busyCursor = 'watch' 28
29 - def __init__(self, parent=None, **kw):
30 self.__illustration = None 31 self.__wizimage = None 32 optiondefs = ( 33 ('image', 'wizard.gif', self.changePicture), 34 ('name', 'Generic Wizard Frame', Pmw.INITOPT), 35 ('framewidth', 1, Pmw.INITOPT), 36 ('frameheight', 1, Pmw.INITOPT)) 37 self.defineoptions(kw, optiondefs) 38 if parent: 39 self.root = Toplevel(parent) 40 else: 41 self.root = Tk() 42 Pmw.initialise(self.root) 43 44 ## self.initializeTk(self.root) 45 self.root.title(self['name']) 46 self.root.geometry('%dx%d' % (self.frameWidth, self.frameHeight)) 47 48 # Initialize the base class 49 Pmw.MegaWidget.__init__(self, parent=self.root) 50 51 # initialize the wizard 52 self.wizardInit() 53 54 # setup panes 55 self.pCurrent = 0 56 self.pFrame = [None] * self.panes 57 58 # create the interface 59 self.__createInterface() 60 self.changePicture() 61 62 # create a table to hold the cursors for 63 # widgets which get changed when we go busy 64 self.preBusyCursors = None 65 66 # pack the container and set focus 67 # to ourselves 68 self._hull.pack(side=TOP, fill=BOTH, expand=YES) 69 self.focus_set() 70 71 # initialize our options 72 self.initialiseoptions(WizardShell)
73
74 - def wizardInit(self):
75 # Called before interface is created (should be overridden). 76 pass
77
78 - def initializeTk(self, root):
79 # Initialize platform-specific options 80 if sys.platform == 'mac': 81 self.__initializeTk_mac(root) 82 elif sys.platform == 'win32': 83 self.__initializeTk_win32(root) 84 else: 85 self.__initializeTk_unix(root)
86
87 - def __initializeTk_colors_common(self, root):
88 root.option_add('*background', 'grey') 89 root.option_add('*foreground', 'black') 90 root.option_add('*EntryField.Entry.background', 'white') 91 root.option_add('*Entry.background', 'white') 92 root.option_add('*MessageBar.Entry.background', 'gray85') 93 root.option_add('*Listbox*background', 'white') 94 root.option_add('*Listbox*selectBackground', 'dark slate blue') 95 root.option_add('*Listbox*selectForeground', 'white')
96
97 - def __initializeTk_win32(self, root):
98 self.__initializeTk_colors_common(root) 99 root.option_add('*Font', 'Verdana 10 bold') 100 root.option_add('*EntryField.Entry.Font', 'Courier 10') 101 root.option_add('*Listbox*Font', 'Courier 10')
102
103 - def __initializeTk_mac(self, root):
104 self.__initializeTk_colors_common(root)
105
106 - def __initializeTk_unix(self, root):
107 self.__initializeTk_colors_common(root)
108
109 - def busyStart(self, newcursor=None):
110 if not newcursor: 111 newcursor = self.busyCursor 112 newPreBusyCursors = {} 113 for component in self.busyWidgets: 114 newPreBusyCursors[component] = component['cursor'] 115 component.configure(cursor=newcursor) 116 component.update_idletasks() 117 self.preBusyCursors = (newPreBusyCursors, self.preBusyCursors)
118
119 - def busyEnd(self):
120 if not self.preBusyCursors: 121 return 122 oldPreBusyCursors = self.preBusyCursors[0] 123 self.preBusyCursors = self.preBusyCursors[1] 124 for component in self.busyWidgets: 125 try: 126 component.configure(cursor=oldPreBusyCursors[component]) 127 except KeyError: 128 pass 129 component.update_idletasks()
130
131 - def __createWizardArea(self):
132 # Create data area where data entry widgets are placed. 133 self.__illustration = self.createcomponent('illust', 134 (), None, 135 Label, 136 (self._hull,)) 137 138 self.__illustration.grid(row=0,column=0,sticky='ns') 139 self._hull.columnconfigure(0,weight=0) 140 141 self.__dataArea = self.createcomponent('dataarea', 142 (), None, 143 Frame, 144 (self._hull,), 145 relief=FLAT, bd=1) 146 147 self.__dataArea.grid(row=0,column=1,sticky='ewns') 148 self._hull.columnconfigure(1,weight=1) 149 self._hull.rowconfigure(0,weight=1)
150
151 - def __createSeparator(self):
152 self.__separator = self.createcomponent('separator', 153 (), None, 154 Frame, 155 (self._hull,), 156 relief=SUNKEN, 157 bd=2, height=2) 158 self.__separator.grid(row=1,column=0,columnspan=2,sticky='ew') 159 self._hull.rowconfigure(1,minsize=5)
160
161 - def __createCommandArea(self):
162 # Create a command area for application-wide buttons. 163 self.__commandFrame = self.createcomponent('commandframe', 164 (), None, 165 Frame, 166 (self._hull,), 167 relief=FLAT, bd=1) 168 self.__commandFrame.grid(row=2,column=0,columnspan=2,sticky='ew') 169 self._hull.rowconfigure(2,minsize=20)
170
171 - def interior(self):
172 # Retrieve the interior site where widgets should go. 173 return self.__dataArea
174
175 - def changePicture(self):
176 if self.__illustration: 177 if self.__wizimage: del self.__wizimage 178 # Modified by pynadath (support image, as well as file name) 179 if type(self['image']) is StringType: 180 try: 181 self.__wizimage = PhotoImage(file=self['image']) 182 except: 183 self.__wizimage = None 184 else: 185 self.__wizimage = self['image'] 186 self.__illustration['image'] = self.__wizimage
187
188 - def buttonAdd(self, buttonName, command=None, state=1):
189 # Add a button to the control area. 190 frame = Frame(self.__commandFrame) 191 newBtn = Button(frame, text=buttonName, command=command) 192 newBtn.pack() 193 newBtn['state'] = [DISABLED,NORMAL][state] 194 frame.pack(side=RIGHT) 195 return newBtn
196
197 - def __createPanes(self):
198 for i in range(self.panes): 199 self.pFrame[i] = self.createcomponent('pframe'+str(i), 200 (), None, 201 Frame, 202 (self.interior(),), 203 relief=FLAT, bd=1) 204 if not i == self.pCurrent: 205 self.pFrame[i].forget() 206 else: 207 self.pFrame[i].pack(fill=BOTH, expand=YES)
208
209 - def pInterior(self, idx):
210 return self.pFrame[idx]
211
212 - def next(self):
213 cpane = self.pCurrent 214 self.pCurrent = self.pCurrent + 1 215 self.prevB['state'] = NORMAL 216 if self.pCurrent == self.panes - 1: 217 self.nextB['text'] = 'Finish' 218 self.nextB['command'] = self.done 219 self.pFrame[cpane].forget() 220 self.pFrame[self.pCurrent].pack(fill=BOTH, expand=YES)
221
222 - def prev(self):
223 cpane = self.pCurrent 224 self.pCurrent = self.pCurrent - 1 225 if self.pCurrent <= 0: 226 self.pCurrent = 0 227 self.prevB['state'] = DISABLED 228 if cpane == self.panes - 1: 229 self.nextB['text'] = 'Next' 230 self.nextB['command'] = self.next 231 self.pFrame[cpane].forget() 232 self.pFrame[self.pCurrent].pack(fill=BOTH, expand=YES)
233
234 - def done(self):
235 #to be Overridden 236 pass
237
238 - def __createInterface(self):
239 self.__createWizardArea() 240 self.__createSeparator() 241 self.__createCommandArea() 242 self.__createPanes() 243 # 244 # Create the parts of the interface 245 # which can be modified by subclasses 246 # 247 self.busyWidgets = ( self.root, ) 248 self.createInterface()
249
250 - def createInterface(self):
251 # Override this method to create the interface for the wiz. 252 pass
253
254 - def main(self):
255 # This method should be left intact! 256 self.pack() 257 try: 258 self.mainloop() 259 except KeyboardInterrupt: 260 self.done()
261
262 - def run(self):
263 self.main()
264
265 -class TestWizardShell(WizardShell):
266
267 - def createButtons(self):
268 self.buttonAdd('Cancel', command=self.quit, state=1) 269 self.nextB = self.buttonAdd('Next', command=self.next, state=1) 270 self.nextB.configure(default=ACTIVE) 271 self.prevB = self.buttonAdd('Prev', command=self.prev, state=0)
272
273 - def createMain(self):
274 self.w1 = self.createcomponent('w1', (), None, 275 Label, 276 (self.pInterior(0),), 277 text='Wizard Area 1') 278 self.w1.pack() 279 self.w2 = self.createcomponent('w2', (), None, 280 Label, 281 (self.pInterior(1),), 282 text='Wizard Area 2') 283 self.w2.pack()
284
285 - def createInterface(self):
286 WizardShell.createInterface(self) 287 self.createButtons() 288 self.createMain()
289
290 - def done(self):
291 print 'All Done'
292 293 if __name__ == '__main__': 294 test = TestWizardShell(image='Icons/faces.gif') 295 test.run() 296