blob: 177e452c5c2544a7477a310f46944575c65c781d [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öwis20efa682001-11-11 14:07:37 +000012# % python tixwidget.py
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000013#
14
15import os, 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)
63 fm = Tix.Menu(file)
64 file['menu'] = fm
65 hm = Tix.Menu(help)
66 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
72 fm.add_command(label='Exit', underline=1, accelerator='Ctrl+X',
73 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):
131 # self.root.destroy()
132 self.exit = 0
133
134 def loop(self):
135 while self.exit < 0:
136 self.root.tk.dooneevent(TCL_ALL_EVENTS)
137 # self.root.tk.dooneevent(TCL_DONT_WAIT)
138
139 def destroy (self):
140 self.root.destroy()
141
142def RunMain(top):
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000143 global demo, root
144
Martin v. Löwis20efa682001-11-11 14:07:37 +0000145 demo = Demo(top)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000146
Martin v. Löwis20efa682001-11-11 14:07:37 +0000147 # top.withdraw()
148 # root = Tix.Toplevel()
149 root = top
150 demo.build()
151 demo.loop()
152 demo.destroy()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000153
Martin v. Löwis20efa682001-11-11 14:07:37 +0000154# Tabs
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000155def MkWelcome(nb, name):
156 w = nb.page(name)
157 bar = MkWelcomeBar(w)
158 text = MkWelcomeText(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000159 bar.pack(side=TOP, fill=X, padx=2, pady=2)
160 text.pack(side=TOP, fill=BOTH, expand=1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000161
162def MkWelcomeBar(top):
163 global demo
164
165 w = Tix.Frame(top, bd=2, relief=Tix.GROOVE)
166 b1 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
167 b2 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
168 b1.entry['width'] = 15
169 b1.slistbox.listbox['height'] = 3
170 b2.entry['width'] = 4
171 b2.slistbox.listbox['height'] = 3
172
173 demo.welfont = b1
174 demo.welsize = b2
175
176 b1.insert(Tix.END, 'Courier')
177 b1.insert(Tix.END, 'Helvetica')
178 b1.insert(Tix.END, 'Lucida')
179 b1.insert(Tix.END, 'Times Roman')
180
181 b2.insert(Tix.END, '8')
182 b2.insert(Tix.END, '10')
183 b2.insert(Tix.END, '12')
184 b2.insert(Tix.END, '14')
185 b2.insert(Tix.END, '18')
186
187 b1.pick(1)
188 b2.pick(3)
189
190 b1.pack(side=Tix.LEFT, padx=4, pady=4)
191 b2.pack(side=Tix.LEFT, padx=4, pady=4)
192
193 demo.balloon.bind_widget(b1, msg='Choose\na font',
194 statusmsg='Choose a font for this page')
195 demo.balloon.bind_widget(b2, msg='Point size',
196 statusmsg='Choose the font size for this page')
197 return w
198
199def MkWelcomeText(top):
200 global demo
201
202 w = Tix.ScrolledWindow(top, scrollbar='auto')
203 win = w.window
204 text = 'Welcome to TIX in Python'
Martin v. Löwis20efa682001-11-11 14:07:37 +0000205 title = Tix.Label(win,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000206 bd=0, width=30, anchor=Tix.N, text=text)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000207 msg = Tix.Message(win,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000208 bd=0, width=400, anchor=Tix.N,
209 text='Tix is a set of mega-widgets based on TK. This program \
210demonstrates the widgets in the Tix widget set. You can choose the pages \
211in this window to look at the corresponding widgets. \n\n\
212To quit this program, choose the "File | Exit" command.\n\n\
213For more information, see http://tix.sourceforge.net.')
214 title.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
215 msg.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
216 demo.welmsg = msg
217 return w
218
219def MainTextFont(w):
220 global demo
221
222 if not demo.welmsg:
223 return
224 font = demo.welfont['value']
225 point = demo.welsize['value']
226 if font == 'Times Roman':
227 font = 'times'
Martin v. Löwis20efa682001-11-11 14:07:37 +0000228 fontstr = '%s %s' % (font, point)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000229 demo.welmsg['font'] = fontstr
230
231def ToggleHelp():
232 if demo.useBalloons.get() == '1':
233 demo.balloon['state'] = 'both'
234 else:
235 demo.balloon['state'] = 'none'
236
237def MkChoosers(nb, name):
238 w = nb.page(name)
239 prefix = Tix.OptionName(w)
240 if not prefix:
241 prefix = ''
242 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
243
244 til = Tix.LabelFrame(w, label='Chooser Widgets')
245 cbx = Tix.LabelFrame(w, label='tixComboBox')
246 ctl = Tix.LabelFrame(w, label='tixControl')
247 sel = Tix.LabelFrame(w, label='tixSelect')
248 opt = Tix.LabelFrame(w, label='tixOptionMenu')
249 fil = Tix.LabelFrame(w, label='tixFileEntry')
250 fbx = Tix.LabelFrame(w, label='tixFileSelectBox')
251 tbr = Tix.LabelFrame(w, label='Tool Bar')
252
253 MkTitle(til.frame)
254 MkCombo(cbx.frame)
255 MkControl(ctl.frame)
256 MkSelect(sel.frame)
257 MkOptMenu(opt.frame)
258 MkFileEnt(fil.frame)
259 MkFileBox(fbx.frame)
260 MkToolBar(tbr.frame)
261
262 # First column: comBox and selector
263 cbx.form(top=0, left=0, right='%33')
264 sel.form(left=0, right='&'+str(cbx), top=cbx)
265 opt.form(left=0, right='&'+str(cbx), top=sel, bottom=-1)
266
267 # Second column: title .. etc
268 til.form(left=cbx, top=0,right='%66')
269 ctl.form(left=cbx, right='&'+str(til), top=til)
270 fil.form(left=cbx, right='&'+str(til), top=ctl)
271 tbr.form(left=cbx, right='&'+str(til), top=fil, bottom=-1)
272
273 #
274 # Third column: file selection
275 fbx.form(right=-1, top=0, left='%66')
276
277def MkCombo(w):
278 prefix = Tix.OptionName(w)
279 if not prefix: prefix = ''
280 w.option_add('*' + prefix + '*TixComboBox*label.width', 10)
281 w.option_add('*' + prefix + '*TixComboBox*label.anchor', Tix.E)
282 w.option_add('*' + prefix + '*TixComboBox*entry.width', 14)
283
284 static = Tix.ComboBox(w, label='Static', editable=0)
285 editable = Tix.ComboBox(w, label='Editable', editable=1)
286 history = Tix.ComboBox(w, label='History', editable=1, history=1,
287 anchor=Tix.E)
288 static.insert(Tix.END, 'January')
289 static.insert(Tix.END, 'February')
290 static.insert(Tix.END, 'March')
291 static.insert(Tix.END, 'April')
292 static.insert(Tix.END, 'May')
293 static.insert(Tix.END, 'June')
294 static.insert(Tix.END, 'July')
295 static.insert(Tix.END, 'August')
296 static.insert(Tix.END, 'September')
297 static.insert(Tix.END, 'October')
298 static.insert(Tix.END, 'November')
299 static.insert(Tix.END, 'December')
300
301 editable.insert(Tix.END, 'Angola')
302 editable.insert(Tix.END, 'Bangladesh')
303 editable.insert(Tix.END, 'China')
304 editable.insert(Tix.END, 'Denmark')
305 editable.insert(Tix.END, 'Ecuador')
306
307 history.insert(Tix.END, '/usr/bin/ksh')
308 history.insert(Tix.END, '/usr/local/lib/python')
309 history.insert(Tix.END, '/var/adm')
310
311 static.pack(side=Tix.TOP, padx=5, pady=3)
312 editable.pack(side=Tix.TOP, padx=5, pady=3)
313 history.pack(side=Tix.TOP, padx=5, pady=3)
314
315states = ['Bengal', 'Delhi', 'Karnataka', 'Tamil Nadu']
316
317def spin_cmd(w, inc):
318 idx = states.index(demo_spintxt.get()) + inc
319 if idx < 0:
320 idx = len(states) - 1
321 elif idx >= len(states):
322 idx = 0
323# following doesn't work.
324# return states[idx]
325 demo_spintxt.set(states[idx]) # this works
326
327def spin_validate(w):
328 global states, demo_spintxt
329
330 try:
331 i = states.index(demo_spintxt.get())
Fred Drake7def2562001-05-11 19:44:55 +0000332 except ValueError:
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000333 return states[0]
334 return states[i]
335 # why this procedure works as opposed to the previous one beats me.
336
337def MkControl(w):
338 global demo_spintxt
339
340 prefix = Tix.OptionName(w)
341 if not prefix: prefix = ''
342 w.option_add('*' + prefix + '*TixControl*label.width', 10)
343 w.option_add('*' + prefix + '*TixControl*label.anchor', Tix.E)
344 w.option_add('*' + prefix + '*TixControl*entry.width', 13)
345
346 demo_spintxt = Tix.StringVar()
347 demo_spintxt.set(states[0])
348 simple = Tix.Control(w, label='Numbers')
349 spintxt = Tix.Control(w, label='States', variable=demo_spintxt)
350 spintxt['incrcmd'] = lambda w=spintxt: spin_cmd(w, 1)
351 spintxt['decrcmd'] = lambda w=spintxt: spin_cmd(w, -1)
352 spintxt['validatecmd'] = lambda w=spintxt: spin_validate(w)
353
354 simple.pack(side=Tix.TOP, padx=5, pady=3)
355 spintxt.pack(side=Tix.TOP, padx=5, pady=3)
356
357def MkSelect(w):
358 prefix = Tix.OptionName(w)
359 if not prefix: prefix = ''
360 w.option_add('*' + prefix + '*TixSelect*label.anchor', Tix.CENTER)
361 w.option_add('*' + prefix + '*TixSelect*orientation', Tix.VERTICAL)
362 w.option_add('*' + prefix + '*TixSelect*labelSide', Tix.TOP)
363
364 sel1 = Tix.Select(w, label='Mere Mortals', allowzero=1, radio=1)
365 sel2 = Tix.Select(w, label='Geeks', allowzero=1, radio=0)
366
367 sel1.add('eat', text='Eat')
368 sel1.add('work', text='Work')
369 sel1.add('play', text='Play')
370 sel1.add('party', text='Party')
371 sel1.add('sleep', text='Sleep')
372
373 sel2.add('eat', text='Eat')
374 sel2.add('prog1', text='Program')
375 sel2.add('prog2', text='Program')
376 sel2.add('prog3', text='Program')
377 sel2.add('sleep', text='Sleep')
378
379 sel1.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
380 sel2.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
381
382def MkOptMenu(w):
383 prefix = Tix.OptionName(w)
384 if not prefix: prefix = ''
385 w.option_add('*' + prefix + '*TixOptionMenu*label.anchor', Tix.E)
386 m = Tix.OptionMenu(w, label='File Format : ', options='menubutton.width 15')
387 m.add_command('text', label='Plain Text')
388 m.add_command('post', label='PostScript')
389 m.add_command('format', label='Formatted Text')
390 m.add_command('html', label='HTML')
391 m.add_command('sep')
392 m.add_command('tex', label='LaTeX')
393 m.add_command('rtf', label='Rich Text Format')
394
395 m.pack(fill=Tix.X, padx=5, pady=3)
396
397def MkFileEnt(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000398 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000399 relief=Tix.FLAT, width=240, anchor=Tix.N,
400 text='Press the "open file" icon button and a TixFileSelectDialog will popup.')
401 ent = Tix.FileEntry(w, label='Select a file : ')
402 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
403 ent.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
404
405def MkFileBox(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000406 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000407 relief=Tix.FLAT, width=240, anchor=Tix.N,
408 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.')
409 box = Tix.FileSelectBox(w)
410 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
411 box.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
412
413def MkToolBar(w):
414 global demo
415
416 prefix = Tix.OptionName(w)
417 if not prefix: prefix = ''
418 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000419 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000420 relief=Tix.FLAT, width=240, anchor=Tix.N,
421 text='The Select widget is also good for arranging buttons in a tool bar.')
422 bar = Tix.Frame(w, bd=2, relief=Tix.RAISED)
423 font = Tix.Select(w, allowzero=1, radio=0, label='')
424 para = Tix.Select(w, allowzero=0, radio=1, label='')
425
426 font.add('bold', bitmap='@' + demo.dir + '/bitmaps/bold.xbm')
427 font.add('italic', bitmap='@' + demo.dir + '/bitmaps/italic.xbm')
428 font.add('underline', bitmap='@' + demo.dir + '/bitmaps/underline.xbm')
429 font.add('capital', bitmap='@' + demo.dir + '/bitmaps/capital.xbm')
430
431 para.add('left', bitmap='@' + demo.dir + '/bitmaps/leftj.xbm')
432 para.add('right', bitmap='@' + demo.dir + '/bitmaps/rightj.xbm')
433 para.add('center', bitmap='@' + demo.dir + '/bitmaps/centerj.xbm')
434 para.add('justify', bitmap='@' + demo.dir + '/bitmaps/justify.xbm')
435
436 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
437 bar.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
438 font.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
439 para.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
440
441def MkTitle(w):
442 prefix = Tix.OptionName(w)
443 if not prefix: prefix = ''
444 w.option_add('*' + prefix + '*TixSelect*frame.borderWidth', 1)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000445 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000446 relief=Tix.FLAT, width=240, anchor=Tix.N,
447 text='There are many types of "chooser" widgets that allow the user to input different types of information')
448 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
449
450def MkScroll(nb, name):
451 w = nb.page(name)
452 prefix = Tix.OptionName(w)
453 if not prefix:
454 prefix = ''
455 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
456
457 sls = Tix.LabelFrame(w, label='tixScrolledListBox')
458 swn = Tix.LabelFrame(w, label='tixScrolledWindow')
459 stx = Tix.LabelFrame(w, label='tixScrolledText')
460
461 MkSList(sls.frame)
462 MkSWindow(swn.frame)
463 MkSText(stx.frame)
464
465 sls.form(top=0, left=0, right='%33', bottom=-1)
466 swn.form(top=0, left=sls, right='%66', bottom=-1)
467 stx.form(top=0, left=swn, right=-1, bottom=-1)
468
469def MkSList(w):
470 top = Tix.Frame(w, width=300, height=330)
471 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000472 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000473 relief=Tix.FLAT, width=200, anchor=Tix.N,
474 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.')
475
476 list = Tix.ScrolledListBox(top, scrollbar='auto')
477 list.place(x=50, y=150, width=120, height=80)
478 list.listbox.insert(Tix.END, 'Alabama')
479 list.listbox.insert(Tix.END, 'California')
480 list.listbox.insert(Tix.END, 'Montana')
481 list.listbox.insert(Tix.END, 'New Jersey')
482 list.listbox.insert(Tix.END, 'New York')
483 list.listbox.insert(Tix.END, 'Pennsylvania')
484 list.listbox.insert(Tix.END, 'Washington')
485
486 rh = Tix.ResizeHandle(top, bg='black',
487 relief=Tix.RAISED,
488 handlesize=8, gridded=1, minwidth=50, minheight=30)
489 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=list: SList_reset(w,x))
490 top.propagate(0)
491 msg.pack(fill=Tix.X)
492 btn.pack(anchor=Tix.CENTER)
493 top.pack(expand=1, fill=Tix.BOTH)
494 bot.pack(fill=Tix.BOTH)
495 list.bind('<Map>', func=lambda arg=0, rh=rh, list=list:
496 list.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(list)))
497
498def SList_reset(rh, list):
499 list.place(x=50, y=150, width=120, height=80)
500 list.update()
501 rh.attach_widget(list)
502
503def MkSWindow(w):
504 global demo
505
506 top = Tix.Frame(w, width=330, height=330)
507 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000508 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000509 relief=Tix.FLAT, width=200, anchor=Tix.N,
510 text='The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.')
511 win = Tix.ScrolledWindow(top, scrollbar='auto')
512 image = Tix.Image('photo', file=demo.dir + "/bitmaps/tix.gif")
513 lbl = Tix.Label(win.window, image=image)
514 lbl.pack(expand=1, fill=Tix.BOTH)
515
516 win.place(x=30, y=150, width=190, height=120)
517
518 rh = Tix.ResizeHandle(top, bg='black',
519 relief=Tix.RAISED,
520 handlesize=8, gridded=1, minwidth=50, minheight=30)
521 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SWindow_reset(w,x))
522 top.propagate(0)
523 msg.pack(fill=Tix.X)
524 btn.pack(anchor=Tix.CENTER)
525 top.pack(expand=1, fill=Tix.BOTH)
526 bot.pack(fill=Tix.BOTH)
527 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
528 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
529
530def SWindow_reset(rh, win):
531 win.place(x=30, y=150, width=190, height=120)
532 win.update()
533 rh.attach_widget(win)
534
535def MkSText(w):
536 top = Tix.Frame(w, width=330, height=330)
537 bot = Tix.Frame(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000538 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000539 relief=Tix.FLAT, width=200, anchor=Tix.N,
540 text='The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.')
541
542 win = Tix.ScrolledText(top, scrollbar='auto')
543# win.text['wrap'] = 'none'
544 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.')
545 win.place(x=30, y=150, width=190, height=100)
546
547 rh = Tix.ResizeHandle(top, bg='black',
548 relief=Tix.RAISED,
549 handlesize=8, gridded=1, minwidth=50, minheight=30)
550 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SText_reset(w,x))
551 top.propagate(0)
552 msg.pack(fill=Tix.X)
553 btn.pack(anchor=Tix.CENTER)
554 top.pack(expand=1, fill=Tix.BOTH)
555 bot.pack(fill=Tix.BOTH)
556 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
557 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
558
559def SText_reset(rh, win):
560 win.place(x=30, y=150, width=190, height=120)
561 win.update()
562 rh.attach_widget(win)
563
564def MkManager(nb, name):
565 w = nb.page(name)
566 prefix = Tix.OptionName(w)
567 if not prefix:
568 prefix = ''
569 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
570
571 pane = Tix.LabelFrame(w, label='tixPanedWindow')
572 note = Tix.LabelFrame(w, label='tixNoteBook')
573
574 MkPanedWindow(pane.frame)
575 MkNoteBook(note.frame)
576
577 pane.form(top=0, left=0, right=note, bottom=-1)
578 note.form(top=0, right=-1, bottom=-1)
579
580def MkPanedWindow(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000581 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000582 relief=Tix.FLAT, width=240, anchor=Tix.N,
583 text='The PanedWindow widget allows the user to interactively manipulate the sizes of several panes. The panes can be arranged either vertically or horizontally.')
584 group = Tix.Label(w, text='Newsgroup: comp.lang.python')
585 pane = Tix.PanedWindow(w, orientation='vertical')
586
587 p1 = pane.add('list', min=70, size=100)
588 p2 = pane.add('text', min=70)
589 list = Tix.ScrolledListBox(p1)
590 text = Tix.ScrolledText(p2)
591
592 list.listbox.insert(Tix.END, " 12324 Re: TK is good for your health")
593 list.listbox.insert(Tix.END, "+ 12325 Re: TK is good for your health")
594 list.listbox.insert(Tix.END, "+ 12326 Re: Tix is even better for your health (Was: TK is good...)")
595 list.listbox.insert(Tix.END, " 12327 Re: Tix is even better for your health (Was: TK is good...)")
596 list.listbox.insert(Tix.END, "+ 12328 Re: Tix is even better for your health (Was: TK is good...)")
597 list.listbox.insert(Tix.END, " 12329 Re: Tix is even better for your health (Was: TK is good...)")
598 list.listbox.insert(Tix.END, "+ 12330 Re: Tix is even better for your health (Was: TK is good...)")
599
600 text.text['bg'] = list.listbox['bg']
601 text.text['wrap'] = 'none'
602 text.text.insert(Tix.END, """
603Mon, 19 Jun 1995 11:39:52 comp.lang.tcl Thread 34 of 220
604Lines 353 A new way to put text and bitmaps together iNo responses
605ioi@blue.seas.upenn.edu Ioi K. Lam at University of Pennsylvania
606
607Hi,
608
609I have implemented a new image type called "compound". It allows you
610to glue together a bunch of bitmaps, images and text strings together
611to form a bigger image. Then you can use this image with widgets that
612support the -image option. For example, you can display a text string string
613together with a bitmap, at the same time, inside a TK button widget.
614""")
615 list.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
616 text.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
617
618 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
619 group.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
620 pane.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH, expand=1)
621
622def MkNoteBook(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000623 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000624 relief=Tix.FLAT, width=240, anchor=Tix.N,
625 text='The NoteBook widget allows you to layout a complex interface into individual pages.')
626 prefix = Tix.OptionName(w)
627 if not prefix:
628 prefix = ''
629 w.option_add('*' + prefix + '*TixControl*entry.width', 10)
630 w.option_add('*' + prefix + '*TixControl*label.width', 18)
631 w.option_add('*' + prefix + '*TixControl*label.anchor', Tix.E)
632 w.option_add('*' + prefix + '*TixNoteBook*tagPadX', 8)
633
634 nb = Tix.NoteBook(w, ipadx=6, ipady=6)
635 nb.add('hard_disk', label="Hard Disk", underline=0)
636 nb.add('network', label="Network", underline=0)
637
638 # Frame for the buttons that are present on all pages
639 common = Tix.Frame(nb.hard_disk)
640 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
641 CreateCommonButtons(common)
642
643 # Widgets belonging only to this page
644 a = Tix.Control(nb.hard_disk, value=12, label='Access Time: ')
645 w = Tix.Control(nb.hard_disk, value=400, label='Write Throughput: ')
646 r = Tix.Control(nb.hard_disk, value=400, label='Read Throughput: ')
647 c = Tix.Control(nb.hard_disk, value=1021, label='Capacity: ')
648 a.pack(side=Tix.TOP, padx=20, pady=2)
649 w.pack(side=Tix.TOP, padx=20, pady=2)
650 r.pack(side=Tix.TOP, padx=20, pady=2)
651 c.pack(side=Tix.TOP, padx=20, pady=2)
652
653 common = Tix.Frame(nb.network)
654 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
655 CreateCommonButtons(common)
656
657 a = Tix.Control(nb.network, value=12, label='Access Time: ')
658 w = Tix.Control(nb.network, value=400, label='Write Throughput: ')
659 r = Tix.Control(nb.network, value=400, label='Read Throughput: ')
660 c = Tix.Control(nb.network, value=1021, label='Capacity: ')
661 u = Tix.Control(nb.network, value=10, label='Users: ')
662 a.pack(side=Tix.TOP, padx=20, pady=2)
663 w.pack(side=Tix.TOP, padx=20, pady=2)
664 r.pack(side=Tix.TOP, padx=20, pady=2)
665 c.pack(side=Tix.TOP, padx=20, pady=2)
666 u.pack(side=Tix.TOP, padx=20, pady=2)
667
668 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
669 nb.pack(side=Tix.TOP, padx=5, pady=5, fill=Tix.BOTH, expand=1)
670
671def CreateCommonButtons(f):
672 ok = Tix.Button(f, text='OK', width = 6)
673 cancel = Tix.Button(f, text='Cancel', width = 6)
674 ok.pack(side=Tix.TOP, padx=2, pady=2)
675 cancel.pack(side=Tix.TOP, padx=2, pady=2)
676
677def MkDirList(nb, name):
678 w = nb.page(name)
679 prefix = Tix.OptionName(w)
680 if not prefix:
681 prefix = ''
682 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
683
684 dir = Tix.LabelFrame(w, label='tixDirList')
685 fsbox = Tix.LabelFrame(w, label='tixExFileSelectBox')
686 MkDirListWidget(dir.frame)
687 MkExFileWidget(fsbox.frame)
688 dir.form(top=0, left=0, right='%40', bottom=-1)
689 fsbox.form(top=0, left='%40', right=-1, bottom=-1)
690
691def MkDirListWidget(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000692 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000693 relief=Tix.FLAT, width=240, anchor=Tix.N,
694 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.')
695 dirlist = Tix.DirList(w, options='hlist.padY 1 hlist.width 25 hlist.height 16')
696 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
697 dirlist.pack(side=Tix.TOP, padx=3, pady=3)
698
699def MkExFileWidget(w):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000700 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000701 relief=Tix.FLAT, width=240, anchor=Tix.N,
702 text='The TixExFileSelectBox widget is more user friendly than the Motif style FileSelectBox.')
703 # There's a bug in the ComboBoxes - the scrolledlistbox is destroyed
704 box = Tix.ExFileSelectBox(w, bd=2, relief=Tix.RAISED)
705 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
706 box.pack(side=Tix.TOP, padx=3, pady=3)
707
708###
709### List of all the demos we want to show off
710comments = {'widget' : 'Widget Demos', 'image' : 'Image Demos'}
711samples = {'Balloon' : 'Balloon',
712 'Button Box' : 'BtnBox',
713 'Combo Box' : 'ComboBox',
714 'Compound Image' : 'CmpImg',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000715 'Directory List' : 'DirList',
716 'Directory Tree' : 'DirTree',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000717 'Control' : 'Control',
718 'Notebook' : 'NoteBook',
719 'Option Menu' : 'OptMenu',
720 'Popup Menu' : 'PopMenu',
721 'ScrolledHList (1)' : 'SHList1',
722 'ScrolledHList (2)' : 'SHList2',
723 'Tree (dynamic)' : 'Tree'
724}
725
Martin v. Löwis20efa682001-11-11 14:07:37 +0000726# There are still a lot of demos to be translated:
727## set root {
728## {d "File Selectors" file }
729## {d "Hierachical ListBox" hlist }
730## {d "Tabular ListBox" tlist {c tixTList}}
731## {d "Grid Widget" grid {c tixGrid}}
732## {d "Manager Widgets" manager }
733## {d "Scrolled Widgets" scroll }
734## {d "Miscellaneous Widgets" misc }
735## {d "Image Types" image }
736## }
737##
738## set image {
739## {d "Compound Image" cmpimg }
740## {d "XPM Image" xpm {i pixmap}}
741## }
742##
743## set cmpimg {
744## {f "In Buttons" CmpImg.tcl }
745## {f "In NoteBook" CmpImg2.tcl }
746## {f "Notebook Color Tabs" CmpImg4.tcl }
747## {f "Icons" CmpImg3.tcl }
748## }
749##
750## set xpm {
751## {f "In Button" Xpm.tcl {i pixmap}}
752## {f "In Menu" Xpm1.tcl {i pixmap}}
753## }
754##
755## set file {
756##added {f DirList DirList.tcl }
757##added {f DirTree DirTree.tcl }
758## {f DirSelectDialog DirDlg.tcl }
759## {f ExFileSelectDialog EFileDlg.tcl }
760## {f FileSelectDialog FileDlg.tcl }
761## {f FileEntry FileEnt.tcl }
762## }
763##
764## set hlist {
765## {f HList HList1.tcl }
766## {f CheckList ChkList.tcl {c tixCheckList}}
767##done {f "ScrolledHList (1)" SHList.tcl }
768##done {f "ScrolledHList (2)" SHList2.tcl }
769##done {f Tree Tree.tcl }
770##done {f "Tree (Dynamic)" DynTree.tcl {v win}}
771## }
772##
773## set tlist {
774## {f "ScrolledTList (1)" STList1.tcl {c tixTList}}
775## {f "ScrolledTList (2)" STList2.tcl {c tixTList}}
776## }
777## global tcl_platform
778## # This demo hangs windows
779## if {$tcl_platform(platform) != "windows"} {
780##na lappend tlist {f "TList File Viewer" STList3.tcl {c tixTList}}
781## }
782##
783## set grid {
784##na {f "Simple Grid" SGrid0.tcl {c tixGrid}}
785##na {f "ScrolledGrid" SGrid1.tcl {c tixGrid}}
786##na {f "Editable Grid" EditGrid.tcl {c tixGrid}}
787## }
788##
789## set scroll {
790## {f ScrolledListBox SListBox.tcl }
791## {f ScrolledText SText.tcl }
792## {f ScrolledWindow SWindow.tcl }
793##na {f "Canvas Object View" CObjView.tcl {c tixCObjView}}
794## }
795##
796## set manager {
797##na {f ListNoteBook ListNBK.tcl }
798## {f NoteBook NoteBook.tcl }
799## {f PanedWindow PanedWin.tcl }
800## }
801##
802## set misc {
803##done {f Balloon Balloon.tcl }
804##done {f ButtonBox BtnBox.tcl }
805##done {f ComboBox ComboBox.tcl }
806##done {f Control Control.tcl }
807## {f LabelEntry LabEntry.tcl }
808## {f LabelFrame LabFrame.tcl }
809##na {f Meter Meter.tcl {c tixMeter}}
810##done {f OptionMenu OptMenu.tcl }
811##done {f PopupMenu PopMenu.tcl }
812## {f Select Select.tcl }
813## {f StdButtonBox StdBBox.tcl }
814## }
815##
816
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000817stypes = {}
818stypes['widget'] = ['Balloon', 'Button Box', 'Combo Box', 'Control',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000819 'Directory List', 'Directory Tree',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000820 'Notebook', 'Option Menu', 'Popup Menu',
821 'ScrolledHList (1)', 'ScrolledHList (2)', 'Tree (dynamic)']
822stypes['image'] = ['Compound Image']
823
824def MkSample(nb, name):
825 w = nb.page(name)
826 prefix = Tix.OptionName(w)
827 if not prefix:
828 prefix = ''
829 w.option_add('*' + prefix + '*TixLabelFrame*label.padX', 4)
830
831 lab = Tix.Label(w, text='Select a sample program:', anchor=Tix.W)
832 lab1 = Tix.Label(w, text='Source:', anchor=Tix.W)
833
834 slb = Tix.ScrolledHList(w, options='listbox.exportSelection 0')
835 slb.hlist['command'] = lambda args=0, w=w,slb=slb: Sample_Action(w, slb, 'run')
836 slb.hlist['browsecmd'] = lambda args=0, w=w,slb=slb: Sample_Action(w, slb, 'browse')
837
838 stext = Tix.ScrolledText(w, name='stext')
Martin v. Löwis20efa682001-11-11 14:07:37 +0000839 font = root.tk.eval('tix option get fixed_font')
840 stext.text.config(font=font)
841 # stext.text.bind('<1>', stext.text.focus())
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000842 stext.text.bind('<Up>', lambda w=stext.text: w.yview(scroll='-1 unit'))
843 stext.text.bind('<Down>', lambda w=stext.text: w.yview(scroll='1 unit'))
844 stext.text.bind('<Left>', lambda w=stext.text: w.xview(scroll='-1 unit'))
845 stext.text.bind('<Right>', lambda w=stext.text: w.xview(scroll='1 unit'))
846
847 run = Tix.Button(w, text='Run ...', name='run', command=lambda args=0, w=w,slb=slb: Sample_Action(w, slb, 'run'))
848 view = Tix.Button(w, text='View Source ...', name='view', command=lambda args=0,w=w,slb=slb: Sample_Action(w, slb, 'view'))
849
850 lab.form(top=0, left=0, right='&'+str(slb))
851 slb.form(left=0, top=lab, bottom=-4)
852 lab1.form(left='&'+str(stext), top=0, right='&'+str(stext), bottom=stext)
853 run.form(left=str(slb)+' 30', bottom=-4)
854 view.form(left=run, bottom=-4)
855 stext.form(bottom=str(run)+' -5', left='&'+str(run), right='-0', top='&'+str(slb))
856
857 stext.text['bg'] = slb.hlist['bg']
858 stext.text['state'] = 'disabled'
859 stext.text['wrap'] = 'none'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000860
861 slb.hlist['separator'] = '.'
862 slb.hlist['width'] = 25
863 slb.hlist['drawbranch'] = 0
864 slb.hlist['indent'] = 10
865 slb.hlist['wideselect'] = 1
866
867 for type in ['widget', 'image']:
868 if type != 'widget':
869 x = Tix.Frame(slb.hlist, bd=2, height=2, width=150,
870 relief=Tix.SUNKEN, bg=slb.hlist['bg'])
871 slb.hlist.add_child(itemtype=Tix.WINDOW, window=x, state='disabled')
872 x = slb.hlist.add_child(itemtype=Tix.TEXT, state='disabled',
873 text=comments[type])
874 for key in stypes[type]:
875 slb.hlist.add_child(x, itemtype=Tix.TEXT, data=key,
876 text=key)
877 slb.hlist.selection_clear()
878
879 run['state'] = 'disabled'
880 view['state'] = 'disabled'
881
882def Sample_Action(w, slb, action):
883 global demo
884
885 run = w._nametowidget(str(w) + '.run')
886 view = w._nametowidget(str(w) + '.view')
887 stext = w._nametowidget(str(w) + '.stext')
888
889 hlist = slb.hlist
890 anchor = hlist.info_anchor()
891 if not anchor:
892 run['state'] = 'disabled'
893 view['state'] = 'disabled'
894 elif not hlist.info_parent(anchor):
895 # a comment
896 return
897
898 run['state'] = 'normal'
899 view['state'] = 'normal'
900 key = hlist.info_data(anchor)
901 title = key
902 prog = samples[key]
903
904 if action == 'run':
905 exec('import ' + prog)
906 w = Tix.Toplevel()
907 w.title(title)
908 rtn = eval(prog + '.RunSample')
909 rtn(w)
910 elif action == 'view':
911 w = Tix.Toplevel()
912 w.title('Source view: ' + title)
913 LoadFile(w, demo.dir + '/samples/' + prog + '.py')
914 elif action == 'browse':
915 ReadFile(stext.text, demo.dir + '/samples/' + prog + '.py')
916
917def LoadFile(w, fname):
Martin v. Löwis20efa682001-11-11 14:07:37 +0000918 global root
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000919 b = Tix.Button(w, text='Close', command=w.destroy)
920 t = Tix.ScrolledText(w)
921 # b.form(left=0, bottom=0, padx=4, pady=4)
922 # t.form(left=0, bottom=b, right='-0', top=0)
923 t.pack()
924 b.pack()
925
Martin v. Löwis20efa682001-11-11 14:07:37 +0000926 font = root.tk.eval('tix option get fixed_font')
927 t.text.config(font=font)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000928 t.text['bd'] = 2
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000929 t.text['wrap'] = 'none'
930
931 ReadFile(t.text, fname)
932
933def ReadFile(w, fname):
934 old_state = w['state']
935 w['state'] = 'normal'
936 w.delete('0.0', Tix.END)
937
938 try:
939 f = open(fname)
940 lines = f.readlines()
941 for s in lines:
942 w.insert(Tix.END, s)
943 f.close()
944 finally:
945# w.see('1.0')
946 w['state'] = old_state
947
948if __name__ == '__main__':
Martin v. Löwis20efa682001-11-11 14:07:37 +0000949 root = Tix.Tk()
950 RunMain(root)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000951