blob: 68cf56ecf1578d3aeb8fa1e51191b869f391097a [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:
Martin v. Löwis20efa682001-11-11 14:07:37 +0000138 while self.exit < 0:
139 self.root.tk.dooneevent(TCL_ALL_EVENTS)
Martin v. Löwis652e1912001-11-25 14:50:56 +0000140 except SystemExit:
141 #print 'Exit'
142 self.exit = 1
143 break
144 except KeyboardInterrupt:
145 if tkMessageBox.askquestion ('Interrupt', 'Really Quit?') == 'yes':
146 # self.tk.eval('exit')
147 return
148 else:
149 pass
150 continue
151 except:
152 t, v, tb = sys.exc_info()
153 text = ""
154 for line in traceback.format_exception(t,v,tb):
155 text += line + '\n'
156 try: tkMessageBox.showerror ('Error', text)
157 except: pass
158 tkinspect_quit (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öwis20efa682001-11-11 14:07:37 +0000424 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000425 relief=Tix.FLAT, width=240, anchor=Tix.N,
426 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.')
427 box = Tix.FileSelectBox(w)
428 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
429 box.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
430
431def MkToolBar(w):
432 global demo
433
434 prefix = Tix.OptionName(w)
435 if not prefix: prefix = ''
436 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000437 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000438 relief=Tix.FLAT, width=240, anchor=Tix.N,
439 text='The Select widget is also good for arranging buttons in a tool bar.')
440 bar = Tix.Frame(w, bd=2, relief=Tix.RAISED)
441 font = Tix.Select(w, allowzero=1, radio=0, label='')
442 para = Tix.Select(w, allowzero=0, radio=1, label='')
443
444 font.add('bold', bitmap='@' + demo.dir + '/bitmaps/bold.xbm')
445 font.add('italic', bitmap='@' + demo.dir + '/bitmaps/italic.xbm')
446 font.add('underline', bitmap='@' + demo.dir + '/bitmaps/underline.xbm')
447 font.add('capital', bitmap='@' + demo.dir + '/bitmaps/capital.xbm')
448
449 para.add('left', bitmap='@' + demo.dir + '/bitmaps/leftj.xbm')
450 para.add('right', bitmap='@' + demo.dir + '/bitmaps/rightj.xbm')
451 para.add('center', bitmap='@' + demo.dir + '/bitmaps/centerj.xbm')
452 para.add('justify', bitmap='@' + demo.dir + '/bitmaps/justify.xbm')
453
454 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
455 bar.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
456 font.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
457 para.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
458
459def MkTitle(w):
460 prefix = Tix.OptionName(w)
461 if not prefix: prefix = ''
462 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000463 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000464 relief=Tix.FLAT, width=240, anchor=Tix.N,
465 text='There are many types of "chooser" widgets that allow the user to input different types of information')
466 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
467
468def MkScroll(nb, name):
469 w = nb.page(name)
470 prefix = Tix.OptionName(w)
471 if not prefix:
472 prefix = ''
473 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
474
475 sls = Tix.LabelFrame(w, label='tixScrolledListBox')
476 swn = Tix.LabelFrame(w, label='tixScrolledWindow')
477 stx = Tix.LabelFrame(w, label='tixScrolledText')
478
479 MkSList(sls.frame)
480 MkSWindow(swn.frame)
481 MkSText(stx.frame)
482
483 sls.form(top=0, left=0, right='%33', bottom=-1)
484 swn.form(top=0, left=sls, right='%66', bottom=-1)
485 stx.form(top=0, left=swn, right=-1, bottom=-1)
486
487def MkSList(w):
488 top = Tix.Frame(w, width=300, height=330)
489 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000490 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000491 relief=Tix.FLAT, width=200, anchor=Tix.N,
492 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.')
493
494 list = Tix.ScrolledListBox(top, scrollbar='auto')
495 list.place(x=50, y=150, width=120, height=80)
496 list.listbox.insert(Tix.END, 'Alabama')
497 list.listbox.insert(Tix.END, 'California')
498 list.listbox.insert(Tix.END, 'Montana')
499 list.listbox.insert(Tix.END, 'New Jersey')
500 list.listbox.insert(Tix.END, 'New York')
501 list.listbox.insert(Tix.END, 'Pennsylvania')
502 list.listbox.insert(Tix.END, 'Washington')
503
504 rh = Tix.ResizeHandle(top, bg='black',
505 relief=Tix.RAISED,
506 handlesize=8, gridded=1, minwidth=50, minheight=30)
507 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=list: SList_reset(w,x))
508 top.propagate(0)
509 msg.pack(fill=Tix.X)
510 btn.pack(anchor=Tix.CENTER)
511 top.pack(expand=1, fill=Tix.BOTH)
512 bot.pack(fill=Tix.BOTH)
513 list.bind('<Map>', func=lambda arg=0, rh=rh, list=list:
514 list.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(list)))
515
516def SList_reset(rh, list):
517 list.place(x=50, y=150, width=120, height=80)
518 list.update()
519 rh.attach_widget(list)
520
Martin v. Löwis652e1912001-11-25 14:50:56 +0000521# See below why this is necessary.
522global image1
523image1 = None
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000524def MkSWindow(w):
Martin v. Löwis652e1912001-11-25 14:50:56 +0000525 global demo, image1
526
527 text = 'The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.'
528
529 file = os.path.join(demo.dir, 'bitmaps', 'tix.gif')
530 if not os.path.isfile(file):
531 text += ' (Image missing)'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000532
533 top = Tix.Frame(w, width=330, height=330)
534 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000535 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000536 relief=Tix.FLAT, width=200, anchor=Tix.N,
Martin v. Löwis652e1912001-11-25 14:50:56 +0000537 text=text)
538
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000539 win = Tix.ScrolledWindow(top, scrollbar='auto')
Martin v. Löwis652e1912001-11-25 14:50:56 +0000540
541 # This image is not showing up under Python unless it is set to a
542 # global variable - no problem under Tcl. I assume it is being garbage
543 # collected some how, even though the tcl command 'image names' shows
544 # that as far as Tcl is concerned, the image exists and is called pyimage1.
545 image1 = Tix.Image('photo', file=file)
546 lbl = Tix.Label(win.window, image=image1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000547 lbl.pack(expand=1, fill=Tix.BOTH)
548
549 win.place(x=30, y=150, width=190, height=120)
550
551 rh = Tix.ResizeHandle(top, bg='black',
552 relief=Tix.RAISED,
553 handlesize=8, gridded=1, minwidth=50, minheight=30)
554 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SWindow_reset(w,x))
555 top.propagate(0)
556 msg.pack(fill=Tix.X)
557 btn.pack(anchor=Tix.CENTER)
558 top.pack(expand=1, fill=Tix.BOTH)
559 bot.pack(fill=Tix.BOTH)
560 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
561 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
562
563def SWindow_reset(rh, win):
564 win.place(x=30, y=150, width=190, height=120)
565 win.update()
566 rh.attach_widget(win)
567
568def MkSText(w):
569 top = Tix.Frame(w, width=330, height=330)
570 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000571 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000572 relief=Tix.FLAT, width=200, anchor=Tix.N,
573 text='The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.')
574
575 win = Tix.ScrolledText(top, scrollbar='auto')
576# win.text['wrap'] = 'none'
577 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.')
578 win.place(x=30, y=150, width=190, height=100)
579
580 rh = Tix.ResizeHandle(top, bg='black',
581 relief=Tix.RAISED,
582 handlesize=8, gridded=1, minwidth=50, minheight=30)
583 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SText_reset(w,x))
584 top.propagate(0)
585 msg.pack(fill=Tix.X)
586 btn.pack(anchor=Tix.CENTER)
587 top.pack(expand=1, fill=Tix.BOTH)
588 bot.pack(fill=Tix.BOTH)
589 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
590 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
591
592def SText_reset(rh, win):
593 win.place(x=30, y=150, width=190, height=120)
594 win.update()
595 rh.attach_widget(win)
596
597def MkManager(nb, name):
598 w = nb.page(name)
599 prefix = Tix.OptionName(w)
600 if not prefix:
601 prefix = ''
602 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
603
604 pane = Tix.LabelFrame(w, label='tixPanedWindow')
605 note = Tix.LabelFrame(w, label='tixNoteBook')
606
607 MkPanedWindow(pane.frame)
608 MkNoteBook(note.frame)
609
610 pane.form(top=0, left=0, right=note, bottom=-1)
611 note.form(top=0, right=-1, bottom=-1)
612
613def MkPanedWindow(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000614 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000615 relief=Tix.FLAT, width=240, anchor=Tix.N,
616 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 +0000617 group = Tix.LabelEntry(w, label='Newsgroup:', options='entry.width 25')
618 group.entry.insert(0,'comp.lang.python')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000619 pane = Tix.PanedWindow(w, orientation='vertical')
620
621 p1 = pane.add('list', min=70, size=100)
622 p2 = pane.add('text', min=70)
623 list = Tix.ScrolledListBox(p1)
624 text = Tix.ScrolledText(p2)
625
Martin v. Löwis652e1912001-11-25 14:50:56 +0000626 list.listbox.insert(Tix.END, " 12324 Re: Tkinter is good for your health")
627 list.listbox.insert(Tix.END, "+ 12325 Re: Tkinter is good for your health")
628 list.listbox.insert(Tix.END, "+ 12326 Re: Tix is even better for your health (Was: Tkinter is good...)")
629 list.listbox.insert(Tix.END, " 12327 Re: Tix is even better for your health (Was: Tkinter is good...)")
630 list.listbox.insert(Tix.END, "+ 12328 Re: Tix is even better for your health (Was: Tkinter is good...)")
631 list.listbox.insert(Tix.END, " 12329 Re: Tix is even better for your health (Was: Tkinter is good...)")
632 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 +0000633
634 text.text['bg'] = list.listbox['bg']
635 text.text['wrap'] = 'none'
636 text.text.insert(Tix.END, """
Martin v. Löwis652e1912001-11-25 14:50:56 +0000637Mon, 19 Jun 1995 11:39:52 comp.lang.python Thread 34 of 220
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000638Lines 353 A new way to put text and bitmaps together iNo responses
639ioi@blue.seas.upenn.edu Ioi K. Lam at University of Pennsylvania
640
641Hi,
642
643I have implemented a new image type called "compound". It allows you
644to glue together a bunch of bitmaps, images and text strings together
645to form a bigger image. Then you can use this image with widgets that
646support the -image option. For example, you can display a text string string
647together with a bitmap, at the same time, inside a TK button widget.
648""")
649 list.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
650 text.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
651
652 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
653 group.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
654 pane.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH, expand=1)
655
656def MkNoteBook(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000657 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000658 relief=Tix.FLAT, width=240, anchor=Tix.N,
659 text='The NoteBook widget allows you to layout a complex interface into individual pages.')
660 prefix = Tix.OptionName(w)
661 if not prefix:
662 prefix = ''
663 w.option_add('*' + prefix + '*TixControl*entry.width', 10)
664 w.option_add('*' + prefix + '*TixControl*label.width', 18)
665 w.option_add('*' + prefix + '*TixControl*label.anchor', Tix.E)
666 w.option_add('*' + prefix + '*TixNoteBook*tagPadX', 8)
667
668 nb = Tix.NoteBook(w, ipadx=6, ipady=6)
669 nb.add('hard_disk', label="Hard Disk", underline=0)
670 nb.add('network', label="Network", underline=0)
671
672 # Frame for the buttons that are present on all pages
673 common = Tix.Frame(nb.hard_disk)
674 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
675 CreateCommonButtons(common)
676
677 # Widgets belonging only to this page
678 a = Tix.Control(nb.hard_disk, value=12, label='Access Time: ')
679 w = Tix.Control(nb.hard_disk, value=400, label='Write Throughput: ')
680 r = Tix.Control(nb.hard_disk, value=400, label='Read Throughput: ')
681 c = Tix.Control(nb.hard_disk, value=1021, label='Capacity: ')
682 a.pack(side=Tix.TOP, padx=20, pady=2)
683 w.pack(side=Tix.TOP, padx=20, pady=2)
684 r.pack(side=Tix.TOP, padx=20, pady=2)
685 c.pack(side=Tix.TOP, padx=20, pady=2)
686
687 common = Tix.Frame(nb.network)
688 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
689 CreateCommonButtons(common)
690
691 a = Tix.Control(nb.network, value=12, label='Access Time: ')
692 w = Tix.Control(nb.network, value=400, label='Write Throughput: ')
693 r = Tix.Control(nb.network, value=400, label='Read Throughput: ')
694 c = Tix.Control(nb.network, value=1021, label='Capacity: ')
695 u = Tix.Control(nb.network, value=10, label='Users: ')
696 a.pack(side=Tix.TOP, padx=20, pady=2)
697 w.pack(side=Tix.TOP, padx=20, pady=2)
698 r.pack(side=Tix.TOP, padx=20, pady=2)
699 c.pack(side=Tix.TOP, padx=20, pady=2)
700 u.pack(side=Tix.TOP, padx=20, pady=2)
701
702 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
703 nb.pack(side=Tix.TOP, padx=5, pady=5, fill=Tix.BOTH, expand=1)
704
705def CreateCommonButtons(f):
706 ok = Tix.Button(f, text='OK', width = 6)
707 cancel = Tix.Button(f, text='Cancel', width = 6)
708 ok.pack(side=Tix.TOP, padx=2, pady=2)
709 cancel.pack(side=Tix.TOP, padx=2, pady=2)
710
711def MkDirList(nb, name):
712 w = nb.page(name)
713 prefix = Tix.OptionName(w)
714 if not prefix:
715 prefix = ''
716 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
717
718 dir = Tix.LabelFrame(w, label='tixDirList')
719 fsbox = Tix.LabelFrame(w, label='tixExFileSelectBox')
720 MkDirListWidget(dir.frame)
721 MkExFileWidget(fsbox.frame)
722 dir.form(top=0, left=0, right='%40', bottom=-1)
723 fsbox.form(top=0, left='%40', right=-1, bottom=-1)
724
725def MkDirListWidget(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000726 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000727 relief=Tix.FLAT, width=240, anchor=Tix.N,
728 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.')
729 dirlist = Tix.DirList(w, options='hlist.padY 1 hlist.width 25 hlist.height 16')
730 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
731 dirlist.pack(side=Tix.TOP, padx=3, pady=3)
732
733def MkExFileWidget(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000734 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000735 relief=Tix.FLAT, width=240, anchor=Tix.N,
736 text='The TixExFileSelectBox widget is more user friendly than the Motif style FileSelectBox.')
737 # There's a bug in the ComboBoxes - the scrolledlistbox is destroyed
738 box = Tix.ExFileSelectBox(w, bd=2, relief=Tix.RAISED)
739 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
740 box.pack(side=Tix.TOP, padx=3, pady=3)
741
742###
743### List of all the demos we want to show off
744comments = {'widget' : 'Widget Demos', 'image' : 'Image Demos'}
745samples = {'Balloon' : 'Balloon',
746 'Button Box' : 'BtnBox',
747 'Combo Box' : 'ComboBox',
748 'Compound Image' : 'CmpImg',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000749 'Directory List' : 'DirList',
750 'Directory Tree' : 'DirTree',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000751 'Control' : 'Control',
752 'Notebook' : 'NoteBook',
753 'Option Menu' : 'OptMenu',
Martin v. Löwis652e1912001-11-25 14:50:56 +0000754 'Paned Window' : 'PanedWin',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000755 'Popup Menu' : 'PopMenu',
756 'ScrolledHList (1)' : 'SHList1',
757 'ScrolledHList (2)' : 'SHList2',
758 'Tree (dynamic)' : 'Tree'
759}
760
Martin v. Löwis20efa682001-11-11 14:07:37 +0000761# There are still a lot of demos to be translated:
762## set root {
763## {d "File Selectors" file }
764## {d "Hierachical ListBox" hlist }
765## {d "Tabular ListBox" tlist {c tixTList}}
766## {d "Grid Widget" grid {c tixGrid}}
767## {d "Manager Widgets" manager }
768## {d "Scrolled Widgets" scroll }
769## {d "Miscellaneous Widgets" misc }
770## {d "Image Types" image }
771## }
772##
773## set image {
774## {d "Compound Image" cmpimg }
775## {d "XPM Image" xpm {i pixmap}}
776## }
777##
778## set cmpimg {
779## {f "In Buttons" CmpImg.tcl }
780## {f "In NoteBook" CmpImg2.tcl }
781## {f "Notebook Color Tabs" CmpImg4.tcl }
782## {f "Icons" CmpImg3.tcl }
783## }
784##
785## set xpm {
786## {f "In Button" Xpm.tcl {i pixmap}}
787## {f "In Menu" Xpm1.tcl {i pixmap}}
788## }
789##
790## set file {
791##added {f DirList DirList.tcl }
792##added {f DirTree DirTree.tcl }
793## {f DirSelectDialog DirDlg.tcl }
794## {f ExFileSelectDialog EFileDlg.tcl }
795## {f FileSelectDialog FileDlg.tcl }
796## {f FileEntry FileEnt.tcl }
797## }
798##
799## set hlist {
800## {f HList HList1.tcl }
801## {f CheckList ChkList.tcl {c tixCheckList}}
802##done {f "ScrolledHList (1)" SHList.tcl }
803##done {f "ScrolledHList (2)" SHList2.tcl }
804##done {f Tree Tree.tcl }
805##done {f "Tree (Dynamic)" DynTree.tcl {v win}}
806## }
807##
808## set tlist {
809## {f "ScrolledTList (1)" STList1.tcl {c tixTList}}
810## {f "ScrolledTList (2)" STList2.tcl {c tixTList}}
811## }
812## global tcl_platform
813## # This demo hangs windows
814## if {$tcl_platform(platform) != "windows"} {
815##na lappend tlist {f "TList File Viewer" STList3.tcl {c tixTList}}
816## }
817##
818## set grid {
819##na {f "Simple Grid" SGrid0.tcl {c tixGrid}}
820##na {f "ScrolledGrid" SGrid1.tcl {c tixGrid}}
821##na {f "Editable Grid" EditGrid.tcl {c tixGrid}}
822## }
823##
824## set scroll {
825## {f ScrolledListBox SListBox.tcl }
826## {f ScrolledText SText.tcl }
827## {f ScrolledWindow SWindow.tcl }
828##na {f "Canvas Object View" CObjView.tcl {c tixCObjView}}
829## }
830##
831## set manager {
832##na {f ListNoteBook ListNBK.tcl }
Martin v. Löwis652e1912001-11-25 14:50:56 +0000833##done {f NoteBook NoteBook.tcl }
834##done {f PanedWindow PanedWin.tcl }
Martin v. Löwis20efa682001-11-11 14:07:37 +0000835## }
836##
837## set misc {
838##done {f Balloon Balloon.tcl }
839##done {f ButtonBox BtnBox.tcl }
840##done {f ComboBox ComboBox.tcl }
841##done {f Control Control.tcl }
842## {f LabelEntry LabEntry.tcl }
843## {f LabelFrame LabFrame.tcl }
844##na {f Meter Meter.tcl {c tixMeter}}
845##done {f OptionMenu OptMenu.tcl }
846##done {f PopupMenu PopMenu.tcl }
847## {f Select Select.tcl }
848## {f StdButtonBox StdBBox.tcl }
849## }
850##
851
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000852stypes = {}
853stypes['widget'] = ['Balloon', 'Button Box', 'Combo Box', 'Control',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000854 'Directory List', 'Directory Tree',
Martin v. Löwis652e1912001-11-25 14:50:56 +0000855 'Notebook', 'Option Menu', 'Popup Menu', 'Paned Window',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000856 'ScrolledHList (1)', 'ScrolledHList (2)', 'Tree (dynamic)']
857stypes['image'] = ['Compound Image']
858
859def MkSample(nb, name):
860 w = nb.page(name)
861 prefix = Tix.OptionName(w)
862 if not prefix:
863 prefix = ''
Martin v. Löwis652e1912001-11-25 14:50:56 +0000864 else:
865 prefix = '*' + prefix
866 w.option_add(prefix + '*TixLabelFrame*label.padX', 4)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000867
Martin v. Löwis652e1912001-11-25 14:50:56 +0000868 pane = Tix.PanedWindow(w, orientation='horizontal')
869 pane.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH)
870 f1 = pane.add('list', expand='1')
871 f2 = pane.add('text', expand='5')
872 f1['relief'] = 'flat'
873 f2['relief'] = 'flat'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000874
Martin v. Löwis652e1912001-11-25 14:50:56 +0000875 lab = Tix.Label(f1, text='Select a sample program:', anchor=Tix.W)
876 lab.pack(side=Tix.TOP, expand=0, fill=Tix.X, padx=5, pady=5)
877 lab1 = Tix.Label(f2, text='Source:', anchor=Tix.W)
878 lab1.pack(side=Tix.TOP, expand=0, fill=Tix.X, padx=5, pady=5)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000879
Martin v. Löwis652e1912001-11-25 14:50:56 +0000880 slb = Tix.Tree(f1, options='hlist.width 25')
881 slb.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=5)
882
883 stext = Tix.ScrolledText(f2, name='stext')
Martin v. Löwis20efa682001-11-11 14:07:37 +0000884 font = root.tk.eval('tix option get fixed_font')
885 stext.text.config(font=font)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000886 stext.text.bind('<Up>', lambda w=stext.text: w.yview(scroll='-1 unit'))
887 stext.text.bind('<Down>', lambda w=stext.text: w.yview(scroll='1 unit'))
888 stext.text.bind('<Left>', lambda w=stext.text: w.xview(scroll='-1 unit'))
889 stext.text.bind('<Right>', lambda w=stext.text: w.xview(scroll='1 unit'))
Martin v. Löwis652e1912001-11-25 14:50:56 +0000890 stext.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=7)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000891
Martin v. Löwis652e1912001-11-25 14:50:56 +0000892 frame = Tix.Frame(f2, name='frame')
893 frame.pack(side=Tix.TOP, expand=0, fill=Tix.X, padx=7)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000894
Martin v. Löwis652e1912001-11-25 14:50:56 +0000895 run = Tix.Button(frame, text='Run ...', name='run')
896 view = Tix.Button(frame, text='View Source ...', name='view')
897 run.pack(side=Tix.LEFT, expand=0, fill=Tix.NONE)
898 view.pack(side=Tix.LEFT, expand=0, fill=Tix.NONE)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000899
900 stext.text['bg'] = slb.hlist['bg']
901 stext.text['state'] = 'disabled'
902 stext.text['wrap'] = 'none'
Martin v. Löwis652e1912001-11-25 14:50:56 +0000903 stext.text['width'] = 80
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000904
905 slb.hlist['separator'] = '.'
906 slb.hlist['width'] = 25
907 slb.hlist['drawbranch'] = 0
908 slb.hlist['indent'] = 10
909 slb.hlist['wideselect'] = 1
Martin v. Löwis652e1912001-11-25 14:50:56 +0000910 slb.hlist['command'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'run')
911 slb.hlist['browsecmd'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'browse')
912
913 run['command'] = lambda args=0, w=w,slb=slb,stext=stext,run=run,view=view: Sample_Action(w, slb, stext, run, view, 'run')
914 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 +0000915
916 for type in ['widget', 'image']:
917 if type != 'widget':
918 x = Tix.Frame(slb.hlist, bd=2, height=2, width=150,
919 relief=Tix.SUNKEN, bg=slb.hlist['bg'])
920 slb.hlist.add_child(itemtype=Tix.WINDOW, window=x, state='disabled')
921 x = slb.hlist.add_child(itemtype=Tix.TEXT, state='disabled',
922 text=comments[type])
923 for key in stypes[type]:
924 slb.hlist.add_child(x, itemtype=Tix.TEXT, data=key,
925 text=key)
926 slb.hlist.selection_clear()
927
928 run['state'] = 'disabled'
929 view['state'] = 'disabled'
930
Martin v. Löwis652e1912001-11-25 14:50:56 +0000931def Sample_Action(w, slb, stext, run, view, action):
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000932 global demo
933
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000934 hlist = slb.hlist
935 anchor = hlist.info_anchor()
936 if not anchor:
937 run['state'] = 'disabled'
938 view['state'] = 'disabled'
939 elif not hlist.info_parent(anchor):
940 # a comment
941 return
942
943 run['state'] = 'normal'
944 view['state'] = 'normal'
945 key = hlist.info_data(anchor)
946 title = key
947 prog = samples[key]
948
949 if action == 'run':
950 exec('import ' + prog)
951 w = Tix.Toplevel()
952 w.title(title)
953 rtn = eval(prog + '.RunSample')
954 rtn(w)
955 elif action == 'view':
956 w = Tix.Toplevel()
957 w.title('Source view: ' + title)
958 LoadFile(w, demo.dir + '/samples/' + prog + '.py')
959 elif action == 'browse':
960 ReadFile(stext.text, demo.dir + '/samples/' + prog + '.py')
961
962def LoadFile(w, fname):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000963 global root
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000964 b = Tix.Button(w, text='Close', command=w.destroy)
965 t = Tix.ScrolledText(w)
966 # b.form(left=0, bottom=0, padx=4, pady=4)
967 # t.form(left=0, bottom=b, right='-0', top=0)
968 t.pack()
969 b.pack()
970
Martin v. Löwis20efa682001-11-11 14:07:37 +0000971 font = root.tk.eval('tix option get fixed_font')
972 t.text.config(font=font)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000973 t.text['bd'] = 2
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000974 t.text['wrap'] = 'none'
975
976 ReadFile(t.text, fname)
977
978def ReadFile(w, fname):
979 old_state = w['state']
980 w['state'] = 'normal'
981 w.delete('0.0', Tix.END)
982
983 try:
984 f = open(fname)
985 lines = f.readlines()
986 for s in lines:
987 w.insert(Tix.END, s)
988 f.close()
989 finally:
990# w.see('1.0')
991 w['state'] = old_state
992
993if __name__ == '__main__':
Martin v. Löwis20efa682001-11-11 14:07:37 +0000994 root = Tix.Tk()
995 RunMain(root)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000996