blob: f0cf4d9a6e85754df65a693fccc4db48fee4925e [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
Martin v. Löwis8ec03e02002-03-17 18:19:13 +0000157 self.exit = 1
158 raise SystemExit, 1
Martin v. Löwis20efa682001-11-11 14:07:37 +0000159
160 def destroy (self):
161 self.root.destroy()
162
Martin v. Löwis652e1912001-11-25 14:50:56 +0000163def RunMain(root):
164 global demo
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000165
Martin v. Löwis652e1912001-11-25 14:50:56 +0000166 demo = Demo(root)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000167
Martin v. Löwis20efa682001-11-11 14:07:37 +0000168 demo.build()
169 demo.loop()
170 demo.destroy()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000171
Martin v. Löwis20efa682001-11-11 14:07:37 +0000172# Tabs
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000173def MkWelcome(nb, name):
174 w = nb.page(name)
175 bar = MkWelcomeBar(w)
176 text = MkWelcomeText(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000177 bar.pack(side=TOP, fill=X, padx=2, pady=2)
178 text.pack(side=TOP, fill=BOTH, expand=1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000179
180def MkWelcomeBar(top):
181 global demo
182
183 w = Tix.Frame(top, bd=2, relief=Tix.GROOVE)
184 b1 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
185 b2 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
186 b1.entry['width'] = 15
187 b1.slistbox.listbox['height'] = 3
188 b2.entry['width'] = 4
189 b2.slistbox.listbox['height'] = 3
190
191 demo.welfont = b1
192 demo.welsize = b2
193
194 b1.insert(Tix.END, 'Courier')
195 b1.insert(Tix.END, 'Helvetica')
196 b1.insert(Tix.END, 'Lucida')
197 b1.insert(Tix.END, 'Times Roman')
198
199 b2.insert(Tix.END, '8')
200 b2.insert(Tix.END, '10')
201 b2.insert(Tix.END, '12')
202 b2.insert(Tix.END, '14')
203 b2.insert(Tix.END, '18')
204
205 b1.pick(1)
206 b2.pick(3)
207
208 b1.pack(side=Tix.LEFT, padx=4, pady=4)
209 b2.pack(side=Tix.LEFT, padx=4, pady=4)
210
211 demo.balloon.bind_widget(b1, msg='Choose\na font',
212 statusmsg='Choose a font for this page')
213 demo.balloon.bind_widget(b2, msg='Point size',
214 statusmsg='Choose the font size for this page')
215 return w
216
217def MkWelcomeText(top):
218 global demo
219
220 w = Tix.ScrolledWindow(top, scrollbar='auto')
221 win = w.window
222 text = 'Welcome to TIX in Python'
Martin v. Löwis20efa682001-11-11 14:07:37 +0000223 title = Tix.Label(win,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000224 bd=0, width=30, anchor=Tix.N, text=text)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000225 msg = Tix.Message(win,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000226 bd=0, width=400, anchor=Tix.N,
227 text='Tix is a set of mega-widgets based on TK. This program \
228demonstrates the widgets in the Tix widget set. You can choose the pages \
229in this window to look at the corresponding widgets. \n\n\
230To quit this program, choose the "File | Exit" command.\n\n\
231For more information, see http://tix.sourceforge.net.')
232 title.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
233 msg.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
234 demo.welmsg = msg
235 return w
236
237def MainTextFont(w):
238 global demo
239
240 if not demo.welmsg:
241 return
242 font = demo.welfont['value']
243 point = demo.welsize['value']
244 if font == 'Times Roman':
245 font = 'times'
Martin v. Löwis20efa682001-11-11 14:07:37 +0000246 fontstr = '%s %s' % (font, point)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000247 demo.welmsg['font'] = fontstr
248
249def ToggleHelp():
250 if demo.useBalloons.get() == '1':
251 demo.balloon['state'] = 'both'
252 else:
253 demo.balloon['state'] = 'none'
254
255def MkChoosers(nb, name):
256 w = nb.page(name)
257 prefix = Tix.OptionName(w)
258 if not prefix:
259 prefix = ''
260 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
261
262 til = Tix.LabelFrame(w, label='Chooser Widgets')
263 cbx = Tix.LabelFrame(w, label='tixComboBox')
264 ctl = Tix.LabelFrame(w, label='tixControl')
265 sel = Tix.LabelFrame(w, label='tixSelect')
266 opt = Tix.LabelFrame(w, label='tixOptionMenu')
267 fil = Tix.LabelFrame(w, label='tixFileEntry')
268 fbx = Tix.LabelFrame(w, label='tixFileSelectBox')
269 tbr = Tix.LabelFrame(w, label='Tool Bar')
270
271 MkTitle(til.frame)
272 MkCombo(cbx.frame)
273 MkControl(ctl.frame)
274 MkSelect(sel.frame)
275 MkOptMenu(opt.frame)
276 MkFileEnt(fil.frame)
277 MkFileBox(fbx.frame)
278 MkToolBar(tbr.frame)
279
280 # First column: comBox and selector
281 cbx.form(top=0, left=0, right='%33')
282 sel.form(left=0, right='&'+str(cbx), top=cbx)
283 opt.form(left=0, right='&'+str(cbx), top=sel, bottom=-1)
284
285 # Second column: title .. etc
286 til.form(left=cbx, top=0,right='%66')
287 ctl.form(left=cbx, right='&'+str(til), top=til)
288 fil.form(left=cbx, right='&'+str(til), top=ctl)
289 tbr.form(left=cbx, right='&'+str(til), top=fil, bottom=-1)
290
291 #
292 # Third column: file selection
293 fbx.form(right=-1, top=0, left='%66')
294
295def MkCombo(w):
296 prefix = Tix.OptionName(w)
297 if not prefix: prefix = ''
298 w.option_add('*' + prefix + '*TixComboBox*label.width', 10)
299 w.option_add('*' + prefix + '*TixComboBox*label.anchor', Tix.E)
300 w.option_add('*' + prefix + '*TixComboBox*entry.width', 14)
301
302 static = Tix.ComboBox(w, label='Static', editable=0)
303 editable = Tix.ComboBox(w, label='Editable', editable=1)
304 history = Tix.ComboBox(w, label='History', editable=1, history=1,
305 anchor=Tix.E)
306 static.insert(Tix.END, 'January')
307 static.insert(Tix.END, 'February')
308 static.insert(Tix.END, 'March')
309 static.insert(Tix.END, 'April')
310 static.insert(Tix.END, 'May')
311 static.insert(Tix.END, 'June')
312 static.insert(Tix.END, 'July')
313 static.insert(Tix.END, 'August')
314 static.insert(Tix.END, 'September')
315 static.insert(Tix.END, 'October')
316 static.insert(Tix.END, 'November')
317 static.insert(Tix.END, 'December')
318
319 editable.insert(Tix.END, 'Angola')
320 editable.insert(Tix.END, 'Bangladesh')
321 editable.insert(Tix.END, 'China')
322 editable.insert(Tix.END, 'Denmark')
323 editable.insert(Tix.END, 'Ecuador')
324
325 history.insert(Tix.END, '/usr/bin/ksh')
326 history.insert(Tix.END, '/usr/local/lib/python')
327 history.insert(Tix.END, '/var/adm')
328
329 static.pack(side=Tix.TOP, padx=5, pady=3)
330 editable.pack(side=Tix.TOP, padx=5, pady=3)
331 history.pack(side=Tix.TOP, padx=5, pady=3)
332
333states = ['Bengal', 'Delhi', 'Karnataka', 'Tamil Nadu']
334
335def spin_cmd(w, inc):
336 idx = states.index(demo_spintxt.get()) + inc
337 if idx < 0:
338 idx = len(states) - 1
339 elif idx >= len(states):
340 idx = 0
341# following doesn't work.
342# return states[idx]
343 demo_spintxt.set(states[idx]) # this works
344
345def spin_validate(w):
346 global states, demo_spintxt
347
348 try:
349 i = states.index(demo_spintxt.get())
Fred Drake7def2562001-05-11 19:44:55 +0000350 except ValueError:
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000351 return states[0]
352 return states[i]
353 # why this procedure works as opposed to the previous one beats me.
354
355def MkControl(w):
356 global demo_spintxt
357
358 prefix = Tix.OptionName(w)
359 if not prefix: prefix = ''
360 w.option_add('*' + prefix + '*TixControl*label.width', 10)
361 w.option_add('*' + prefix + '*TixControl*label.anchor', Tix.E)
362 w.option_add('*' + prefix + '*TixControl*entry.width', 13)
363
364 demo_spintxt = Tix.StringVar()
365 demo_spintxt.set(states[0])
366 simple = Tix.Control(w, label='Numbers')
367 spintxt = Tix.Control(w, label='States', variable=demo_spintxt)
368 spintxt['incrcmd'] = lambda w=spintxt: spin_cmd(w, 1)
369 spintxt['decrcmd'] = lambda w=spintxt: spin_cmd(w, -1)
370 spintxt['validatecmd'] = lambda w=spintxt: spin_validate(w)
371
372 simple.pack(side=Tix.TOP, padx=5, pady=3)
373 spintxt.pack(side=Tix.TOP, padx=5, pady=3)
374
375def MkSelect(w):
376 prefix = Tix.OptionName(w)
377 if not prefix: prefix = ''
378 w.option_add('*' + prefix + '*TixSelect*label.anchor', Tix.CENTER)
379 w.option_add('*' + prefix + '*TixSelect*orientation', Tix.VERTICAL)
380 w.option_add('*' + prefix + '*TixSelect*labelSide', Tix.TOP)
381
382 sel1 = Tix.Select(w, label='Mere Mortals', allowzero=1, radio=1)
383 sel2 = Tix.Select(w, label='Geeks', allowzero=1, radio=0)
384
385 sel1.add('eat', text='Eat')
386 sel1.add('work', text='Work')
387 sel1.add('play', text='Play')
388 sel1.add('party', text='Party')
389 sel1.add('sleep', text='Sleep')
390
391 sel2.add('eat', text='Eat')
392 sel2.add('prog1', text='Program')
393 sel2.add('prog2', text='Program')
394 sel2.add('prog3', text='Program')
395 sel2.add('sleep', text='Sleep')
396
397 sel1.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
398 sel2.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
399
400def MkOptMenu(w):
401 prefix = Tix.OptionName(w)
402 if not prefix: prefix = ''
403 w.option_add('*' + prefix + '*TixOptionMenu*label.anchor', Tix.E)
404 m = Tix.OptionMenu(w, label='File Format : ', options='menubutton.width 15')
405 m.add_command('text', label='Plain Text')
406 m.add_command('post', label='PostScript')
407 m.add_command('format', label='Formatted Text')
408 m.add_command('html', label='HTML')
409 m.add_command('sep')
410 m.add_command('tex', label='LaTeX')
411 m.add_command('rtf', label='Rich Text Format')
412
413 m.pack(fill=Tix.X, padx=5, pady=3)
414
415def MkFileEnt(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000416 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000417 relief=Tix.FLAT, width=240, anchor=Tix.N,
418 text='Press the "open file" icon button and a TixFileSelectDialog will popup.')
419 ent = Tix.FileEntry(w, label='Select a file : ')
420 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
421 ent.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
422
423def MkFileBox(w):
Martin v. Löwis8ec03e02002-03-17 18:19:13 +0000424 """The FileSelectBox is a Motif-style box with various enhancements.
425 For example, you can adjust the size of the two listboxes
426 and your past selections are recorded.
427 """
Martin v. Löwis20efa682001-11-11 14:07:37 +0000428 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000429 relief=Tix.FLAT, width=240, anchor=Tix.N,
Martin v. Löwis8ec03e02002-03-17 18:19:13 +0000430 text='The Tix FileSelectBox 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.')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000431 box = Tix.FileSelectBox(w)
432 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
433 box.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
434
435def MkToolBar(w):
436 global demo
437
438 prefix = Tix.OptionName(w)
439 if not prefix: prefix = ''
440 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000441 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000442 relief=Tix.FLAT, width=240, anchor=Tix.N,
443 text='The Select widget is also good for arranging buttons in a tool bar.')
444 bar = Tix.Frame(w, bd=2, relief=Tix.RAISED)
445 font = Tix.Select(w, allowzero=1, radio=0, label='')
446 para = Tix.Select(w, allowzero=0, radio=1, label='')
447
448 font.add('bold', bitmap='@' + demo.dir + '/bitmaps/bold.xbm')
449 font.add('italic', bitmap='@' + demo.dir + '/bitmaps/italic.xbm')
450 font.add('underline', bitmap='@' + demo.dir + '/bitmaps/underline.xbm')
451 font.add('capital', bitmap='@' + demo.dir + '/bitmaps/capital.xbm')
452
453 para.add('left', bitmap='@' + demo.dir + '/bitmaps/leftj.xbm')
454 para.add('right', bitmap='@' + demo.dir + '/bitmaps/rightj.xbm')
455 para.add('center', bitmap='@' + demo.dir + '/bitmaps/centerj.xbm')
456 para.add('justify', bitmap='@' + demo.dir + '/bitmaps/justify.xbm')
457
458 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
459 bar.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
460 font.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
461 para.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
462
463def MkTitle(w):
464 prefix = Tix.OptionName(w)
465 if not prefix: prefix = ''
466 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000467 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000468 relief=Tix.FLAT, width=240, anchor=Tix.N,
469 text='There are many types of "chooser" widgets that allow the user to input different types of information')
470 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
471
472def MkScroll(nb, name):
473 w = nb.page(name)
474 prefix = Tix.OptionName(w)
475 if not prefix:
476 prefix = ''
477 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
478
479 sls = Tix.LabelFrame(w, label='tixScrolledListBox')
480 swn = Tix.LabelFrame(w, label='tixScrolledWindow')
481 stx = Tix.LabelFrame(w, label='tixScrolledText')
482
483 MkSList(sls.frame)
484 MkSWindow(swn.frame)
485 MkSText(stx.frame)
486
487 sls.form(top=0, left=0, right='%33', bottom=-1)
488 swn.form(top=0, left=sls, right='%66', bottom=-1)
489 stx.form(top=0, left=swn, right=-1, bottom=-1)
490
491def MkSList(w):
492 top = Tix.Frame(w, width=300, height=330)
493 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000494 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000495 relief=Tix.FLAT, width=200, anchor=Tix.N,
496 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.')
497
498 list = Tix.ScrolledListBox(top, scrollbar='auto')
499 list.place(x=50, y=150, width=120, height=80)
500 list.listbox.insert(Tix.END, 'Alabama')
501 list.listbox.insert(Tix.END, 'California')
502 list.listbox.insert(Tix.END, 'Montana')
503 list.listbox.insert(Tix.END, 'New Jersey')
504 list.listbox.insert(Tix.END, 'New York')
505 list.listbox.insert(Tix.END, 'Pennsylvania')
506 list.listbox.insert(Tix.END, 'Washington')
507
508 rh = Tix.ResizeHandle(top, bg='black',
509 relief=Tix.RAISED,
510 handlesize=8, gridded=1, minwidth=50, minheight=30)
511 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=list: SList_reset(w,x))
512 top.propagate(0)
513 msg.pack(fill=Tix.X)
514 btn.pack(anchor=Tix.CENTER)
515 top.pack(expand=1, fill=Tix.BOTH)
516 bot.pack(fill=Tix.BOTH)
517 list.bind('<Map>', func=lambda arg=0, rh=rh, list=list:
518 list.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(list)))
519
520def SList_reset(rh, list):
521 list.place(x=50, y=150, width=120, height=80)
522 list.update()
523 rh.attach_widget(list)
524
Martin v. Löwis652e1912001-11-25 14:50:56 +0000525# See below why this is necessary.
526global image1
527image1 = None
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000528def MkSWindow(w):
Martin v. Löwis652e1912001-11-25 14:50:56 +0000529 global demo, image1
530
531 text = 'The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.'
532
533 file = os.path.join(demo.dir, 'bitmaps', 'tix.gif')
534 if not os.path.isfile(file):
535 text += ' (Image missing)'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000536
537 top = Tix.Frame(w, width=330, height=330)
538 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000539 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000540 relief=Tix.FLAT, width=200, anchor=Tix.N,
Martin v. Löwis652e1912001-11-25 14:50:56 +0000541 text=text)
542
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000543 win = Tix.ScrolledWindow(top, scrollbar='auto')
Martin v. Löwis652e1912001-11-25 14:50:56 +0000544
545 # This image is not showing up under Python unless it is set to a
546 # global variable - no problem under Tcl. I assume it is being garbage
547 # collected some how, even though the tcl command 'image names' shows
548 # that as far as Tcl is concerned, the image exists and is called pyimage1.
549 image1 = Tix.Image('photo', file=file)
550 lbl = Tix.Label(win.window, image=image1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000551 lbl.pack(expand=1, fill=Tix.BOTH)
552
553 win.place(x=30, y=150, width=190, height=120)
554
555 rh = Tix.ResizeHandle(top, bg='black',
556 relief=Tix.RAISED,
557 handlesize=8, gridded=1, minwidth=50, minheight=30)
558 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SWindow_reset(w,x))
559 top.propagate(0)
560 msg.pack(fill=Tix.X)
561 btn.pack(anchor=Tix.CENTER)
562 top.pack(expand=1, fill=Tix.BOTH)
563 bot.pack(fill=Tix.BOTH)
564 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
565 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
566
567def SWindow_reset(rh, win):
568 win.place(x=30, y=150, width=190, height=120)
569 win.update()
570 rh.attach_widget(win)
571
572def MkSText(w):
573 top = Tix.Frame(w, width=330, height=330)
574 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000575 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000576 relief=Tix.FLAT, width=200, anchor=Tix.N,
577 text='The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.')
578
579 win = Tix.ScrolledText(top, scrollbar='auto')
580# win.text['wrap'] = 'none'
581 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.')
582 win.place(x=30, y=150, width=190, height=100)
583
584 rh = Tix.ResizeHandle(top, bg='black',
585 relief=Tix.RAISED,
586 handlesize=8, gridded=1, minwidth=50, minheight=30)
587 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SText_reset(w,x))
588 top.propagate(0)
589 msg.pack(fill=Tix.X)
590 btn.pack(anchor=Tix.CENTER)
591 top.pack(expand=1, fill=Tix.BOTH)
592 bot.pack(fill=Tix.BOTH)
593 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
594 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
595
596def SText_reset(rh, win):
597 win.place(x=30, y=150, width=190, height=120)
598 win.update()
599 rh.attach_widget(win)
600
601def MkManager(nb, name):
602 w = nb.page(name)
603 prefix = Tix.OptionName(w)
604 if not prefix:
605 prefix = ''
606 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
607
608 pane = Tix.LabelFrame(w, label='tixPanedWindow')
609 note = Tix.LabelFrame(w, label='tixNoteBook')
610
611 MkPanedWindow(pane.frame)
612 MkNoteBook(note.frame)
613
614 pane.form(top=0, left=0, right=note, bottom=-1)
615 note.form(top=0, right=-1, bottom=-1)
616
617def MkPanedWindow(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000618 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000619 relief=Tix.FLAT, width=240, anchor=Tix.N,
620 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 +0000621 group = Tix.LabelEntry(w, label='Newsgroup:', options='entry.width 25')
622 group.entry.insert(0,'comp.lang.python')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000623 pane = Tix.PanedWindow(w, orientation='vertical')
624
625 p1 = pane.add('list', min=70, size=100)
626 p2 = pane.add('text', min=70)
627 list = Tix.ScrolledListBox(p1)
628 text = Tix.ScrolledText(p2)
629
Martin v. Löwis652e1912001-11-25 14:50:56 +0000630 list.listbox.insert(Tix.END, " 12324 Re: Tkinter is good for your health")
631 list.listbox.insert(Tix.END, "+ 12325 Re: Tkinter is good for your health")
632 list.listbox.insert(Tix.END, "+ 12326 Re: Tix is even better for your health (Was: Tkinter is good...)")
633 list.listbox.insert(Tix.END, " 12327 Re: Tix is even better for your health (Was: Tkinter is good...)")
634 list.listbox.insert(Tix.END, "+ 12328 Re: Tix is even better for your health (Was: Tkinter is good...)")
635 list.listbox.insert(Tix.END, " 12329 Re: Tix is even better for your health (Was: Tkinter is good...)")
636 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 +0000637
638 text.text['bg'] = list.listbox['bg']
639 text.text['wrap'] = 'none'
640 text.text.insert(Tix.END, """
Martin v. Löwis652e1912001-11-25 14:50:56 +0000641Mon, 19 Jun 1995 11:39:52 comp.lang.python Thread 34 of 220
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000642Lines 353 A new way to put text and bitmaps together iNo responses
643ioi@blue.seas.upenn.edu Ioi K. Lam at University of Pennsylvania
644
645Hi,
646
647I have implemented a new image type called "compound". It allows you
648to glue together a bunch of bitmaps, images and text strings together
649to form a bigger image. Then you can use this image with widgets that
650support the -image option. For example, you can display a text string string
651together with a bitmap, at the same time, inside a TK button widget.
652""")
653 list.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
654 text.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
655
656 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
657 group.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
658 pane.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH, expand=1)
659
660def MkNoteBook(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000661 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000662 relief=Tix.FLAT, width=240, anchor=Tix.N,
663 text='The NoteBook widget allows you to layout a complex interface into individual pages.')
664 prefix = Tix.OptionName(w)
665 if not prefix:
666 prefix = ''
667 w.option_add('*' + prefix + '*TixControl*entry.width', 10)
668 w.option_add('*' + prefix + '*TixControl*label.width', 18)
669 w.option_add('*' + prefix + '*TixControl*label.anchor', Tix.E)
670 w.option_add('*' + prefix + '*TixNoteBook*tagPadX', 8)
671
672 nb = Tix.NoteBook(w, ipadx=6, ipady=6)
673 nb.add('hard_disk', label="Hard Disk", underline=0)
674 nb.add('network', label="Network", underline=0)
675
676 # Frame for the buttons that are present on all pages
677 common = Tix.Frame(nb.hard_disk)
678 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
679 CreateCommonButtons(common)
680
681 # Widgets belonging only to this page
682 a = Tix.Control(nb.hard_disk, value=12, label='Access Time: ')
683 w = Tix.Control(nb.hard_disk, value=400, label='Write Throughput: ')
684 r = Tix.Control(nb.hard_disk, value=400, label='Read Throughput: ')
685 c = Tix.Control(nb.hard_disk, value=1021, label='Capacity: ')
686 a.pack(side=Tix.TOP, padx=20, pady=2)
687 w.pack(side=Tix.TOP, padx=20, pady=2)
688 r.pack(side=Tix.TOP, padx=20, pady=2)
689 c.pack(side=Tix.TOP, padx=20, pady=2)
690
691 common = Tix.Frame(nb.network)
692 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
693 CreateCommonButtons(common)
694
695 a = Tix.Control(nb.network, value=12, label='Access Time: ')
696 w = Tix.Control(nb.network, value=400, label='Write Throughput: ')
697 r = Tix.Control(nb.network, value=400, label='Read Throughput: ')
698 c = Tix.Control(nb.network, value=1021, label='Capacity: ')
699 u = Tix.Control(nb.network, value=10, label='Users: ')
700 a.pack(side=Tix.TOP, padx=20, pady=2)
701 w.pack(side=Tix.TOP, padx=20, pady=2)
702 r.pack(side=Tix.TOP, padx=20, pady=2)
703 c.pack(side=Tix.TOP, padx=20, pady=2)
704 u.pack(side=Tix.TOP, padx=20, pady=2)
705
706 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
707 nb.pack(side=Tix.TOP, padx=5, pady=5, fill=Tix.BOTH, expand=1)
708
709def CreateCommonButtons(f):
710 ok = Tix.Button(f, text='OK', width = 6)
711 cancel = Tix.Button(f, text='Cancel', width = 6)
712 ok.pack(side=Tix.TOP, padx=2, pady=2)
713 cancel.pack(side=Tix.TOP, padx=2, pady=2)
714
715def MkDirList(nb, name):
716 w = nb.page(name)
717 prefix = Tix.OptionName(w)
718 if not prefix:
719 prefix = ''
720 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
721
722 dir = Tix.LabelFrame(w, label='tixDirList')
723 fsbox = Tix.LabelFrame(w, label='tixExFileSelectBox')
724 MkDirListWidget(dir.frame)
725 MkExFileWidget(fsbox.frame)
726 dir.form(top=0, left=0, right='%40', bottom=-1)
727 fsbox.form(top=0, left='%40', right=-1, bottom=-1)
728
729def MkDirListWidget(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000730 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000731 relief=Tix.FLAT, width=240, anchor=Tix.N,
732 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.')
733 dirlist = Tix.DirList(w, options='hlist.padY 1 hlist.width 25 hlist.height 16')
734 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
735 dirlist.pack(side=Tix.TOP, padx=3, pady=3)
736
737def MkExFileWidget(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000738 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000739 relief=Tix.FLAT, width=240, anchor=Tix.N,
740 text='The TixExFileSelectBox widget is more user friendly than the Motif style FileSelectBox.')
741 # There's a bug in the ComboBoxes - the scrolledlistbox is destroyed
742 box = Tix.ExFileSelectBox(w, bd=2, relief=Tix.RAISED)
743 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
744 box.pack(side=Tix.TOP, padx=3, pady=3)
745
746###
747### List of all the demos we want to show off
748comments = {'widget' : 'Widget Demos', 'image' : 'Image Demos'}
749samples = {'Balloon' : 'Balloon',
750 'Button Box' : 'BtnBox',
751 'Combo Box' : 'ComboBox',
752 'Compound Image' : 'CmpImg',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000753 'Directory List' : 'DirList',
754 'Directory Tree' : 'DirTree',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000755 'Control' : 'Control',
756 'Notebook' : 'NoteBook',
757 'Option Menu' : 'OptMenu',
Martin v. Löwis652e1912001-11-25 14:50:56 +0000758 'Paned Window' : 'PanedWin',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000759 'Popup Menu' : 'PopMenu',
760 'ScrolledHList (1)' : 'SHList1',
761 'ScrolledHList (2)' : 'SHList2',
762 'Tree (dynamic)' : 'Tree'
763}
764
Martin v. Löwis20efa682001-11-11 14:07:37 +0000765# There are still a lot of demos to be translated:
766## set root {
767## {d "File Selectors" file }
768## {d "Hierachical ListBox" hlist }
769## {d "Tabular ListBox" tlist {c tixTList}}
770## {d "Grid Widget" grid {c tixGrid}}
771## {d "Manager Widgets" manager }
772## {d "Scrolled Widgets" scroll }
773## {d "Miscellaneous Widgets" misc }
774## {d "Image Types" image }
775## }
776##
777## set image {
778## {d "Compound Image" cmpimg }
779## {d "XPM Image" xpm {i pixmap}}
780## }
781##
782## set cmpimg {
783## {f "In Buttons" CmpImg.tcl }
784## {f "In NoteBook" CmpImg2.tcl }
785## {f "Notebook Color Tabs" CmpImg4.tcl }
786## {f "Icons" CmpImg3.tcl }
787## }
788##
789## set xpm {
790## {f "In Button" Xpm.tcl {i pixmap}}
791## {f "In Menu" Xpm1.tcl {i pixmap}}
792## }
793##
794## set file {
795##added {f DirList DirList.tcl }
796##added {f DirTree DirTree.tcl }
797## {f DirSelectDialog DirDlg.tcl }
798## {f ExFileSelectDialog EFileDlg.tcl }
799## {f FileSelectDialog FileDlg.tcl }
800## {f FileEntry FileEnt.tcl }
801## }
802##
803## set hlist {
804## {f HList HList1.tcl }
805## {f CheckList ChkList.tcl {c tixCheckList}}
806##done {f "ScrolledHList (1)" SHList.tcl }
807##done {f "ScrolledHList (2)" SHList2.tcl }
808##done {f Tree Tree.tcl }
809##done {f "Tree (Dynamic)" DynTree.tcl {v win}}
810## }
811##
812## set tlist {
813## {f "ScrolledTList (1)" STList1.tcl {c tixTList}}
814## {f "ScrolledTList (2)" STList2.tcl {c tixTList}}
815## }
816## global tcl_platform
817## # This demo hangs windows
818## if {$tcl_platform(platform) != "windows"} {
819##na lappend tlist {f "TList File Viewer" STList3.tcl {c tixTList}}
820## }
821##
822## set grid {
823##na {f "Simple Grid" SGrid0.tcl {c tixGrid}}
824##na {f "ScrolledGrid" SGrid1.tcl {c tixGrid}}
825##na {f "Editable Grid" EditGrid.tcl {c tixGrid}}
826## }
827##
828## set scroll {
829## {f ScrolledListBox SListBox.tcl }
830## {f ScrolledText SText.tcl }
831## {f ScrolledWindow SWindow.tcl }
832##na {f "Canvas Object View" CObjView.tcl {c tixCObjView}}
833## }
834##
835## set manager {
836##na {f ListNoteBook ListNBK.tcl }
Martin v. Löwis652e1912001-11-25 14:50:56 +0000837##done {f NoteBook NoteBook.tcl }
838##done {f PanedWindow PanedWin.tcl }
Martin v. Löwis20efa682001-11-11 14:07:37 +0000839## }
840##
841## set misc {
842##done {f Balloon Balloon.tcl }
843##done {f ButtonBox BtnBox.tcl }
844##done {f ComboBox ComboBox.tcl }
845##done {f Control Control.tcl }
846## {f LabelEntry LabEntry.tcl }
847## {f LabelFrame LabFrame.tcl }
848##na {f Meter Meter.tcl {c tixMeter}}
849##done {f OptionMenu OptMenu.tcl }
850##done {f PopupMenu PopMenu.tcl }
851## {f Select Select.tcl }
852## {f StdButtonBox StdBBox.tcl }
853## }
854##
855
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000856stypes = {}
857stypes['widget'] = ['Balloon', 'Button Box', 'Combo Box', 'Control',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000858 'Directory List', 'Directory Tree',
Martin v. Löwis652e1912001-11-25 14:50:56 +0000859 'Notebook', 'Option Menu', 'Popup Menu', 'Paned Window',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000860 'ScrolledHList (1)', 'ScrolledHList (2)', 'Tree (dynamic)']
861stypes['image'] = ['Compound Image']
862
863def MkSample(nb, name):
864 w = nb.page(name)
865 prefix = Tix.OptionName(w)
866 if not prefix:
867 prefix = ''
Martin v. Löwis652e1912001-11-25 14:50:56 +0000868 else:
869 prefix = '*' + prefix
870 w.option_add(prefix + '*TixLabelFrame*label.padX', 4)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000871
Martin v. Löwis652e1912001-11-25 14:50:56 +0000872 pane = Tix.PanedWindow(w, orientation='horizontal')
873 pane.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH)
874 f1 = pane.add('list', expand='1')
875 f2 = pane.add('text', expand='5')
876 f1['relief'] = 'flat'
877 f2['relief'] = 'flat'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000878
Martin v. Löwis652e1912001-11-25 14:50:56 +0000879 lab = Tix.Label(f1, text='Select a sample program:', anchor=Tix.W)
880 lab.pack(side=Tix.TOP, expand=0, fill=Tix.X, padx=5, pady=5)
881 lab1 = Tix.Label(f2, text='Source:', anchor=Tix.W)
882 lab1.pack(side=Tix.TOP, expand=0, fill=Tix.X, padx=5, pady=5)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000883
Martin v. Löwis652e1912001-11-25 14:50:56 +0000884 slb = Tix.Tree(f1, options='hlist.width 25')
885 slb.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=5)
886
887 stext = Tix.ScrolledText(f2, name='stext')
Martin v. Löwis20efa682001-11-11 14:07:37 +0000888 font = root.tk.eval('tix option get fixed_font')
889 stext.text.config(font=font)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000890 stext.text.bind('<Up>', lambda w=stext.text: w.yview(scroll='-1 unit'))
891 stext.text.bind('<Down>', lambda w=stext.text: w.yview(scroll='1 unit'))
892 stext.text.bind('<Left>', lambda w=stext.text: w.xview(scroll='-1 unit'))
893 stext.text.bind('<Right>', lambda w=stext.text: w.xview(scroll='1 unit'))
Martin v. Löwis652e1912001-11-25 14:50:56 +0000894 stext.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=7)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000895
Martin v. Löwis652e1912001-11-25 14:50:56 +0000896 frame = Tix.Frame(f2, name='frame')
897 frame.pack(side=Tix.TOP, expand=0, fill=Tix.X, padx=7)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000898
Martin v. Löwis652e1912001-11-25 14:50:56 +0000899 run = Tix.Button(frame, text='Run ...', name='run')
900 view = Tix.Button(frame, text='View Source ...', name='view')
901 run.pack(side=Tix.LEFT, expand=0, fill=Tix.NONE)
902 view.pack(side=Tix.LEFT, expand=0, fill=Tix.NONE)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000903
904 stext.text['bg'] = slb.hlist['bg']
905 stext.text['state'] = 'disabled'
906 stext.text['wrap'] = 'none'
Martin v. Löwis652e1912001-11-25 14:50:56 +0000907 stext.text['width'] = 80
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000908
909 slb.hlist['separator'] = '.'
910 slb.hlist['width'] = 25
911 slb.hlist['drawbranch'] = 0
912 slb.hlist['indent'] = 10
913 slb.hlist['wideselect'] = 1
Martin v. Löwis652e1912001-11-25 14:50:56 +0000914 slb.hlist['command'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'run')
915 slb.hlist['browsecmd'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'browse')
916
917 run['command'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'run')
918 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 +0000919
920 for type in ['widget', 'image']:
921 if type != 'widget':
922 x = Tix.Frame(slb.hlist, bd=2, height=2, width=150,
923 relief=Tix.SUNKEN, bg=slb.hlist['bg'])
924 slb.hlist.add_child(itemtype=Tix.WINDOW, window=x, state='disabled')
925 x = slb.hlist.add_child(itemtype=Tix.TEXT, state='disabled',
926 text=comments[type])
927 for key in stypes[type]:
928 slb.hlist.add_child(x, itemtype=Tix.TEXT, data=key,
929 text=key)
930 slb.hlist.selection_clear()
931
932 run['state'] = 'disabled'
933 view['state'] = 'disabled'
934
Martin v. Löwis652e1912001-11-25 14:50:56 +0000935def Sample_Action(w, slb, stext, run, view, action):
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000936 global demo
937
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000938 hlist = slb.hlist
939 anchor = hlist.info_anchor()
940 if not anchor:
941 run['state'] = 'disabled'
942 view['state'] = 'disabled'
943 elif not hlist.info_parent(anchor):
944 # a comment
945 return
946
947 run['state'] = 'normal'
948 view['state'] = 'normal'
949 key = hlist.info_data(anchor)
950 title = key
951 prog = samples[key]
952
953 if action == 'run':
954 exec('import ' + prog)
955 w = Tix.Toplevel()
956 w.title(title)
957 rtn = eval(prog + '.RunSample')
958 rtn(w)
959 elif action == 'view':
960 w = Tix.Toplevel()
961 w.title('Source view: ' + title)
962 LoadFile(w, demo.dir + '/samples/' + prog + '.py')
963 elif action == 'browse':
964 ReadFile(stext.text, demo.dir + '/samples/' + prog + '.py')
965
966def LoadFile(w, fname):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000967 global root
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000968 b = Tix.Button(w, text='Close', command=w.destroy)
969 t = Tix.ScrolledText(w)
970 # b.form(left=0, bottom=0, padx=4, pady=4)
971 # t.form(left=0, bottom=b, right='-0', top=0)
972 t.pack()
973 b.pack()
974
Martin v. Löwis20efa682001-11-11 14:07:37 +0000975 font = root.tk.eval('tix option get fixed_font')
976 t.text.config(font=font)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000977 t.text['bd'] = 2
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000978 t.text['wrap'] = 'none'
979
980 ReadFile(t.text, fname)
981
982def ReadFile(w, fname):
983 old_state = w['state']
984 w['state'] = 'normal'
985 w.delete('0.0', Tix.END)
986
987 try:
988 f = open(fname)
989 lines = f.readlines()
990 for s in lines:
991 w.insert(Tix.END, s)
992 f.close()
993 finally:
994# w.see('1.0')
995 w['state'] = old_state
996
997if __name__ == '__main__':
Martin v. Löwis20efa682001-11-11 14:07:37 +0000998 root = Tix.Tk()
999 RunMain(root)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00001000