blob: 6e570d783fead0840b9bd9790cc6084005ea0942 [file] [log] [blame]
Martin v. Löwis20efa682001-11-11 14:07:37 +00001# -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00002#
3# $Id$
4#
5# tixwidgets.py --
Martin v. Löwis20efa682001-11-11 14:07:37 +00006#
7# For Tix, see http://tix.sourceforge.net
8#
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00009# This is a demo program of all Tix widgets available from Python. If
10# you have installed Python & Tix properly, you can execute this as
11#
Martin v. Löwis652e1912001-11-25 14:50:56 +000012# % python tixwidgets.py
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000013#
14
Martin v. Löwis652e1912001-11-25 14:50:56 +000015import os, os.path, sys, Tix
Martin v. Löwis20efa682001-11-11 14:07:37 +000016from Tkconstants import *
17
18TCL_DONT_WAIT = 1<<1
19TCL_WINDOW_EVENTS = 1<<2
20TCL_FILE_EVENTS = 1<<3
21TCL_TIMER_EVENTS = 1<<4
22TCL_IDLE_EVENTS = 1<<5
23TCL_ALL_EVENTS = 0
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000024
25class Demo:
Martin v. Löwis20efa682001-11-11 14:07:37 +000026 def __init__(self, top):
27 self.root = top
28 self.exit = -1
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000029
Martin v. Löwis20efa682001-11-11 14:07:37 +000030 self.dir = None # script directory
31 self.balloon = None # balloon widget
32 self.useBalloons = Tix.StringVar()
33 self.useBalloons.set('0')
34 self.statusbar = None # status bar widget
35 self.welmsg = None # Msg widget
36 self.welfont = '' # font name
37 self.welsize = '' # font size
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000038
Martin v. Löwis20efa682001-11-11 14:07:37 +000039 progname = sys.argv[0]
40 dirname = os.path.dirname(progname)
41 if dirname and dirname != os.curdir:
42 self.dir = dirname
43 index = -1
44 for i in range(len(sys.path)):
45 p = sys.path[i]
46 if p in ("", os.curdir):
47 index = i
48 if index >= 0:
49 sys.path[index] = dirname
50 else:
51 sys.path.insert(0, dirname)
52 else:
53 self.dir = os.getcwd()
54 sys.path.insert(0, self.dir+'/samples')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000055
Martin v. Löwis20efa682001-11-11 14:07:37 +000056 def MkMainMenu(self):
57 top = self.root
58 w = Tix.Frame(top, bd=2, relief=RAISED)
59 file = Tix.Menubutton(w, text='File', underline=0, takefocus=0)
60 help = Tix.Menubutton(w, text='Help', underline=0, takefocus=0)
61 file.pack(side=LEFT)
62 help.pack(side=RIGHT)
Martin v. Löwis652e1912001-11-25 14:50:56 +000063 fm = Tix.Menu(file, tearoff=0)
Martin v. Löwis20efa682001-11-11 14:07:37 +000064 file['menu'] = fm
Martin v. Löwis652e1912001-11-25 14:50:56 +000065 hm = Tix.Menu(help, tearoff=0)
Martin v. Löwis20efa682001-11-11 14:07:37 +000066 help['menu'] = hm
67
68 if w.tk.eval ('info commands console') == "console":
69 fm.add_command(label='Console', underline=1,
70 command=lambda w=w: w.tk.eval('console show'))
71
Martin v. Löwis652e1912001-11-25 14:50:56 +000072 fm.add_command(label='Exit', underline=1,
Martin v. Löwis20efa682001-11-11 14:07:37 +000073 command = lambda self=self: self.quitcmd () )
74 hm.add_checkbutton(label='BalloonHelp', underline=0, command=ToggleHelp,
75 variable=self.useBalloons)
76 # The trace variable option doesn't seem to work, instead I use 'command'
77 #apply(w.tk.call, ('trace', 'variable', self.useBalloons, 'w',
78 # ToggleHelp))
79 return w
80
81 def MkMainNotebook(self):
82 top = self.root
83 w = Tix.NoteBook(top, ipadx=5, ipady=5, options="""
84 *TixNoteBook*tagPadX 6
85 *TixNoteBook*tagPadY 4
86 *TixNoteBook*borderWidth 2
87 """)
88 # This may be required if there is no *Background option
89 top['bg'] = w['bg']
90
91 w.add('wel', label='Welcome', underline=0,
92 createcmd=lambda w=w, name='wel': MkWelcome(w, name))
93 w.add('cho', label='Choosers', underline=0,
94 createcmd=lambda w=w, name='cho': MkChoosers(w, name))
95 w.add('scr', label='Scrolled Widgets', underline=0,
96 createcmd=lambda w=w, name='scr': MkScroll(w, name))
97 w.add('mgr', label='Manager Widgets', underline=0,
98 createcmd=lambda w=w, name='mgr': MkManager(w, name))
99 w.add('dir', label='Directory List', underline=0,
100 createcmd=lambda w=w, name='dir': MkDirList(w, name))
101 w.add('exp', label='Run Sample Programs', underline=0,
102 createcmd=lambda w=w, name='exp': MkSample(w, name))
103 return w
104
105 def MkMainStatus(self):
106 global demo
107 top = self.root
108
109 w = Tix.Frame(top, relief=Tix.RAISED, bd=1)
110 demo.statusbar = Tix.Label(w, relief=Tix.SUNKEN, bd=1)
111 demo.statusbar.form(padx=3, pady=3, left=0, right='%70')
112 return w
113
114 def build(self):
115 root = self.root
116 z = root.winfo_toplevel()
117 z.wm_title('Tix Widget Demonstration')
118 z.geometry('790x590+10+10')
119
120 demo.balloon = Tix.Balloon(root)
121 frame1 = self.MkMainMenu()
122 frame2 = self.MkMainNotebook()
123 frame3 = self.MkMainStatus()
124 frame1.pack(side=TOP, fill=X)
125 frame3.pack(side=BOTTOM, fill=X)
126 frame2.pack(side=TOP, expand=1, fill=BOTH, padx=4, pady=4)
127 demo.balloon['statusbar'] = demo.statusbar
128 z.wm_protocol("WM_DELETE_WINDOW", lambda self=self: self.quitcmd())
129
130 def quitcmd (self):
Martin v. Löwis652e1912001-11-25 14:50:56 +0000131 """Quit our mainloop. It is up to you to call root.destroy() after."""
Martin v. Löwis20efa682001-11-11 14:07:37 +0000132 self.exit = 0
133
134 def loop(self):
Martin v. Löwis652e1912001-11-25 14:50:56 +0000135 import tkMessageBox, traceback
136 while self.exit < 0:
137 try:
Neal Norwitz719cfc42002-03-01 13:07:34 +0000138 self.root.tk.dooneevent(TCL_ALL_EVENTS)
Martin v. Löwis652e1912001-11-25 14:50:56 +0000139 except SystemExit:
140 #print 'Exit'
141 self.exit = 1
142 break
143 except KeyboardInterrupt:
144 if tkMessageBox.askquestion ('Interrupt', 'Really Quit?') == 'yes':
145 # self.tk.eval('exit')
146 return
147 else:
148 pass
149 continue
150 except:
151 t, v, tb = sys.exc_info()
152 text = ""
153 for line in traceback.format_exception(t,v,tb):
154 text += line + '\n'
155 try: tkMessageBox.showerror ('Error', text)
156 except: pass
157 tkinspect_quit (1)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000158
159 def destroy (self):
160 self.root.destroy()
161
Martin v. Löwis652e1912001-11-25 14:50:56 +0000162def RunMain(root):
163 global demo
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000164
Martin v. Löwis652e1912001-11-25 14:50:56 +0000165 demo = Demo(root)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000166
Martin v. Löwis20efa682001-11-11 14:07:37 +0000167 demo.build()
168 demo.loop()
169 demo.destroy()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000170
Martin v. Löwis20efa682001-11-11 14:07:37 +0000171# Tabs
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000172def MkWelcome(nb, name):
173 w = nb.page(name)
174 bar = MkWelcomeBar(w)
175 text = MkWelcomeText(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000176 bar.pack(side=TOP, fill=X, padx=2, pady=2)
177 text.pack(side=TOP, fill=BOTH, expand=1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000178
179def MkWelcomeBar(top):
180 global demo
181
182 w = Tix.Frame(top, bd=2, relief=Tix.GROOVE)
183 b1 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
184 b2 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
185 b1.entry['width'] = 15
186 b1.slistbox.listbox['height'] = 3
187 b2.entry['width'] = 4
188 b2.slistbox.listbox['height'] = 3
189
190 demo.welfont = b1
191 demo.welsize = b2
192
193 b1.insert(Tix.END, 'Courier')
194 b1.insert(Tix.END, 'Helvetica')
195 b1.insert(Tix.END, 'Lucida')
196 b1.insert(Tix.END, 'Times Roman')
197
198 b2.insert(Tix.END, '8')
199 b2.insert(Tix.END, '10')
200 b2.insert(Tix.END, '12')
201 b2.insert(Tix.END, '14')
202 b2.insert(Tix.END, '18')
203
204 b1.pick(1)
205 b2.pick(3)
206
207 b1.pack(side=Tix.LEFT, padx=4, pady=4)
208 b2.pack(side=Tix.LEFT, padx=4, pady=4)
209
210 demo.balloon.bind_widget(b1, msg='Choose\na font',
211 statusmsg='Choose a font for this page')
212 demo.balloon.bind_widget(b2, msg='Point size',
213 statusmsg='Choose the font size for this page')
214 return w
215
216def MkWelcomeText(top):
217 global demo
218
219 w = Tix.ScrolledWindow(top, scrollbar='auto')
220 win = w.window
221 text = 'Welcome to TIX in Python'
Martin v. Löwis20efa682001-11-11 14:07:37 +0000222 title = Tix.Label(win,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000223 bd=0, width=30, anchor=Tix.N, text=text)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000224 msg = Tix.Message(win,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000225 bd=0, width=400, anchor=Tix.N,
226 text='Tix is a set of mega-widgets based on TK. This program \
227demonstrates the widgets in the Tix widget set. You can choose the pages \
228in this window to look at the corresponding widgets. \n\n\
229To quit this program, choose the "File | Exit" command.\n\n\
230For more information, see http://tix.sourceforge.net.')
231 title.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
232 msg.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
233 demo.welmsg = msg
234 return w
235
236def MainTextFont(w):
237 global demo
238
239 if not demo.welmsg:
240 return
241 font = demo.welfont['value']
242 point = demo.welsize['value']
243 if font == 'Times Roman':
244 font = 'times'
Martin v. Löwis20efa682001-11-11 14:07:37 +0000245 fontstr = '%s %s' % (font, point)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000246 demo.welmsg['font'] = fontstr
247
248def ToggleHelp():
249 if demo.useBalloons.get() == '1':
250 demo.balloon['state'] = 'both'
251 else:
252 demo.balloon['state'] = 'none'
253
254def MkChoosers(nb, name):
255 w = nb.page(name)
256 prefix = Tix.OptionName(w)
257 if not prefix:
258 prefix = ''
259 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
260
261 til = Tix.LabelFrame(w, label='Chooser Widgets')
262 cbx = Tix.LabelFrame(w, label='tixComboBox')
263 ctl = Tix.LabelFrame(w, label='tixControl')
264 sel = Tix.LabelFrame(w, label='tixSelect')
265 opt = Tix.LabelFrame(w, label='tixOptionMenu')
266 fil = Tix.LabelFrame(w, label='tixFileEntry')
267 fbx = Tix.LabelFrame(w, label='tixFileSelectBox')
268 tbr = Tix.LabelFrame(w, label='Tool Bar')
269
270 MkTitle(til.frame)
271 MkCombo(cbx.frame)
272 MkControl(ctl.frame)
273 MkSelect(sel.frame)
274 MkOptMenu(opt.frame)
275 MkFileEnt(fil.frame)
276 MkFileBox(fbx.frame)
277 MkToolBar(tbr.frame)
278
279 # First column: comBox and selector
280 cbx.form(top=0, left=0, right='%33')
281 sel.form(left=0, right='&'+str(cbx), top=cbx)
282 opt.form(left=0, right='&'+str(cbx), top=sel, bottom=-1)
283
284 # Second column: title .. etc
285 til.form(left=cbx, top=0,right='%66')
286 ctl.form(left=cbx, right='&'+str(til), top=til)
287 fil.form(left=cbx, right='&'+str(til), top=ctl)
288 tbr.form(left=cbx, right='&'+str(til), top=fil, bottom=-1)
289
290 #
291 # Third column: file selection
292 fbx.form(right=-1, top=0, left='%66')
293
294def MkCombo(w):
295 prefix = Tix.OptionName(w)
296 if not prefix: prefix = ''
297 w.option_add('*' + prefix + '*TixComboBox*label.width', 10)
298 w.option_add('*' + prefix + '*TixComboBox*label.anchor', Tix.E)
299 w.option_add('*' + prefix + '*TixComboBox*entry.width', 14)
300
301 static = Tix.ComboBox(w, label='Static', editable=0)
302 editable = Tix.ComboBox(w, label='Editable', editable=1)
303 history = Tix.ComboBox(w, label='History', editable=1, history=1,
304 anchor=Tix.E)
305 static.insert(Tix.END, 'January')
306 static.insert(Tix.END, 'February')
307 static.insert(Tix.END, 'March')
308 static.insert(Tix.END, 'April')
309 static.insert(Tix.END, 'May')
310 static.insert(Tix.END, 'June')
311 static.insert(Tix.END, 'July')
312 static.insert(Tix.END, 'August')
313 static.insert(Tix.END, 'September')
314 static.insert(Tix.END, 'October')
315 static.insert(Tix.END, 'November')
316 static.insert(Tix.END, 'December')
317
318 editable.insert(Tix.END, 'Angola')
319 editable.insert(Tix.END, 'Bangladesh')
320 editable.insert(Tix.END, 'China')
321 editable.insert(Tix.END, 'Denmark')
322 editable.insert(Tix.END, 'Ecuador')
323
324 history.insert(Tix.END, '/usr/bin/ksh')
325 history.insert(Tix.END, '/usr/local/lib/python')
326 history.insert(Tix.END, '/var/adm')
327
328 static.pack(side=Tix.TOP, padx=5, pady=3)
329 editable.pack(side=Tix.TOP, padx=5, pady=3)
330 history.pack(side=Tix.TOP, padx=5, pady=3)
331
332states = ['Bengal', 'Delhi', 'Karnataka', 'Tamil Nadu']
333
334def spin_cmd(w, inc):
335 idx = states.index(demo_spintxt.get()) + inc
336 if idx < 0:
337 idx = len(states) - 1
338 elif idx >= len(states):
339 idx = 0
340# following doesn't work.
341# return states[idx]
342 demo_spintxt.set(states[idx]) # this works
343
344def spin_validate(w):
345 global states, demo_spintxt
346
347 try:
348 i = states.index(demo_spintxt.get())
Fred Drake7def2562001-05-11 19:44:55 +0000349 except ValueError:
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000350 return states[0]
351 return states[i]
352 # why this procedure works as opposed to the previous one beats me.
353
354def MkControl(w):
355 global demo_spintxt
356
357 prefix = Tix.OptionName(w)
358 if not prefix: prefix = ''
359 w.option_add('*' + prefix + '*TixControl*label.width', 10)
360 w.option_add('*' + prefix + '*TixControl*label.anchor', Tix.E)
361 w.option_add('*' + prefix + '*TixControl*entry.width', 13)
362
363 demo_spintxt = Tix.StringVar()
364 demo_spintxt.set(states[0])
365 simple = Tix.Control(w, label='Numbers')
366 spintxt = Tix.Control(w, label='States', variable=demo_spintxt)
367 spintxt['incrcmd'] = lambda w=spintxt: spin_cmd(w, 1)
368 spintxt['decrcmd'] = lambda w=spintxt: spin_cmd(w, -1)
369 spintxt['validatecmd'] = lambda w=spintxt: spin_validate(w)
370
371 simple.pack(side=Tix.TOP, padx=5, pady=3)
372 spintxt.pack(side=Tix.TOP, padx=5, pady=3)
373
374def MkSelect(w):
375 prefix = Tix.OptionName(w)
376 if not prefix: prefix = ''
377 w.option_add('*' + prefix + '*TixSelect*label.anchor', Tix.CENTER)
378 w.option_add('*' + prefix + '*TixSelect*orientation', Tix.VERTICAL)
379 w.option_add('*' + prefix + '*TixSelect*labelSide', Tix.TOP)
380
381 sel1 = Tix.Select(w, label='Mere Mortals', allowzero=1, radio=1)
382 sel2 = Tix.Select(w, label='Geeks', allowzero=1, radio=0)
383
384 sel1.add('eat', text='Eat')
385 sel1.add('work', text='Work')
386 sel1.add('play', text='Play')
387 sel1.add('party', text='Party')
388 sel1.add('sleep', text='Sleep')
389
390 sel2.add('eat', text='Eat')
391 sel2.add('prog1', text='Program')
392 sel2.add('prog2', text='Program')
393 sel2.add('prog3', text='Program')
394 sel2.add('sleep', text='Sleep')
395
396 sel1.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
397 sel2.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
398
399def MkOptMenu(w):
400 prefix = Tix.OptionName(w)
401 if not prefix: prefix = ''
402 w.option_add('*' + prefix + '*TixOptionMenu*label.anchor', Tix.E)
403 m = Tix.OptionMenu(w, label='File Format : ', options='menubutton.width 15')
404 m.add_command('text', label='Plain Text')
405 m.add_command('post', label='PostScript')
406 m.add_command('format', label='Formatted Text')
407 m.add_command('html', label='HTML')
408 m.add_command('sep')
409 m.add_command('tex', label='LaTeX')
410 m.add_command('rtf', label='Rich Text Format')
411
412 m.pack(fill=Tix.X, padx=5, pady=3)
413
414def MkFileEnt(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000415 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000416 relief=Tix.FLAT, width=240, anchor=Tix.N,
417 text='Press the "open file" icon button and a TixFileSelectDialog will popup.')
418 ent = Tix.FileEntry(w, label='Select a file : ')
419 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
420 ent.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
421
422def MkFileBox(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000423 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000424 relief=Tix.FLAT, width=240, anchor=Tix.N,
425 text='The TixFileSelectBox is a Motif-style box with various enhancements. For example, you can adjust the size of the two listboxes and your past selections are recorded.')
426 box = Tix.FileSelectBox(w)
427 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
428 box.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
429
430def MkToolBar(w):
431 global demo
432
433 prefix = Tix.OptionName(w)
434 if not prefix: prefix = ''
435 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000436 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000437 relief=Tix.FLAT, width=240, anchor=Tix.N,
438 text='The Select widget is also good for arranging buttons in a tool bar.')
439 bar = Tix.Frame(w, bd=2, relief=Tix.RAISED)
440 font = Tix.Select(w, allowzero=1, radio=0, label='')
441 para = Tix.Select(w, allowzero=0, radio=1, label='')
442
443 font.add('bold', bitmap='@' + demo.dir + '/bitmaps/bold.xbm')
444 font.add('italic', bitmap='@' + demo.dir + '/bitmaps/italic.xbm')
445 font.add('underline', bitmap='@' + demo.dir + '/bitmaps/underline.xbm')
446 font.add('capital', bitmap='@' + demo.dir + '/bitmaps/capital.xbm')
447
448 para.add('left', bitmap='@' + demo.dir + '/bitmaps/leftj.xbm')
449 para.add('right', bitmap='@' + demo.dir + '/bitmaps/rightj.xbm')
450 para.add('center', bitmap='@' + demo.dir + '/bitmaps/centerj.xbm')
451 para.add('justify', bitmap='@' + demo.dir + '/bitmaps/justify.xbm')
452
453 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
454 bar.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
455 font.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
456 para.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
457
458def MkTitle(w):
459 prefix = Tix.OptionName(w)
460 if not prefix: prefix = ''
461 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000462 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000463 relief=Tix.FLAT, width=240, anchor=Tix.N,
464 text='There are many types of "chooser" widgets that allow the user to input different types of information')
465 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
466
467def MkScroll(nb, name):
468 w = nb.page(name)
469 prefix = Tix.OptionName(w)
470 if not prefix:
471 prefix = ''
472 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
473
474 sls = Tix.LabelFrame(w, label='tixScrolledListBox')
475 swn = Tix.LabelFrame(w, label='tixScrolledWindow')
476 stx = Tix.LabelFrame(w, label='tixScrolledText')
477
478 MkSList(sls.frame)
479 MkSWindow(swn.frame)
480 MkSText(stx.frame)
481
482 sls.form(top=0, left=0, right='%33', bottom=-1)
483 swn.form(top=0, left=sls, right='%66', bottom=-1)
484 stx.form(top=0, left=swn, right=-1, bottom=-1)
485
486def MkSList(w):
487 top = Tix.Frame(w, width=300, height=330)
488 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000489 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000490 relief=Tix.FLAT, width=200, anchor=Tix.N,
491 text='This TixScrolledListBox is configured so that it uses scrollbars only when it is necessary. Use the handles to resize the listbox and watch the scrollbars automatically appear and disappear.')
492
493 list = Tix.ScrolledListBox(top, scrollbar='auto')
494 list.place(x=50, y=150, width=120, height=80)
495 list.listbox.insert(Tix.END, 'Alabama')
496 list.listbox.insert(Tix.END, 'California')
497 list.listbox.insert(Tix.END, 'Montana')
498 list.listbox.insert(Tix.END, 'New Jersey')
499 list.listbox.insert(Tix.END, 'New York')
500 list.listbox.insert(Tix.END, 'Pennsylvania')
501 list.listbox.insert(Tix.END, 'Washington')
502
503 rh = Tix.ResizeHandle(top, bg='black',
504 relief=Tix.RAISED,
505 handlesize=8, gridded=1, minwidth=50, minheight=30)
506 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=list: SList_reset(w,x))
507 top.propagate(0)
508 msg.pack(fill=Tix.X)
509 btn.pack(anchor=Tix.CENTER)
510 top.pack(expand=1, fill=Tix.BOTH)
511 bot.pack(fill=Tix.BOTH)
512 list.bind('<Map>', func=lambda arg=0, rh=rh, list=list:
513 list.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(list)))
514
515def SList_reset(rh, list):
516 list.place(x=50, y=150, width=120, height=80)
517 list.update()
518 rh.attach_widget(list)
519
Martin v. Löwis652e1912001-11-25 14:50:56 +0000520# See below why this is necessary.
521global image1
522image1 = None
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000523def MkSWindow(w):
Martin v. Löwis652e1912001-11-25 14:50:56 +0000524 global demo, image1
525
526 text = 'The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.'
527
528 file = os.path.join(demo.dir, 'bitmaps', 'tix.gif')
529 if not os.path.isfile(file):
530 text += ' (Image missing)'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000531
532 top = Tix.Frame(w, width=330, height=330)
533 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000534 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000535 relief=Tix.FLAT, width=200, anchor=Tix.N,
Martin v. Löwis652e1912001-11-25 14:50:56 +0000536 text=text)
537
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000538 win = Tix.ScrolledWindow(top, scrollbar='auto')
Martin v. Löwis652e1912001-11-25 14:50:56 +0000539
540 # This image is not showing up under Python unless it is set to a
541 # global variable - no problem under Tcl. I assume it is being garbage
542 # collected some how, even though the tcl command 'image names' shows
543 # that as far as Tcl is concerned, the image exists and is called pyimage1.
544 image1 = Tix.Image('photo', file=file)
545 lbl = Tix.Label(win.window, image=image1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000546 lbl.pack(expand=1, fill=Tix.BOTH)
547
548 win.place(x=30, y=150, width=190, height=120)
549
550 rh = Tix.ResizeHandle(top, bg='black',
551 relief=Tix.RAISED,
552 handlesize=8, gridded=1, minwidth=50, minheight=30)
553 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SWindow_reset(w,x))
554 top.propagate(0)
555 msg.pack(fill=Tix.X)
556 btn.pack(anchor=Tix.CENTER)
557 top.pack(expand=1, fill=Tix.BOTH)
558 bot.pack(fill=Tix.BOTH)
559 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
560 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
561
562def SWindow_reset(rh, win):
563 win.place(x=30, y=150, width=190, height=120)
564 win.update()
565 rh.attach_widget(win)
566
567def MkSText(w):
568 top = Tix.Frame(w, width=330, height=330)
569 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000570 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000571 relief=Tix.FLAT, width=200, anchor=Tix.N,
572 text='The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.')
573
574 win = Tix.ScrolledText(top, scrollbar='auto')
575# win.text['wrap'] = 'none'
576 win.text.insert(Tix.END, 'This is a text widget embedded in a scrolled window. Although the original Tix demo does not have any text here, I decided to put in some so that you can see the effect of scrollbars etc.')
577 win.place(x=30, y=150, width=190, height=100)
578
579 rh = Tix.ResizeHandle(top, bg='black',
580 relief=Tix.RAISED,
581 handlesize=8, gridded=1, minwidth=50, minheight=30)
582 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SText_reset(w,x))
583 top.propagate(0)
584 msg.pack(fill=Tix.X)
585 btn.pack(anchor=Tix.CENTER)
586 top.pack(expand=1, fill=Tix.BOTH)
587 bot.pack(fill=Tix.BOTH)
588 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
589 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
590
591def SText_reset(rh, win):
592 win.place(x=30, y=150, width=190, height=120)
593 win.update()
594 rh.attach_widget(win)
595
596def MkManager(nb, name):
597 w = nb.page(name)
598 prefix = Tix.OptionName(w)
599 if not prefix:
600 prefix = ''
601 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
602
603 pane = Tix.LabelFrame(w, label='tixPanedWindow')
604 note = Tix.LabelFrame(w, label='tixNoteBook')
605
606 MkPanedWindow(pane.frame)
607 MkNoteBook(note.frame)
608
609 pane.form(top=0, left=0, right=note, bottom=-1)
610 note.form(top=0, right=-1, bottom=-1)
611
612def MkPanedWindow(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000613 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000614 relief=Tix.FLAT, width=240, anchor=Tix.N,
615 text='The PanedWindow widget allows the user to interactively manipulate the sizes of several panes. The panes can be arranged either vertically or horizontally.')
Martin v. Löwis652e1912001-11-25 14:50:56 +0000616 group = Tix.LabelEntry(w, label='Newsgroup:', options='entry.width 25')
617 group.entry.insert(0,'comp.lang.python')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000618 pane = Tix.PanedWindow(w, orientation='vertical')
619
620 p1 = pane.add('list', min=70, size=100)
621 p2 = pane.add('text', min=70)
622 list = Tix.ScrolledListBox(p1)
623 text = Tix.ScrolledText(p2)
624
Martin v. Löwis652e1912001-11-25 14:50:56 +0000625 list.listbox.insert(Tix.END, " 12324 Re: Tkinter is good for your health")
626 list.listbox.insert(Tix.END, "+ 12325 Re: Tkinter is good for your health")
627 list.listbox.insert(Tix.END, "+ 12326 Re: Tix is even better for your health (Was: Tkinter is good...)")
628 list.listbox.insert(Tix.END, " 12327 Re: Tix is even better for your health (Was: Tkinter is good...)")
629 list.listbox.insert(Tix.END, "+ 12328 Re: Tix is even better for your health (Was: Tkinter is good...)")
630 list.listbox.insert(Tix.END, " 12329 Re: Tix is even better for your health (Was: Tkinter is good...)")
631 list.listbox.insert(Tix.END, "+ 12330 Re: Tix is even better for your health (Was: Tkinter is good...)")
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000632
633 text.text['bg'] = list.listbox['bg']
634 text.text['wrap'] = 'none'
635 text.text.insert(Tix.END, """
Martin v. Löwis652e1912001-11-25 14:50:56 +0000636Mon, 19 Jun 1995 11:39:52 comp.lang.python Thread 34 of 220
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000637Lines 353 A new way to put text and bitmaps together iNo responses
638ioi@blue.seas.upenn.edu Ioi K. Lam at University of Pennsylvania
639
640Hi,
641
642I have implemented a new image type called "compound". It allows you
643to glue together a bunch of bitmaps, images and text strings together
644to form a bigger image. Then you can use this image with widgets that
645support the -image option. For example, you can display a text string string
646together with a bitmap, at the same time, inside a TK button widget.
647""")
648 list.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
649 text.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
650
651 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
652 group.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
653 pane.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH, expand=1)
654
655def MkNoteBook(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000656 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000657 relief=Tix.FLAT, width=240, anchor=Tix.N,
658 text='The NoteBook widget allows you to layout a complex interface into individual pages.')
659 prefix = Tix.OptionName(w)
660 if not prefix:
661 prefix = ''
662 w.option_add('*' + prefix + '*TixControl*entry.width', 10)
663 w.option_add('*' + prefix + '*TixControl*label.width', 18)
664 w.option_add('*' + prefix + '*TixControl*label.anchor', Tix.E)
665 w.option_add('*' + prefix + '*TixNoteBook*tagPadX', 8)
666
667 nb = Tix.NoteBook(w, ipadx=6, ipady=6)
668 nb.add('hard_disk', label="Hard Disk", underline=0)
669 nb.add('network', label="Network", underline=0)
670
671 # Frame for the buttons that are present on all pages
672 common = Tix.Frame(nb.hard_disk)
673 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
674 CreateCommonButtons(common)
675
676 # Widgets belonging only to this page
677 a = Tix.Control(nb.hard_disk, value=12, label='Access Time: ')
678 w = Tix.Control(nb.hard_disk, value=400, label='Write Throughput: ')
679 r = Tix.Control(nb.hard_disk, value=400, label='Read Throughput: ')
680 c = Tix.Control(nb.hard_disk, value=1021, label='Capacity: ')
681 a.pack(side=Tix.TOP, padx=20, pady=2)
682 w.pack(side=Tix.TOP, padx=20, pady=2)
683 r.pack(side=Tix.TOP, padx=20, pady=2)
684 c.pack(side=Tix.TOP, padx=20, pady=2)
685
686 common = Tix.Frame(nb.network)
687 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
688 CreateCommonButtons(common)
689
690 a = Tix.Control(nb.network, value=12, label='Access Time: ')
691 w = Tix.Control(nb.network, value=400, label='Write Throughput: ')
692 r = Tix.Control(nb.network, value=400, label='Read Throughput: ')
693 c = Tix.Control(nb.network, value=1021, label='Capacity: ')
694 u = Tix.Control(nb.network, value=10, label='Users: ')
695 a.pack(side=Tix.TOP, padx=20, pady=2)
696 w.pack(side=Tix.TOP, padx=20, pady=2)
697 r.pack(side=Tix.TOP, padx=20, pady=2)
698 c.pack(side=Tix.TOP, padx=20, pady=2)
699 u.pack(side=Tix.TOP, padx=20, pady=2)
700
701 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
702 nb.pack(side=Tix.TOP, padx=5, pady=5, fill=Tix.BOTH, expand=1)
703
704def CreateCommonButtons(f):
705 ok = Tix.Button(f, text='OK', width = 6)
706 cancel = Tix.Button(f, text='Cancel', width = 6)
707 ok.pack(side=Tix.TOP, padx=2, pady=2)
708 cancel.pack(side=Tix.TOP, padx=2, pady=2)
709
710def MkDirList(nb, name):
711 w = nb.page(name)
712 prefix = Tix.OptionName(w)
713 if not prefix:
714 prefix = ''
715 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
716
717 dir = Tix.LabelFrame(w, label='tixDirList')
718 fsbox = Tix.LabelFrame(w, label='tixExFileSelectBox')
719 MkDirListWidget(dir.frame)
720 MkExFileWidget(fsbox.frame)
721 dir.form(top=0, left=0, right='%40', bottom=-1)
722 fsbox.form(top=0, left='%40', right=-1, bottom=-1)
723
724def MkDirListWidget(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000725 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000726 relief=Tix.FLAT, width=240, anchor=Tix.N,
727 text='The TixDirList widget gives a graphical representation of the file system directory and makes it easy for the user to choose and access directories.')
728 dirlist = Tix.DirList(w, options='hlist.padY 1 hlist.width 25 hlist.height 16')
729 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
730 dirlist.pack(side=Tix.TOP, padx=3, pady=3)
731
732def MkExFileWidget(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000733 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000734 relief=Tix.FLAT, width=240, anchor=Tix.N,
735 text='The TixExFileSelectBox widget is more user friendly than the Motif style FileSelectBox.')
736 # There's a bug in the ComboBoxes - the scrolledlistbox is destroyed
737 box = Tix.ExFileSelectBox(w, bd=2, relief=Tix.RAISED)
738 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
739 box.pack(side=Tix.TOP, padx=3, pady=3)
740
741###
742### List of all the demos we want to show off
743comments = {'widget' : 'Widget Demos', 'image' : 'Image Demos'}
744samples = {'Balloon' : 'Balloon',
745 'Button Box' : 'BtnBox',
746 'Combo Box' : 'ComboBox',
747 'Compound Image' : 'CmpImg',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000748 'Directory List' : 'DirList',
749 'Directory Tree' : 'DirTree',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000750 'Control' : 'Control',
751 'Notebook' : 'NoteBook',
752 'Option Menu' : 'OptMenu',
Martin v. Löwis652e1912001-11-25 14:50:56 +0000753 'Paned Window' : 'PanedWin',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000754 'Popup Menu' : 'PopMenu',
755 'ScrolledHList (1)' : 'SHList1',
756 'ScrolledHList (2)' : 'SHList2',
757 'Tree (dynamic)' : 'Tree'
758}
759
Martin v. Löwis20efa682001-11-11 14:07:37 +0000760# There are still a lot of demos to be translated:
761## set root {
762## {d "File Selectors" file }
763## {d "Hierachical ListBox" hlist }
764## {d "Tabular ListBox" tlist {c tixTList}}
765## {d "Grid Widget" grid {c tixGrid}}
766## {d "Manager Widgets" manager }
767## {d "Scrolled Widgets" scroll }
768## {d "Miscellaneous Widgets" misc }
769## {d "Image Types" image }
770## }
771##
772## set image {
773## {d "Compound Image" cmpimg }
774## {d "XPM Image" xpm {i pixmap}}
775## }
776##
777## set cmpimg {
778## {f "In Buttons" CmpImg.tcl }
779## {f "In NoteBook" CmpImg2.tcl }
780## {f "Notebook Color Tabs" CmpImg4.tcl }
781## {f "Icons" CmpImg3.tcl }
782## }
783##
784## set xpm {
785## {f "In Button" Xpm.tcl {i pixmap}}
786## {f "In Menu" Xpm1.tcl {i pixmap}}
787## }
788##
789## set file {
790##added {f DirList DirList.tcl }
791##added {f DirTree DirTree.tcl }
792## {f DirSelectDialog DirDlg.tcl }
793## {f ExFileSelectDialog EFileDlg.tcl }
794## {f FileSelectDialog FileDlg.tcl }
795## {f FileEntry FileEnt.tcl }
796## }
797##
798## set hlist {
799## {f HList HList1.tcl }
800## {f CheckList ChkList.tcl {c tixCheckList}}
801##done {f "ScrolledHList (1)" SHList.tcl }
802##done {f "ScrolledHList (2)" SHList2.tcl }
803##done {f Tree Tree.tcl }
804##done {f "Tree (Dynamic)" DynTree.tcl {v win}}
805## }
806##
807## set tlist {
808## {f "ScrolledTList (1)" STList1.tcl {c tixTList}}
809## {f "ScrolledTList (2)" STList2.tcl {c tixTList}}
810## }
811## global tcl_platform
812## # This demo hangs windows
813## if {$tcl_platform(platform) != "windows"} {
814##na lappend tlist {f "TList File Viewer" STList3.tcl {c tixTList}}
815## }
816##
817## set grid {
818##na {f "Simple Grid" SGrid0.tcl {c tixGrid}}
819##na {f "ScrolledGrid" SGrid1.tcl {c tixGrid}}
820##na {f "Editable Grid" EditGrid.tcl {c tixGrid}}
821## }
822##
823## set scroll {
824## {f ScrolledListBox SListBox.tcl }
825## {f ScrolledText SText.tcl }
826## {f ScrolledWindow SWindow.tcl }
827##na {f "Canvas Object View" CObjView.tcl {c tixCObjView}}
828## }
829##
830## set manager {
831##na {f ListNoteBook ListNBK.tcl }
Martin v. Löwis652e1912001-11-25 14:50:56 +0000832##done {f NoteBook NoteBook.tcl }
833##done {f PanedWindow PanedWin.tcl }
Martin v. Löwis20efa682001-11-11 14:07:37 +0000834## }
835##
836## set misc {
837##done {f Balloon Balloon.tcl }
838##done {f ButtonBox BtnBox.tcl }
839##done {f ComboBox ComboBox.tcl }
840##done {f Control Control.tcl }
841## {f LabelEntry LabEntry.tcl }
842## {f LabelFrame LabFrame.tcl }
843##na {f Meter Meter.tcl {c tixMeter}}
844##done {f OptionMenu OptMenu.tcl }
845##done {f PopupMenu PopMenu.tcl }
846## {f Select Select.tcl }
847## {f StdButtonBox StdBBox.tcl }
848## }
849##
850
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000851stypes = {}
852stypes['widget'] = ['Balloon', 'Button Box', 'Combo Box', 'Control',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000853 'Directory List', 'Directory Tree',
Martin v. Löwis652e1912001-11-25 14:50:56 +0000854 'Notebook', 'Option Menu', 'Popup Menu', 'Paned Window',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000855 'ScrolledHList (1)', 'ScrolledHList (2)', 'Tree (dynamic)']
856stypes['image'] = ['Compound Image']
857
858def MkSample(nb, name):
859 w = nb.page(name)
860 prefix = Tix.OptionName(w)
861 if not prefix:
862 prefix = ''
Martin v. Löwis652e1912001-11-25 14:50:56 +0000863 else:
864 prefix = '*' + prefix
865 w.option_add(prefix + '*TixLabelFrame*label.padX', 4)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000866
Martin v. Löwis652e1912001-11-25 14:50:56 +0000867 pane = Tix.PanedWindow(w, orientation='horizontal')
868 pane.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH)
869 f1 = pane.add('list', expand='1')
870 f2 = pane.add('text', expand='5')
871 f1['relief'] = 'flat'
872 f2['relief'] = 'flat'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000873
Martin v. Löwis652e1912001-11-25 14:50:56 +0000874 lab = Tix.Label(f1, text='Select a sample program:', anchor=Tix.W)
875 lab.pack(side=Tix.TOP, expand=0, fill=Tix.X, padx=5, pady=5)
876 lab1 = Tix.Label(f2, text='Source:', anchor=Tix.W)
877 lab1.pack(side=Tix.TOP, expand=0, fill=Tix.X, padx=5, pady=5)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000878
Martin v. Löwis652e1912001-11-25 14:50:56 +0000879 slb = Tix.Tree(f1, options='hlist.width 25')
880 slb.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=5)
881
882 stext = Tix.ScrolledText(f2, name='stext')
Martin v. Löwis20efa682001-11-11 14:07:37 +0000883 font = root.tk.eval('tix option get fixed_font')
884 stext.text.config(font=font)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000885 stext.text.bind('<Up>', lambda w=stext.text: w.yview(scroll='-1 unit'))
886 stext.text.bind('<Down>', lambda w=stext.text: w.yview(scroll='1 unit'))
887 stext.text.bind('<Left>', lambda w=stext.text: w.xview(scroll='-1 unit'))
888 stext.text.bind('<Right>', lambda w=stext.text: w.xview(scroll='1 unit'))
Martin v. Löwis652e1912001-11-25 14:50:56 +0000889 stext.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=7)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000890
Martin v. Löwis652e1912001-11-25 14:50:56 +0000891 frame = Tix.Frame(f2, name='frame')
892 frame.pack(side=Tix.TOP, expand=0, fill=Tix.X, padx=7)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000893
Martin v. Löwis652e1912001-11-25 14:50:56 +0000894 run = Tix.Button(frame, text='Run ...', name='run')
895 view = Tix.Button(frame, text='View Source ...', name='view')
896 run.pack(side=Tix.LEFT, expand=0, fill=Tix.NONE)
897 view.pack(side=Tix.LEFT, expand=0, fill=Tix.NONE)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000898
899 stext.text['bg'] = slb.hlist['bg']
900 stext.text['state'] = 'disabled'
901 stext.text['wrap'] = 'none'
Martin v. Löwis652e1912001-11-25 14:50:56 +0000902 stext.text['width'] = 80
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000903
904 slb.hlist['separator'] = '.'
905 slb.hlist['width'] = 25
906 slb.hlist['drawbranch'] = 0
907 slb.hlist['indent'] = 10
908 slb.hlist['wideselect'] = 1
Martin v. Löwis652e1912001-11-25 14:50:56 +0000909 slb.hlist['command'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'run')
910 slb.hlist['browsecmd'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'browse')
911
912 run['command'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'run')
913 view['command'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'view')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000914
915 for type in ['widget', 'image']:
916 if type != 'widget':
917 x = Tix.Frame(slb.hlist, bd=2, height=2, width=150,
918 relief=Tix.SUNKEN, bg=slb.hlist['bg'])
919 slb.hlist.add_child(itemtype=Tix.WINDOW, window=x, state='disabled')
920 x = slb.hlist.add_child(itemtype=Tix.TEXT, state='disabled',
921 text=comments[type])
922 for key in stypes[type]:
923 slb.hlist.add_child(x, itemtype=Tix.TEXT, data=key,
924 text=key)
925 slb.hlist.selection_clear()
926
927 run['state'] = 'disabled'
928 view['state'] = 'disabled'
929
Martin v. Löwis652e1912001-11-25 14:50:56 +0000930def Sample_Action(w, slb, stext, run, view, action):
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000931 global demo
932
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000933 hlist = slb.hlist
934 anchor = hlist.info_anchor()
935 if not anchor:
936 run['state'] = 'disabled'
937 view['state'] = 'disabled'
938 elif not hlist.info_parent(anchor):
939 # a comment
940 return
941
942 run['state'] = 'normal'
943 view['state'] = 'normal'
944 key = hlist.info_data(anchor)
945 title = key
946 prog = samples[key]
947
948 if action == 'run':
949 exec('import ' + prog)
950 w = Tix.Toplevel()
951 w.title(title)
952 rtn = eval(prog + '.RunSample')
953 rtn(w)
954 elif action == 'view':
955 w = Tix.Toplevel()
956 w.title('Source view: ' + title)
957 LoadFile(w, demo.dir + '/samples/' + prog + '.py')
958 elif action == 'browse':
959 ReadFile(stext.text, demo.dir + '/samples/' + prog + '.py')
960
961def LoadFile(w, fname):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000962 global root
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000963 b = Tix.Button(w, text='Close', command=w.destroy)
964 t = Tix.ScrolledText(w)
965 # b.form(left=0, bottom=0, padx=4, pady=4)
966 # t.form(left=0, bottom=b, right='-0', top=0)
967 t.pack()
968 b.pack()
969
Martin v. Löwis20efa682001-11-11 14:07:37 +0000970 font = root.tk.eval('tix option get fixed_font')
971 t.text.config(font=font)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000972 t.text['bd'] = 2
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000973 t.text['wrap'] = 'none'
974
975 ReadFile(t.text, fname)
976
977def ReadFile(w, fname):
978 old_state = w['state']
979 w['state'] = 'normal'
980 w.delete('0.0', Tix.END)
981
982 try:
983 f = open(fname)
984 lines = f.readlines()
985 for s in lines:
986 w.insert(Tix.END, s)
987 f.close()
988 finally:
989# w.see('1.0')
990 w['state'] = old_state
991
992if __name__ == '__main__':
Martin v. Löwis20efa682001-11-11 14:07:37 +0000993 root = Tix.Tk()
994 RunMain(root)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000995