Package VisionEgg :: Module GUI
[frames] | no frames]

Source Code for Module VisionEgg.GUI

  1  # The Vision Egg: GUI 
  2  # 
  3  # Copyright (C) 2001-2003 Andrew Straw. 
  4  # Copyright (C) 2008 California Institute of Technology 
  5  # 
  6  # URL: <http://www.visionegg.org/> 
  7  # 
  8  # Distributed under the terms of the GNU Lesser General Public License 
  9  # (LGPL). See LICENSE.TXT that came with this file. 
 10   
 11  """ 
 12  Graphical user interface classes and functions. 
 13   
 14  """ 
 15   
 16  import VisionEgg 
 17   
 18  #################################################################### 
 19  # 
 20  #        Import all the necessary packages 
 21  # 
 22  #################################################################### 
 23   
 24   
 25  import logging                              # available in Python 2.3 
 26   
 27  import VisionEgg 
 28  import os 
 29  import sys 
 30   
31 -class _delay_import_error:
32 """Defer import errors until they cause problems."""
33 - def __init__(self,orig_traceback):
34 self.orig_traceback = orig_traceback
35 - def __getattr__(self,name):
36 raise self.orig_traceback # ImportError deferred from earlier failure
37 38 try: 39 import Tkinter 40 except ImportError, x: # don't fail on this until it becomes a problem... 41 Tkinter = _delay_import_error(x) 42 43 try: 44 import tkMessageBox 45 except ImportError, x: # don't fail on this until it becomes a problem... 46 tkMessageBox = _delay_import_error(x) 47 48 try: 49 import tkFileDialog 50 except ImportError, x: # don't fail on this until it becomes a problem... 51 tkFileDialog = _delay_import_error(x) 52
53 -def showexception(exc_type, exc_value, traceback_str):
54 # private subclass of Tkinter.Frame 55 class ShowExceptionFrame(Tkinter.Frame): 56 """A window that shows a string and has a quit button.""" 57 def __init__(self,master,exc_type, exc_value, traceback_str): 58 VisionEgg.config._Tkinter_used = True 59 Tkinter.Frame.__init__(self,master,borderwidth=20) 60 title="Vision Egg: exception caught" 61 first_str = "An unhandled exception was caught." 62 type_value_str = "%s: %s"%(str(exc_type),str(exc_value)) 63 64 frame = self 65 66 top = frame.winfo_toplevel() 67 top.title(title) 68 top.protocol("WM_DELETE_WINDOW",self.close_window) 69 70 Tkinter.Label(frame,text=first_str).pack() 71 Tkinter.Label(frame,text=type_value_str).pack() 72 if traceback_str: 73 Tkinter.Label(frame,text="Traceback (most recent call last):").pack() 74 Tkinter.Label(frame,text=traceback_str).pack() 75 76 b = Tkinter.Button(frame,text="OK",command=self.close_window) 77 b.pack() 78 b.focus_set() 79 b.grab_set() 80 b.bind('<Return>',self.close_window)
81 82 def close_window(self,dummy_arg=None): 83 self.quit() 84 # create instance of class 85 parent = Tkinter._default_root 86 if parent: 87 top = Tkinter.Toplevel(parent) 88 top.transient(parent) 89 else: 90 top = None 91 f = ShowExceptionFrame(top, exc_type, exc_value, traceback_str) 92 f.pack() 93 f.mainloop() 94 f.winfo_toplevel().destroy() 95
96 -class AppWindow(Tkinter.Frame):
97 """A GUI Window that can be subclassed for a main application window"""
98 - def __init__(self,master=None,idle_func=lambda: None,**cnf):
99 VisionEgg.config._Tkinter_used = True 100 Tkinter.Frame.__init__(self,master,**cnf) 101 self.winfo_toplevel().title('Vision Egg') 102 103 self.info_frame = InfoFrame(self) 104 self.info_frame.pack() 105 106 self.idle_func = idle_func 107 self.after(1,self.idle) # register idle function with Tkinter
108
109 - def idle(self):
110 self.idle_func() 111 self.after(1,self.idle) # (re)register idle function with Tkinter
112
113 -class ProgressBar(Tkinter.Frame):
114 - def __init__(self, master=None, orientation="horizontal", 115 min=0, max=100, width=100, height=18, 116 doLabel=1, fillColor="LightSteelBlue1", background="gray", 117 labelColor="black", labelFont="Helvetica", 118 labelText="", labelFormat="%d%%", 119 value=50, **cnf):
120 Tkinter.Frame.__init__(self,master) 121 # preserve various values 122 self.master=master 123 self.orientation=orientation 124 self.min=min 125 self.max=max 126 self.width=width 127 self.height=height 128 self.doLabel=doLabel 129 self.fillColor=fillColor 130 self.labelFont= labelFont 131 self.labelColor=labelColor 132 self.background=background 133 self.labelText=labelText 134 self.labelFormat=labelFormat 135 self.value=value 136 self.canvas=Tkinter.Canvas(self, height=height, width=width, bd=0, 137 highlightthickness=0, background=background) 138 self.scale=self.canvas.create_rectangle(0, 0, width, height, 139 fill=fillColor) 140 self.label=self.canvas.create_text(self.canvas.winfo_reqwidth() / 2, 141 height / 2, text=labelText, 142 anchor="c", fill=labelColor, 143 font=self.labelFont) 144 self.update() 145 self.canvas.pack(side='top', fill='x', expand='no')
146
147 - def updateProgress(self, newValue, newMax=None):
148 if newMax: 149 self.max = newMax 150 self.value = newValue 151 self.update()
152
153 - def update(self):
154 # Trim the values to be between min and max 155 value=self.value 156 if value > self.max: 157 value = self.max 158 if value < self.min: 159 value = self.min 160 # Adjust the rectangle 161 if self.orientation == "horizontal": 162 self.canvas.coords(self.scale, 0, 0, 163 float(value) / self.max * self.width, self.height) 164 else: 165 self.canvas.coords(self.scale, 0, 166 self.height - (float(value) / 167 self.max*self.height), 168 self.width, self.height) 169 # Now update the colors 170 self.canvas.itemconfig(self.scale, fill=self.fillColor) 171 self.canvas.itemconfig(self.label, fill=self.labelColor) 172 # And update the label 173 if self.doLabel: 174 if value: 175 if value >= 0: 176 pvalue = int((float(value) / float(self.max)) * 177 100.0) 178 else: 179 pvalue = 0 180 self.canvas.itemconfig(self.label, text=self.labelFormat 181 % pvalue) 182 else: 183 self.canvas.itemconfig(self.label, text='') 184 else: 185 self.canvas.itemconfig(self.label, text=self.labelFormat % 186 self.labelText) 187 self.canvas.update_idletasks()
188
189 -class GraphicsConfigurationWindow(Tkinter.Frame):
190 """Graphics Configuration Window"""
191 - def __init__(self,master=None,**cnf):
192 VisionEgg.config._Tkinter_used = True 193 Tkinter.Frame.__init__(self,master,**cnf) 194 self.winfo_toplevel().title('Vision Egg - Graphics configuration') 195 self.pack() 196 197 self.clicked_ok = 0 # So we can distinguish between clicking OK and closing the window 198 199 row = 0 200 Tkinter.Label(self, 201 text="Vision Egg - Graphics configuration", 202 font=("Helvetica",14,"bold")).grid(row=row,columnspan=2) 203 row += 1 204 205 ################## begin topframe ############################## 206 207 topframe = Tkinter.Frame(self) 208 topframe.grid(row=row,column=0,columnspan=2) 209 topframe_row = 0 210 211 Tkinter.Label(topframe, 212 text=self.format_string("The default value for these variables and the presence of this dialog window can be controlled via the Vision Egg config file. If this file exists in the Vision Egg user directory, that file is used. Otherwise, the configuration file found in the Vision Egg system directory is used."), 213 ).grid(row=topframe_row,column=1,columnspan=2,sticky=Tkinter.W) 214 topframe_row += 1 215 216 try: 217 import _imaging, _imagingtk 218 import ImageFile, ImageFileIO, BmpImagePlugin, JpegImagePlugin 219 import Image,ImageTk 220 im = Image.open(os.path.join(VisionEgg.config.VISIONEGG_SYSTEM_DIR,'data','visionegg.bmp')) 221 self.tk_im=ImageTk.PhotoImage(im) 222 Tkinter.Label(topframe,image=self.tk_im).grid(row=0,rowspan=topframe_row,column=0) 223 except Exception,x: 224 logger = logging.getLogger('VisionEgg.GUI') 225 logger.info("No Vision Egg logo :( because of error while " 226 "trying to display image in " 227 "GUI.GraphicsConfigurationWindow: %s: " 228 "%s"%(str(x.__class__),str(x))) 229 230 ################## end topframe ############################## 231 232 row += 1 233 234 ################## begin file_frame ############################## 235 236 file_frame = Tkinter.Frame(self) 237 file_frame.grid(row=row,columnspan=2,sticky=Tkinter.W+Tkinter.E,pady=5) 238 239 # Script name and location 240 file_row = 0 241 Tkinter.Label(file_frame, 242 text="This script:").grid(row=file_row,column=0,sticky=Tkinter.E) 243 Tkinter.Label(file_frame, 244 text="%s"%(os.path.abspath(sys.argv[0]),)).grid(row=file_row,column=1,sticky=Tkinter.W) 245 file_row += 1 246 # Vision Egg system dir 247 Tkinter.Label(file_frame, 248 text="Vision Egg system directory:").grid(row=file_row,column=0,sticky=Tkinter.E) 249