blob: 556d3b7797b1524904cf18f1498f7c9992e2b5c7 [file] [log] [blame]
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00001#! /usr/local/bin/python
2#
3# $Id$
4#
5# tixwidgets.py --
6# This is a demo program of all Tix widgets available from Python. If
7# you have installed Python & Tix properly, you can execute this as
8#
9# % tixwidget.py
10#
11
12import os, sys, Tix
13
14class Demo:
15 pass
16
17root = Tix.Tk()
18
19demo = Demo()
20demo.dir = None # script directory
21demo.balloon = None # balloon widget
22demo.useBalloons = Tix.StringVar()
23demo.useBalloons.set('0')
24demo.statusbar = None # status bar widget
25demo.welmsg = None # Msg widget
26demo.welfont = '' # font name
27demo.welsize = '' # font size
28
29def main():
30 global demo, root
31
32 progname = sys.argv[0]
33 dirname = os.path.dirname(progname)
34 if dirname and dirname != os.curdir:
35 demo.dir = dirname
36 index = -1
37 for i in range(len(sys.path)):
38 p = sys.path[i]
39 if p in ("", os.curdir):
40 index = i
41 if index >= 0:
42 sys.path[index] = dirname
43 else:
44 sys.path.insert(0, dirname)
45 else:
46 demo.dir = os.getcwd()
47 sys.path.insert(0, demo.dir+'/samples')
48
49 root.withdraw()
50 root = Tix.Toplevel()
51 root.title('Tix Widget Demonstration')
52 root.geometry('780x570+50+50')
53
54 demo.balloon = Tix.Balloon(root)
55 frame1 = MkMainMenu(root)
56 frame2 = MkMainNotebook(root)
57 frame3 = MkMainStatus(root)
58 frame1.pack(side=Tix.TOP, fill=Tix.X)
59 frame3.pack(side=Tix.BOTTOM, fill=Tix.X)
60 frame2.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=4, pady=4)
61 demo.balloon['statusbar'] = demo.statusbar
62 root.mainloop()
63
64def exit_cmd(event=None):
65 sys.exit()
66
67def MkMainMenu(top):
68 global demo
69
70 w = Tix.Frame(top, bd=2, relief=Tix.RAISED)
71 file = Tix.Menubutton(w, text='File', underline=0, takefocus=0)
72 help = Tix.Menubutton(w, text='Help', underline=0, takefocus=0)
73 file.pack(side=Tix.LEFT)
74 help.pack(side=Tix.RIGHT)
75 fm = Tix.Menu(file)
76 file['menu'] = fm
77 hm = Tix.Menu(help)
78 help['menu'] = hm
79
80 fm.add_command(label='Exit', underline=1, accelerator='Ctrl+X',
81 command=exit_cmd)
82 hm.add_checkbutton(label='BalloonHelp', underline=0, command=ToggleHelp,
83 variable=demo.useBalloons)
84 # The trace variable option doesn't seem to work, instead I use 'command'
85 #apply(w.tk.call, ('trace', 'variable', demo.useBalloons, 'w',
86 # ToggleHelp))
87 top.bind_all("<Control-x>", exit_cmd)
88 top.bind_all("<Control-X>", exit_cmd)
89 return w
90
91def MkMainNotebook(top):
92 top.option_add('*TixNoteBook*tagPadX', 6)
93 top.option_add('*TixNoteBook*tagPadY', 4)
94 top.option_add('*TixNoteBook*borderWidth', 2)
95 top.option_add('*TixNoteBook*font',
96 '-*-helvetica-bold-o-normal-*-14-*-*-*-*-*-*-*')
97 w = Tix.NoteBook(top, ipadx=5, ipady=5)
98 w.add('wel', label='Welcome', underline=0,
99 createcmd=lambda w=w, name='wel': MkWelcome(w, name))
100 w.add('cho', label='Choosers', underline=0,
101 createcmd=lambda w=w, name='cho': MkChoosers(w, name))
102 w.add('scr', label='Scrolled Widgets', underline=0,
103 createcmd=lambda w=w, name='scr': MkScroll(w, name))
104 w.add('mgr', label='Manager Widgets', underline=0,
105 createcmd=lambda w=w, name='mgr': MkManager(w, name))
106 w.add('dir', label='Directory List', underline=0,
107 createcmd=lambda w=w, name='dir': MkDirList(w, name))
108 w.add('exp', label='Run Sample Programs', underline=0,
109 createcmd=lambda w=w, name='exp': MkSample(w, name))
110 return w
111
112def MkMainStatus(top):
113 global demo
114
115 w = Tix.Frame(top, relief=Tix.RAISED, bd=1)
116 demo.statusbar = Tix.Label(w, relief=Tix.SUNKEN, bd=1, font='-*-helvetica-medium-r-normal-*-14-*-*-*-*-*-*-*')
117 demo.statusbar.form(padx=3, pady=3, left=0, right='%70')
118 return w
119
120def MkWelcome(nb, name):
121 w = nb.page(name)
122 bar = MkWelcomeBar(w)
123 text = MkWelcomeText(w)
124 bar.pack(side=Tix.TOP, fill=Tix.X, padx=2, pady=2)
125 text.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)
126
127def MkWelcomeBar(top):
128 global demo
129
130 w = Tix.Frame(top, bd=2, relief=Tix.GROOVE)
131 b1 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
132 b2 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
133 b1.entry['width'] = 15
134 b1.slistbox.listbox['height'] = 3
135 b2.entry['width'] = 4
136 b2.slistbox.listbox['height'] = 3
137
138 demo.welfont = b1
139 demo.welsize = b2
140
141 b1.insert(Tix.END, 'Courier')
142 b1.insert(Tix.END, 'Helvetica')
143 b1.insert(Tix.END, 'Lucida')
144 b1.insert(Tix.END, 'Times Roman')
145
146 b2.insert(Tix.END, '8')
147 b2.insert(Tix.END, '10')
148 b2.insert(Tix.END, '12')
149 b2.insert(Tix.END, '14')
150 b2.insert(Tix.END, '18')
151
152 b1.pick(1)
153 b2.pick(3)
154
155 b1.pack(side=Tix.LEFT, padx=4, pady=4)
156 b2.pack(side=Tix.LEFT, padx=4, pady=4)
157
158 demo.balloon.bind_widget(b1, msg='Choose\na font',
159 statusmsg='Choose a font for this page')
160 demo.balloon.bind_widget(b2, msg='Point size',
161 statusmsg='Choose the font size for this page')
162 return w
163
164def MkWelcomeText(top):
165 global demo
166
167 w = Tix.ScrolledWindow(top, scrollbar='auto')
168 win = w.window
169 text = 'Welcome to TIX in Python'
170 title = Tix.Label(win, font='-*-times-bold-r-normal-*-18-*-*-*-*-*-*-*',
171 bd=0, width=30, anchor=Tix.N, text=text)
172 msg = Tix.Message(win, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
173 bd=0, width=400, anchor=Tix.N,
174 text='Tix is a set of mega-widgets based on TK. This program \
175demonstrates the widgets in the Tix widget set. You can choose the pages \
176in this window to look at the corresponding widgets. \n\n\
177To quit this program, choose the "File | Exit" command.\n\n\
178For more information, see http://tix.sourceforge.net.')
179 title.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
180 msg.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
181 demo.welmsg = msg
182 return w
183
184def MainTextFont(w):
185 global demo
186
187 if not demo.welmsg:
188 return
189 font = demo.welfont['value']
190 point = demo.welsize['value']
191 if font == 'Times Roman':
192 font = 'times'
193 fontstr = '-*-%s-bold-r-normal-*-%s-*-*-*-*-*-*-*' % (font, point)
194 demo.welmsg['font'] = fontstr
195
196def ToggleHelp():
197 if demo.useBalloons.get() == '1':
198 demo.balloon['state'] = 'both'
199 else:
200 demo.balloon['state'] = 'none'
201
202def MkChoosers(nb, name):
203 w = nb.page(name)
204 prefix = Tix.OptionName(w)
205 if not prefix:
206 prefix = ''
207 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
208
209 til = Tix.LabelFrame(w, label='Chooser Widgets')
210 cbx = Tix.LabelFrame(w, label='tixComboBox')
211 ctl = Tix.LabelFrame(w, label='tixControl')
212 sel = Tix.LabelFrame(w, label='tixSelect')
213 opt = Tix.LabelFrame(w, label='tixOptionMenu')
214 fil = Tix.LabelFrame(w, label='tixFileEntry')
215 fbx = Tix.LabelFrame(w, label='tixFileSelectBox')
216 tbr = Tix.LabelFrame(w, label='Tool Bar')
217
218 MkTitle(til.frame)
219 MkCombo(cbx.frame)
220 MkControl(ctl.frame)
221 MkSelect(sel.frame)
222 MkOptMenu(opt.frame)
223 MkFileEnt(fil.frame)
224 MkFileBox(fbx.frame)
225 MkToolBar(tbr.frame)
226
227 # First column: comBox and selector
228 cbx.form(top=0, left=0, right='%33')
229 sel.form(left=0, right='&'+str(cbx), top=cbx)
230 opt.form(left=0, right='&'+str(cbx), top=sel, bottom=-1)
231
232 # Second column: title .. etc
233 til.form(left=cbx, top=0,right='%66')
234 ctl.form(left=cbx, right='&'+str(til), top=til)
235 fil.form(left=cbx, right='&'+str(til), top=ctl)
236 tbr.form(left=cbx, right='&'+str(til), top=fil, bottom=-1)
237
238 #
239 # Third column: file selection
240 fbx.form(right=-1, top=0, left='%66')
241
242def MkCombo(w):
243 prefix = Tix.OptionName(w)
244 if not prefix: prefix = ''
245 w.option_add('*' + prefix + '*TixComboBox*label.width', 10)
246 w.option_add('*' + prefix + '*TixComboBox*label.anchor', Tix.E)
247 w.option_add('*' + prefix + '*TixComboBox*entry.width', 14)
248
249 static = Tix.ComboBox(w, label='Static', editable=0)
250 editable = Tix.ComboBox(w, label='Editable', editable=1)
251 history = Tix.ComboBox(w, label='History', editable=1, history=1,
252 anchor=Tix.E)
253 static.insert(Tix.END, 'January')
254 static.insert(Tix.END, 'February')
255 static.insert(Tix.END, 'March')
256 static.insert(Tix.END, 'April')
257 static.insert(Tix.END, 'May')
258 static.insert(Tix.END, 'June')
259 static.insert(Tix.END, 'July')
260 static.insert(Tix.END, 'August')
261 static.insert(Tix.END, 'September')
262 static.insert(Tix.END, 'October')
263 static.insert(Tix.END, 'November')
264 static.insert(Tix.END, 'December')
265
266 editable.insert(Tix.END, 'Angola')
267 editable.insert(Tix.END, 'Bangladesh')
268 editable.insert(Tix.END, 'China')
269 editable.insert(Tix.END, 'Denmark')
270 editable.insert(Tix.END, 'Ecuador')
271
272 history.insert(Tix.END, '/usr/bin/ksh')
273 history.insert(Tix.END, '/usr/local/lib/python')
274 history.insert(Tix.END, '/var/adm')
275
276 static.pack(side=Tix.TOP, padx=5, pady=3)
277 editable.pack(side=Tix.TOP, padx=5, pady=3)
278 history.pack(side=Tix.TOP, padx=5, pady=3)
279
280states = ['Bengal', 'Delhi', 'Karnataka', 'Tamil Nadu']
281
282def spin_cmd(w, inc):
283 idx = states.index(demo_spintxt.get()) + inc
284 if idx < 0:
285 idx = len(states) - 1
286 elif idx >= len(states):
287 idx = 0
288# following doesn't work.
289# return states[idx]
290 demo_spintxt.set(states[idx]) # this works
291
292def spin_validate(w):
293 global states, demo_spintxt
294
295 try:
296 i = states.index(demo_spintxt.get())
Fred Drake7def2562001-05-11 19:44:55 +0000297 except ValueError:
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000298 return states[0]
299 return states[i]
300 # why this procedure works as opposed to the previous one beats me.
301
302def MkControl(w):
303 global demo_spintxt
304
305 prefix = Tix.OptionName(w)
306 if not prefix: prefix = ''
307 w.option_add('*' + prefix + '*TixControl*label.width', 10)
308 w.option_add('*' + prefix + '*TixControl*label.anchor', Tix.E)
309 w.option_add('*' + prefix + '*TixControl*entry.width', 13)
310
311 demo_spintxt = Tix.StringVar()
312 demo_spintxt.set(states[0])
313 simple = Tix.Control(w, label='Numbers')
314 spintxt = Tix.Control(w, label='States', variable=demo_spintxt)
315 spintxt['incrcmd'] = lambda w=spintxt: spin_cmd(w, 1)
316 spintxt['decrcmd'] = lambda w=spintxt: spin_cmd(w, -1)
317 spintxt['validatecmd'] = lambda w=spintxt: spin_validate(w)
318
319 simple.pack(side=Tix.TOP, padx=5, pady=3)
320 spintxt.pack(side=Tix.TOP, padx=5, pady=3)
321
322def MkSelect(w):
323 prefix = Tix.OptionName(w)
324 if not prefix: prefix = ''
325 w.option_add('*' + prefix + '*TixSelect*label.anchor', Tix.CENTER)
326 w.option_add('*' + prefix + '*TixSelect*orientation', Tix.VERTICAL)
327 w.option_add('*' + prefix + '*TixSelect*labelSide', Tix.TOP)
328
329 sel1 = Tix.Select(w, label='Mere Mortals', allowzero=1, radio=1)
330 sel2 = Tix.Select(w, label='Geeks', allowzero=1, radio=0)
331
332 sel1.add('eat', text='Eat')
333 sel1.add('work', text='Work')
334 sel1.add('play', text='Play')
335 sel1.add('party', text='Party')
336 sel1.add('sleep', text='Sleep')
337
338 sel2.add('eat', text='Eat')
339 sel2.add('prog1', text='Program')
340 sel2.add('prog2', text='Program')
341 sel2.add('prog3', text='Program')
342 sel2.add('sleep', text='Sleep')
343
344 sel1.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
345 sel2.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
346
347def MkOptMenu(w):
348 prefix = Tix.OptionName(w)
349 if not prefix: prefix = ''
350 w.option_add('*' + prefix + '*TixOptionMenu*label.anchor', Tix.E)
351 m = Tix.OptionMenu(w, label='File Format : ', options='menubutton.width 15')
352 m.add_command('text', label='Plain Text')
353 m.add_command('post', label='PostScript')
354 m.add_command('format', label='Formatted Text')
355 m.add_command('html', label='HTML')
356 m.add_command('sep')
357 m.add_command('tex', label='LaTeX')
358 m.add_command('rtf', label='Rich Text Format')
359
360 m.pack(fill=Tix.X, padx=5, pady=3)
361
362def MkFileEnt(w):
363 msg = Tix.Message(w, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
364 relief=Tix.FLAT, width=240, anchor=Tix.N,
365 text='Press the "open file" icon button and a TixFileSelectDialog will popup.')
366 ent = Tix.FileEntry(w, label='Select a file : ')
367 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
368 ent.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
369
370def MkFileBox(w):
371 msg = Tix.Message(w, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
372 relief=Tix.FLAT, width=240, anchor=Tix.N,
373 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.')
374 box = Tix.FileSelectBox(w)
375 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
376 box.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
377
378def MkToolBar(w):
379 global demo
380
381 prefix = Tix.OptionName(w)
382 if not prefix: prefix = ''
383 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
384 msg = Tix.Message(w, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
385 relief=Tix.FLAT, width=240, anchor=Tix.N,
386 text='The Select widget is also good for arranging buttons in a tool bar.')
387 bar = Tix.Frame(w, bd=2, relief=Tix.RAISED)
388 font = Tix.Select(w, allowzero=1, radio=0, label='')
389 para = Tix.Select(w, allowzero=0, radio=1, label='')
390
391 font.add('bold', bitmap='@' + demo.dir + '/bitmaps/bold.xbm')
392 font.add('italic', bitmap='@' + demo.dir + '/bitmaps/italic.xbm')
393 font.add('underline', bitmap='@' + demo.dir + '/bitmaps/underline.xbm')
394 font.add('capital', bitmap='@' + demo.dir + '/bitmaps/capital.xbm')
395
396 para.add('left', bitmap='@' + demo.dir + '/bitmaps/leftj.xbm')
397 para.add('right', bitmap='@' + demo.dir + '/bitmaps/rightj.xbm')
398 para.add('center', bitmap='@' + demo.dir + '/bitmaps/centerj.xbm')
399 para.add('justify', bitmap='@' + demo.dir + '/bitmaps/justify.xbm')
400
401 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
402 bar.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
403 font.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
404 para.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
405
406def MkTitle(w):
407 prefix = Tix.OptionName(w)
408 if not prefix: prefix = ''
409 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
410 msg = Tix.Message(w, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
411 relief=Tix.FLAT, width=240, anchor=Tix.N,
412 text='There are many types of "chooser" widgets that allow the user to input different types of information')
413 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
414
415def MkScroll(nb, name):
416 w = nb.page(name)
417 prefix = Tix.OptionName(w)
418 if not prefix:
419 prefix = ''
420 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
421
422 sls = Tix.LabelFrame(w, label='tixScrolledListBox')
423 swn = Tix.LabelFrame(w, label='tixScrolledWindow')
424 stx = Tix.LabelFrame(w, label='tixScrolledText')
425
426 MkSList(sls.frame)
427 MkSWindow(swn.frame)
428 MkSText(stx.frame)
429
430 sls.form(top=0, left=0, right='%33', bottom=-1)
431 swn.form(top=0, left=sls, right='%66', bottom=-1)
432 stx.form(top=0, left=swn, right=-1, bottom=-1)
433
434def MkSList(w):
435 top = Tix.Frame(w, width=300, height=330)
436 bot = Tix.Frame(w)
437 msg = Tix.Message(top, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
438 relief=Tix.FLAT, width=200, anchor=Tix.N,
439 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.')
440
441 list = Tix.ScrolledListBox(top, scrollbar='auto')
442 list.place(x=50, y=150, width=120, height=80)
443 list.listbox.insert(Tix.END, 'Alabama')
444 list.listbox.insert(Tix.END, 'California')
445 list.listbox.insert(Tix.END, 'Montana')
446 list.listbox.insert(Tix.END, 'New Jersey')
447 list.listbox.insert(Tix.END, 'New York')
448 list.listbox.insert(Tix.END, 'Pennsylvania')
449 list.listbox.insert(Tix.END, 'Washington')
450
451 rh = Tix.ResizeHandle(top, bg='black',
452 relief=Tix.RAISED,
453 handlesize=8, gridded=1, minwidth=50, minheight=30)
454 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=list: SList_reset(w,x))
455 top.propagate(0)
456 msg.pack(fill=Tix.X)
457 btn.pack(anchor=Tix.CENTER)
458 top.pack(expand=1, fill=Tix.BOTH)
459 bot.pack(fill=Tix.BOTH)
460 list.bind('<Map>', func=lambda arg=0, rh=rh, list=list:
461 list.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(list)))
462
463def SList_reset(rh, list):
464 list.place(x=50, y=150, width=120, height=80)
465 list.update()
466 rh.attach_widget(list)
467
468def MkSWindow(w):
469 global demo
470
471 top = Tix.Frame(w, width=330, height=330)
472 bot = Tix.Frame(w)
473 msg = Tix.Message(top, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
474 relief=Tix.FLAT, width=200, anchor=Tix.N,
475 text='The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.')
476 win = Tix.ScrolledWindow(top, scrollbar='auto')
477 image = Tix.Image('photo', file=demo.dir + "/bitmaps/tix.gif")
478 lbl = Tix.Label(win.window, image=image)
479 lbl.pack(expand=1, fill=Tix.BOTH)
480
481 win.place(x=30, y=150, width=190, height=120)
482
483 rh = Tix.ResizeHandle(top, bg='black',
484 relief=Tix.RAISED,
485 handlesize=8, gridded=1, minwidth=50, minheight=30)
486 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SWindow_reset(w,x))
487 top.propagate(0)
488 msg.pack(fill=Tix.X)
489 btn.pack(anchor=Tix.CENTER)
490 top.pack(expand=1, fill=Tix.BOTH)
491 bot.pack(fill=Tix.BOTH)
492 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
493 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
494
495def SWindow_reset(rh, win):
496 win.place(x=30, y=150, width=190, height=120)
497 win.update()
498 rh.attach_widget(win)
499
500def MkSText(w):
501 top = Tix.Frame(w, width=330, height=330)
502 bot = Tix.Frame(w)
503 msg = Tix.Message(top, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
504 relief=Tix.FLAT, width=200, anchor=Tix.N,
505 text='The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.')
506
507 win = Tix.ScrolledText(top, scrollbar='auto')
508# win.text['wrap'] = 'none'
509 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.')
510 win.place(x=30, y=150, width=190, height=100)
511
512 rh = Tix.ResizeHandle(top, bg='black',
513 relief=Tix.RAISED,
514 handlesize=8, gridded=1, minwidth=50, minheight=30)
515 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SText_reset(w,x))
516 top.propagate(0)
517 msg.pack(fill=Tix.X)
518 btn.pack(anchor=Tix.CENTER)
519 top.pack(expand=1, fill=Tix.BOTH)
520 bot.pack(fill=Tix.BOTH)
521 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
522 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
523
524def SText_reset(rh, win):
525 win.place(x=30, y=150, width=190, height=120)
526 win.update()
527 rh.attach_widget(win)
528
529def MkManager(nb, name):
530 w = nb.page(name)
531 prefix = Tix.OptionName(w)
532 if not prefix:
533 prefix = ''
534 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
535
536 pane = Tix.LabelFrame(w, label='tixPanedWindow')
537 note = Tix.LabelFrame(w, label='tixNoteBook')
538
539 MkPanedWindow(pane.frame)
540 MkNoteBook(note.frame)
541
542 pane.form(top=0, left=0, right=note, bottom=-1)
543 note.form(top=0, right=-1, bottom=-1)
544
545def MkPanedWindow(w):
546 msg = Tix.Message(w, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
547 relief=Tix.FLAT, width=240, anchor=Tix.N,
548 text='The PanedWindow widget allows the user to interactively manipulate the sizes of several panes. The panes can be arranged either vertically or horizontally.')
549 group = Tix.Label(w, text='Newsgroup: comp.lang.python')
550 pane = Tix.PanedWindow(w, orientation='vertical')
551
552 p1 = pane.add('list', min=70, size=100)
553 p2 = pane.add('text', min=70)
554 list = Tix.ScrolledListBox(p1)
555 text = Tix.ScrolledText(p2)
556
557 list.listbox.insert(Tix.END, " 12324 Re: TK is good for your health")
558 list.listbox.insert(Tix.END, "+ 12325 Re: TK is good for your health")
559 list.listbox.insert(Tix.END, "+ 12326 Re: Tix is even better for your health (Was: TK is good...)")
560 list.listbox.insert(Tix.END, " 12327 Re: Tix is even better for your health (Was: TK is good...)")
561 list.listbox.insert(Tix.END, "+ 12328 Re: Tix is even better for your health (Was: TK is good...)")
562 list.listbox.insert(Tix.END, " 12329 Re: Tix is even better for your health (Was: TK is good...)")
563 list.listbox.insert(Tix.END, "+ 12330 Re: Tix is even better for your health (Was: TK is good...)")
564
565 text.text['bg'] = list.listbox['bg']
566 text.text['wrap'] = 'none'
567 text.text.insert(Tix.END, """
568Mon, 19 Jun 1995 11:39:52 comp.lang.tcl Thread 34 of 220
569Lines 353 A new way to put text and bitmaps together iNo responses
570ioi@blue.seas.upenn.edu Ioi K. Lam at University of Pennsylvania
571
572Hi,
573
574I have implemented a new image type called "compound". It allows you
575to glue together a bunch of bitmaps, images and text strings together
576to form a bigger image. Then you can use this image with widgets that
577support the -image option. For example, you can display a text string string
578together with a bitmap, at the same time, inside a TK button widget.
579""")
580 list.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
581 text.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
582
583 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
584 group.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
585 pane.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH, expand=1)
586
587def MkNoteBook(w):
588 msg = Tix.Message(w, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
589 relief=Tix.FLAT, width=240, anchor=Tix.N,
590 text='The NoteBook widget allows you to layout a complex interface into individual pages.')
591 prefix = Tix.OptionName(w)
592 if not prefix:
593 prefix = ''
594 w.option_add('*' + prefix + '*TixControl*entry.width', 10)
595 w.option_add('*' + prefix + '*TixControl*label.width', 18)
596 w.option_add('*' + prefix + '*TixControl*label.anchor', Tix.E)
597 w.option_add('*' + prefix + '*TixNoteBook*tagPadX', 8)
598
599 nb = Tix.NoteBook(w, ipadx=6, ipady=6)
600 nb.add('hard_disk', label="Hard Disk", underline=0)
601 nb.add('network', label="Network", underline=0)
602
603 # Frame for the buttons that are present on all pages
604 common = Tix.Frame(nb.hard_disk)
605 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
606 CreateCommonButtons(common)
607
608 # Widgets belonging only to this page
609 a = Tix.Control(nb.hard_disk, value=12, label='Access Time: ')
610 w = Tix.Control(nb.hard_disk, value=400, label='Write Throughput: ')
611 r = Tix.Control(nb.hard_disk, value=400, label='Read Throughput: ')
612 c = Tix.Control(nb.hard_disk, value=1021, label='Capacity: ')
613 a.pack(side=Tix.TOP, padx=20, pady=2)
614 w.pack(side=Tix.TOP, padx=20, pady=2)
615 r.pack(side=Tix.TOP, padx=20, pady=2)
616 c.pack(side=Tix.TOP, padx=20, pady=2)
617
618 common = Tix.Frame(nb.network)
619 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
620 CreateCommonButtons(common)
621
622 a = Tix.Control(nb.network, value=12, label='Access Time: ')
623 w = Tix.Control(nb.network, value=400, label='Write Throughput: ')
624 r = Tix.Control(nb.network, value=400, label='Read Throughput: ')
625 c = Tix.Control(nb.network, value=1021, label='Capacity: ')
626 u = Tix.Control(nb.network, value=10, label='Users: ')
627 a.pack(side=Tix.TOP, padx=20, pady=2)
628 w.pack(side=Tix.TOP, padx=20, pady=2)
629 r.pack(side=Tix.TOP, padx=20, pady=2)
630 c.pack(side=Tix.TOP, padx=20, pady=2)
631 u.pack(side=Tix.TOP, padx=20, pady=2)
632
633 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
634 nb.pack(side=Tix.TOP, padx=5, pady=5, fill=Tix.BOTH, expand=1)
635
636def CreateCommonButtons(f):
637 ok = Tix.Button(f, text='OK', width = 6)
638 cancel = Tix.Button(f, text='Cancel', width = 6)
639 ok.pack(side=Tix.TOP, padx=2, pady=2)
640 cancel.pack(side=Tix.TOP, padx=2, pady=2)
641
642def MkDirList(nb, name):
643 w = nb.page(name)
644 prefix = Tix.OptionName(w)
645 if not prefix:
646 prefix = ''
647 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
648
649 dir = Tix.LabelFrame(w, label='tixDirList')
650 fsbox = Tix.LabelFrame(w, label='tixExFileSelectBox')
651 MkDirListWidget(dir.frame)
652 MkExFileWidget(fsbox.frame)
653 dir.form(top=0, left=0, right='%40', bottom=-1)
654 fsbox.form(top=0, left='%40', right=-1, bottom=-1)
655
656def MkDirListWidget(w):
657 msg = Tix.Message(w, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
658 relief=Tix.FLAT, width=240, anchor=Tix.N,
659 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.')
660 dirlist = Tix.DirList(w, options='hlist.padY 1 hlist.width 25 hlist.height 16')
661 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
662 dirlist.pack(side=Tix.TOP, padx=3, pady=3)
663
664def MkExFileWidget(w):
665 msg = Tix.Message(w, font='-*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*',
666 relief=Tix.FLAT, width=240, anchor=Tix.N,
667 text='The TixExFileSelectBox widget is more user friendly than the Motif style FileSelectBox.')
668 # There's a bug in the ComboBoxes - the scrolledlistbox is destroyed
669 box = Tix.ExFileSelectBox(w, bd=2, relief=Tix.RAISED)
670 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
671 box.pack(side=Tix.TOP, padx=3, pady=3)
672
673###
674### List of all the demos we want to show off
675comments = {'widget' : 'Widget Demos', 'image' : 'Image Demos'}
676samples = {'Balloon' : 'Balloon',
677 'Button Box' : 'BtnBox',
678 'Combo Box' : 'ComboBox',
679 'Compound Image' : 'CmpImg',
680 'Control' : 'Control',
681 'Notebook' : 'NoteBook',
682 'Option Menu' : 'OptMenu',
683 'Popup Menu' : 'PopMenu',
684 'ScrolledHList (1)' : 'SHList1',
685 'ScrolledHList (2)' : 'SHList2',
686 'Tree (dynamic)' : 'Tree'
687}
688
689stypes = {}
690stypes['widget'] = ['Balloon', 'Button Box', 'Combo Box', 'Control',
691 'Notebook', 'Option Menu', 'Popup Menu',
692 'ScrolledHList (1)', 'ScrolledHList (2)', 'Tree (dynamic)']
693stypes['image'] = ['Compound Image']
694
695def MkSample(nb, name):
696 w = nb.page(name)
697 prefix = Tix.OptionName(w)
698 if not prefix:
699 prefix = ''
700 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
701
702 lab = Tix.Label(w, text='Select a sample program:', anchor=Tix.W)
703 lab1 = Tix.Label(w, text='Source:', anchor=Tix.W)
704
705 slb = Tix.ScrolledHList(w, options='listbox.exportSelection 0')
706 slb.hlist['command'] = lambda args=0, w=w,slb=slb: Sample_Action(w, slb, 'run')
707 slb.hlist['browsecmd'] = lambda args=0, w=w,slb=slb: Sample_Action(w, slb, 'browse')
708
709 stext = Tix.ScrolledText(w, name='stext')
710 stext.text.bind('<1>', stext.text.focus())
711 stext.text.bind('<Up>', lambda w=stext.text: w.yview(scroll='-1 unit'))
712 stext.text.bind('<Down>', lambda w=stext.text: w.yview(scroll='1 unit'))
713 stext.text.bind('<Left>', lambda w=stext.text: w.xview(scroll='-1 unit'))
714 stext.text.bind('<Right>', lambda w=stext.text: w.xview(scroll='1 unit'))
715
716 run = Tix.Button(w, text='Run ...', name='run', command=lambda args=0, w=w,slb=slb: Sample_Action(w, slb, 'run'))
717 view = Tix.Button(w, text='View Source ...', name='view', command=lambda args=0,w=w,slb=slb: Sample_Action(w, slb, 'view'))
718
719 lab.form(top=0, left=0, right='&'+str(slb))
720 slb.form(left=0, top=lab, bottom=-4)
721 lab1.form(left='&'+str(stext), top=0, right='&'+str(stext), bottom=stext)
722 run.form(left=str(slb)+' 30', bottom=-4)
723 view.form(left=run, bottom=-4)
724 stext.form(bottom=str(run)+' -5', left='&'+str(run), right='-0', top='&'+str(slb))
725
726 stext.text['bg'] = slb.hlist['bg']
727 stext.text['state'] = 'disabled'
728 stext.text['wrap'] = 'none'
729 #XXX stext.text['font'] = fixed_font
730
731 slb.hlist['separator'] = '.'
732 slb.hlist['width'] = 25
733 slb.hlist['drawbranch'] = 0
734 slb.hlist['indent'] = 10
735 slb.hlist['wideselect'] = 1
736
737 for type in ['widget', 'image']:
738 if type != 'widget':
739 x = Tix.Frame(slb.hlist, bd=2, height=2, width=150,
740 relief=Tix.SUNKEN, bg=slb.hlist['bg'])
741 slb.hlist.add_child(itemtype=Tix.WINDOW, window=x, state='disabled')
742 x = slb.hlist.add_child(itemtype=Tix.TEXT, state='disabled',
743 text=comments[type])
744 for key in stypes[type]:
745 slb.hlist.add_child(x, itemtype=Tix.TEXT, data=key,
746 text=key)
747 slb.hlist.selection_clear()
748
749 run['state'] = 'disabled'
750 view['state'] = 'disabled'
751
752def Sample_Action(w, slb, action):
753 global demo
754
755 run = w._nametowidget(str(w) + '.run')
756 view = w._nametowidget(str(w) + '.view')
757 stext = w._nametowidget(str(w) + '.stext')
758
759 hlist = slb.hlist
760 anchor = hlist.info_anchor()
761 if not anchor:
762 run['state'] = 'disabled'
763 view['state'] = 'disabled'
764 elif not hlist.info_parent(anchor):
765 # a comment
766 return
767
768 run['state'] = 'normal'
769 view['state'] = 'normal'
770 key = hlist.info_data(anchor)
771 title = key
772 prog = samples[key]
773
774 if action == 'run':
775 exec('import ' + prog)
776 w = Tix.Toplevel()
777 w.title(title)
778 rtn = eval(prog + '.RunSample')
779 rtn(w)
780 elif action == 'view':
781 w = Tix.Toplevel()
782 w.title('Source view: ' + title)
783 LoadFile(w, demo.dir + '/samples/' + prog + '.py')
784 elif action == 'browse':
785 ReadFile(stext.text, demo.dir + '/samples/' + prog + '.py')
786
787def LoadFile(w, fname):
788 b = Tix.Button(w, text='Close', command=w.destroy)
789 t = Tix.ScrolledText(w)
790 # b.form(left=0, bottom=0, padx=4, pady=4)
791 # t.form(left=0, bottom=b, right='-0', top=0)
792 t.pack()
793 b.pack()
794
795 t.text['highlightcolor'] = t['bg']
796 t.text['bd'] = 2
797 t.text['bg'] = t['bg']
798 t.text['wrap'] = 'none'
799
800 ReadFile(t.text, fname)
801
802def ReadFile(w, fname):
803 old_state = w['state']
804 w['state'] = 'normal'
805 w.delete('0.0', Tix.END)
806
807 try:
808 f = open(fname)
809 lines = f.readlines()
810 for s in lines:
811 w.insert(Tix.END, s)
812 f.close()
813 finally:
814# w.see('1.0')
815 w['state'] = old_state
816
817if __name__ == '__main__':
818 main()
819