1
2
3
4
5
6
7
8
9
10
11 """
12 Graphical user interface classes and functions.
13
14 """
15
16 import VisionEgg
17
18
19
20
21
22
23
24
25 import logging
26
27 import VisionEgg
28 import os
29 import sys
30
32 """Defer import errors until they cause problems."""
34 self.orig_traceback = orig_traceback
36 raise self.orig_traceback
37
38 try:
39 import Tkinter
40 except ImportError, x:
41 Tkinter = _delay_import_error(x)
42
43 try:
44 import tkMessageBox
45 except ImportError, x:
46 tkMessageBox = _delay_import_error(x)
47
48 try:
49 import tkFileDialog
50 except ImportError, x:
51 tkFileDialog = _delay_import_error(x)
52
54
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
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
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)
108
110 self.idle_func()
111 self.after(1,self.idle)
112
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
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
148 if newMax:
149 self.max = newMax
150 self.value = newValue
151 self.update()
152
154
155 value=self.value
156 if value > self.max:
157 value = self.max
158 if value < self.min:
159 value = self.min
160
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
170 self.canvas.itemconfig(self.scale, fill=self.fillColor)
171 self.canvas.itemconfig(self.label, fill=self.labelColor)
172
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
190 """Graphics Configuration Window"""
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
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
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
231
232 row += 1
233
234
235
236 file_frame = Tkinter.Frame(self)
237 file_frame.grid(row=row,columnspan=2,sticky=Tkinter.W+Tkinter.E,pady=5)
238
239
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
247 Tkinter.Label(file_frame,
248 text="Vision Egg system directory:").grid(row=file_row,column=0,sticky=Tkinter.E)
249