blob: 30507c468e9ca2ea775816d5013a8504d96878dd [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#
Martin v. Löwis46874282002-12-06 10:33:45 +00003# Id: tixwidgets.py,v 1.7 2002/11/14 02:44:08 nnorwitz Exp
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00004# $Id$
5#
6# tixwidgets.py --
Martin v. Löwis20efa682001-11-11 14:07:37 +00007#
8# For Tix, see http://tix.sourceforge.net
9#
Neal Norwitzac30ead2002-11-14 02:44:08 +000010# This is a demo program of some of the Tix widgets available in Python.
11# If you have installed Python & Tix properly, you can execute this as
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000012#
Martin v. Löwis652e1912001-11-25 14:50:56 +000013# % python tixwidgets.py
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000014#
15
Martin v. Löwis652e1912001-11-25 14:50:56 +000016import os, os.path, sys, Tix
Martin v. Löwis20efa682001-11-11 14:07:37 +000017from Tkconstants import *
Neal Norwitzac30ead2002-11-14 02:44:08 +000018import traceback, tkMessageBox
Martin v. Löwis20efa682001-11-11 14:07:37 +000019
20TCL_DONT_WAIT = 1<<1
21TCL_WINDOW_EVENTS = 1<<2
22TCL_FILE_EVENTS = 1<<3
23TCL_TIMER_EVENTS = 1<<4
24TCL_IDLE_EVENTS = 1<<5
25TCL_ALL_EVENTS = 0
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000026
27class Demo:
Martin v. Löwis20efa682001-11-11 14:07:37 +000028 def __init__(self, top):
29 self.root = top
30 self.exit = -1
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000031
Martin v. Löwis20efa682001-11-11 14:07:37 +000032 self.dir = None # script directory
33 self.balloon = None # balloon widget
34 self.useBalloons = Tix.StringVar()
35 self.useBalloons.set('0')
36 self.statusbar = None # status bar widget
37 self.welmsg = None # Msg widget
38 self.welfont = '' # font name
39 self.welsize = '' # font size
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000040
Martin v. Löwis20efa682001-11-11 14:07:37 +000041 progname = sys.argv[0]
42 dirname = os.path.dirname(progname)
43 if dirname and dirname != os.curdir:
44 self.dir = dirname
45 index = -1
46 for i in range(len(sys.path)):
47 p = sys.path[i]
48 if p in ("", os.curdir):
49 index = i
50 if index >= 0:
51 sys.path[index] = dirname
52 else:
53 sys.path.insert(0, dirname)
54 else:
55 self.dir = os.getcwd()
56 sys.path.insert(0, self.dir+'/samples')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000057
Martin v. Löwis20efa682001-11-11 14:07:37 +000058 def MkMainMenu(self):
59 top = self.root
60 w = Tix.Frame(top, bd=2, relief=RAISED)
61 file = Tix.Menubutton(w, text='File', underline=0, takefocus=0)
62 help = Tix.Menubutton(w, text='Help', underline=0, takefocus=0)
63 file.pack(side=LEFT)
64 help.pack(side=RIGHT)
Martin v. Löwis652e1912001-11-25 14:50:56 +000065 fm = Tix.Menu(file, tearoff=0)
Martin v. Löwis20efa682001-11-11 14:07:37 +000066 file['menu'] = fm
Martin v. Löwis652e1912001-11-25 14:50:56 +000067 hm = Tix.Menu(help, tearoff=0)
Martin v. Löwis20efa682001-11-11 14:07:37 +000068 help['menu'] = hm
69
Martin v. Löwis652e1912001-11-25 14:50:56 +000070 fm.add_command(label='Exit', underline=1,
Martin v. Löwis20efa682001-11-11 14:07:37 +000071 command = lambda self=self: self.quitcmd () )
72 hm.add_checkbutton(label='BalloonHelp', underline=0, command=ToggleHelp,
73 variable=self.useBalloons)
74 # The trace variable option doesn't seem to work, instead I use 'command'
75 #apply(w.tk.call, ('trace', 'variable', self.useBalloons, 'w',
76 # ToggleHelp))
Neal Norwitzac30ead2002-11-14 02:44:08 +000077
Martin v. Löwis20efa682001-11-11 14:07:37 +000078 return w
79
80 def MkMainNotebook(self):
81 top = self.root
82 w = Tix.NoteBook(top, ipadx=5, ipady=5, options="""
Neal Norwitzac30ead2002-11-14 02:44:08 +000083 tagPadX 6
84 tagPadY 4
85 borderWidth 2
Martin v. Löwis20efa682001-11-11 14:07:37 +000086 """)
87 # This may be required if there is no *Background option
88 top['bg'] = w['bg']
89
90 w.add('wel', label='Welcome', underline=0,
91 createcmd=lambda w=w, name='wel': MkWelcome(w, name))
92 w.add('cho', label='Choosers', underline=0,
93 createcmd=lambda w=w, name='cho': MkChoosers(w, name))
94 w.add('scr', label='Scrolled Widgets', underline=0,
95 createcmd=lambda w=w, name='scr': MkScroll(w, name))
96 w.add('mgr', label='Manager Widgets', underline=0,
97 createcmd=lambda w=w, name='mgr': MkManager(w, name))
98 w.add('dir', label='Directory List', underline=0,
99 createcmd=lambda w=w, name='dir': MkDirList(w, name))
100 w.add('exp', label='Run Sample Programs', underline=0,
101 createcmd=lambda w=w, name='exp': MkSample(w, name))
102 return w
103
104 def MkMainStatus(self):
105 global demo
106 top = self.root
107
108 w = Tix.Frame(top, relief=Tix.RAISED, bd=1)
109 demo.statusbar = Tix.Label(w, relief=Tix.SUNKEN, bd=1)
110 demo.statusbar.form(padx=3, pady=3, left=0, right='%70')
111 return w
112
113 def build(self):
114 root = self.root
115 z = root.winfo_toplevel()
116 z.wm_title('Tix Widget Demonstration')
Neal Norwitzac30ead2002-11-14 02:44:08 +0000117 if z.winfo_screenwidth() <= 800:
118 z.geometry('790x590+10+10')
119 else:
120 z.geometry('890x640+10+10')
Martin v. Löwis20efa682001-11-11 14:07:37 +0000121 demo.balloon = Tix.Balloon(root)
122 frame1 = self.MkMainMenu()
123 frame2 = self.MkMainNotebook()
124 frame3 = self.MkMainStatus()
125 frame1.pack(side=TOP, fill=X)
126 frame3.pack(side=BOTTOM, fill=X)
127 frame2.pack(side=TOP, expand=1, fill=BOTH, padx=4, pady=4)
128 demo.balloon['statusbar'] = demo.statusbar
129 z.wm_protocol("WM_DELETE_WINDOW", lambda self=self: self.quitcmd())
130
Neal Norwitzac30ead2002-11-14 02:44:08 +0000131 # To show Tcl errors - uncomment this to see the listbox bug.
132 # Tkinter defines a Tcl tkerror procedure that in effect
133 # silences all background Tcl error reporting.
134 # root.tk.eval('if {[info commands tkerror] != ""} {rename tkerror pytkerror}')
Martin v. Löwis20efa682001-11-11 14:07:37 +0000135 def quitcmd (self):
Martin v. Löwis652e1912001-11-25 14:50:56 +0000136 """Quit our mainloop. It is up to you to call root.destroy() after."""
Martin v. Löwis20efa682001-11-11 14:07:37 +0000137 self.exit = 0
138
139 def loop(self):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000140 """This is an explict replacement for _tkinter mainloop()
141 It lets you catch keyboard interrupts easier, and avoids
142 the 20 msec. dead sleep() which burns a constant CPU."""
Martin v. Löwis652e1912001-11-25 14:50:56 +0000143 while self.exit < 0:
Neal Norwitzac30ead2002-11-14 02:44:08 +0000144 # There are 2 whiles here. The outer one lets you continue
145 # after a ^C interrupt.
Martin v. Löwis652e1912001-11-25 14:50:56 +0000146 try:
Neal Norwitzac30ead2002-11-14 02:44:08 +0000147 # This is the replacement for _tkinter mainloop()
148 # It blocks waiting for the next Tcl event using select.
149 while self.exit < 0:
150 self.root.tk.dooneevent(TCL_ALL_EVENTS)
Martin v. Löwis652e1912001-11-25 14:50:56 +0000151 except SystemExit:
Neal Norwitzac30ead2002-11-14 02:44:08 +0000152 # Tkinter uses SystemExit to exit
Martin v. Löwis652e1912001-11-25 14:50:56 +0000153 #print 'Exit'
154 self.exit = 1
Neal Norwitzac30ead2002-11-14 02:44:08 +0000155 return
Martin v. Löwis652e1912001-11-25 14:50:56 +0000156 except KeyboardInterrupt:
157 if tkMessageBox.askquestion ('Interrupt', 'Really Quit?') == 'yes':
158 # self.tk.eval('exit')
Neal Norwitzac30ead2002-11-14 02:44:08 +0000159 self.exit = 1
Martin v. Löwis652e1912001-11-25 14:50:56 +0000160 return
Martin v. Löwis652e1912001-11-25 14:50:56 +0000161 continue
162 except:
Neal Norwitzac30ead2002-11-14 02:44:08 +0000163 # Otherwise it's some other error - be nice and say why
Martin v. Löwis652e1912001-11-25 14:50:56 +0000164 t, v, tb = sys.exc_info()
165 text = ""
166 for line in traceback.format_exception(t,v,tb):
167 text += line + '\n'
168 try: tkMessageBox.showerror ('Error', text)
169 except: pass
Martin v. Löwis8ec03e02002-03-17 18:19:13 +0000170 self.exit = 1
171 raise SystemExit, 1
Martin v. Löwis20efa682001-11-11 14:07:37 +0000172
173 def destroy (self):
174 self.root.destroy()
Neal Norwitzac30ead2002-11-14 02:44:08 +0000175
Martin v. Löwis652e1912001-11-25 14:50:56 +0000176def RunMain(root):
177 global demo
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000178
Martin v. Löwis652e1912001-11-25 14:50:56 +0000179 demo = Demo(root)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000180
Martin v. Löwis20efa682001-11-11 14:07:37 +0000181 demo.build()
182 demo.loop()
183 demo.destroy()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000184
Martin v. Löwis20efa682001-11-11 14:07:37 +0000185# Tabs
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000186def MkWelcome(nb, name):
187 w = nb.page(name)
188 bar = MkWelcomeBar(w)
189 text = MkWelcomeText(w)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000190 bar.pack(side=TOP, fill=X, padx=2, pady=2)
191 text.pack(side=TOP, fill=BOTH, expand=1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000192
193def MkWelcomeBar(top):
194 global demo
195
196 w = Tix.Frame(top, bd=2, relief=Tix.GROOVE)
197 b1 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
198 b2 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
199 b1.entry['width'] = 15
200 b1.slistbox.listbox['height'] = 3
201 b2.entry['width'] = 4
202 b2.slistbox.listbox['height'] = 3
203
204 demo.welfont = b1
205 demo.welsize = b2
206
207 b1.insert(Tix.END, 'Courier')
208 b1.insert(Tix.END, 'Helvetica')
209 b1.insert(Tix.END, 'Lucida')
210 b1.insert(Tix.END, 'Times Roman')
211
212 b2.insert(Tix.END, '8')
213 b2.insert(Tix.END, '10')
214 b2.insert(Tix.END, '12')
215 b2.insert(Tix.END, '14')
216 b2.insert(Tix.END, '18')
217
218 b1.pick(1)
219 b2.pick(3)
220
221 b1.pack(side=Tix.LEFT, padx=4, pady=4)
222 b2.pack(side=Tix.LEFT, padx=4, pady=4)
223
224 demo.balloon.bind_widget(b1, msg='Choose\na font',
225 statusmsg='Choose a font for this page')
226 demo.balloon.bind_widget(b2, msg='Point size',
227 statusmsg='Choose the font size for this page')
228 return w
229
230def MkWelcomeText(top):
231 global demo
232
233 w = Tix.ScrolledWindow(top, scrollbar='auto')
234 win = w.window
235 text = 'Welcome to TIX in Python'
Martin v. Löwis20efa682001-11-11 14:07:37 +0000236 title = Tix.Label(win,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000237 bd=0, width=30, anchor=Tix.N, text=text)
Martin v. Löwis20efa682001-11-11 14:07:37 +0000238 msg = Tix.Message(win,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000239 bd=0, width=400, anchor=Tix.N,
240 text='Tix is a set of mega-widgets based on TK. This program \
241demonstrates the widgets in the Tix widget set. You can choose the pages \
242in this window to look at the corresponding widgets. \n\n\
243To quit this program, choose the "File | Exit" command.\n\n\
244For more information, see http://tix.sourceforge.net.')
245 title.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
246 msg.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
247 demo.welmsg = msg
248 return w
249
250def MainTextFont(w):
251 global demo
252
253 if not demo.welmsg:
254 return
255 font = demo.welfont['value']
256 point = demo.welsize['value']
257 if font == 'Times Roman':
258 font = 'times'
Martin v. Löwis20efa682001-11-11 14:07:37 +0000259 fontstr = '%s %s' % (font, point)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000260 demo.welmsg['font'] = fontstr
261
262def ToggleHelp():
263 if demo.useBalloons.get() == '1':
264 demo.balloon['state'] = 'both'
265 else:
266 demo.balloon['state'] = 'none'
267
268def MkChoosers(nb, name):
269 w = nb.page(name)
Neal Norwitzac30ead2002-11-14 02:44:08 +0000270 options = "label.padX 4"
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000271
Neal Norwitzac30ead2002-11-14 02:44:08 +0000272 til = Tix.LabelFrame(w, label='Chooser Widgets', options=options)
273 cbx = Tix.LabelFrame(w, label='tixComboBox', options=options)
274 ctl = Tix.LabelFrame(w, label='tixControl', options=options)
275 sel = Tix.LabelFrame(w, label='tixSelect', options=options)
276 opt = Tix.LabelFrame(w, label='tixOptionMenu', options=options)
277 fil = Tix.LabelFrame(w, label='tixFileEntry', options=options)
278 fbx = Tix.LabelFrame(w, label='tixFileSelectBox', options=options)
279 tbr = Tix.LabelFrame(w, label='Tool Bar', options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000280
281 MkTitle(til.frame)
282 MkCombo(cbx.frame)
283 MkControl(ctl.frame)
284 MkSelect(sel.frame)
285 MkOptMenu(opt.frame)
286 MkFileEnt(fil.frame)
287 MkFileBox(fbx.frame)
288 MkToolBar(tbr.frame)
289
290 # First column: comBox and selector
291 cbx.form(top=0, left=0, right='%33')
292 sel.form(left=0, right='&'+str(cbx), top=cbx)
293 opt.form(left=0, right='&'+str(cbx), top=sel, bottom=-1)
294
295 # Second column: title .. etc
296 til.form(left=cbx, top=0,right='%66')
297 ctl.form(left=cbx, right='&'+str(til), top=til)
298 fil.form(left=cbx, right='&'+str(til), top=ctl)
299 tbr.form(left=cbx, right='&'+str(til), top=fil, bottom=-1)
300
301 #
302 # Third column: file selection
303 fbx.form(right=-1, top=0, left='%66')
304
305def MkCombo(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000306 options="label.width %d label.anchor %s entry.width %d" % (10, Tix.E, 14)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000307
Neal Norwitzac30ead2002-11-14 02:44:08 +0000308 static = Tix.ComboBox(w, label='Static', editable=0, options=options)
309 editable = Tix.ComboBox(w, label='Editable', editable=1, options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000310 history = Tix.ComboBox(w, label='History', editable=1, history=1,
Neal Norwitzac30ead2002-11-14 02:44:08 +0000311 anchor=Tix.E, options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000312 static.insert(Tix.END, 'January')
313 static.insert(Tix.END, 'February')
314 static.insert(Tix.END, 'March')
315 static.insert(Tix.END, 'April')
316 static.insert(Tix.END, 'May')
317 static.insert(Tix.END, 'June')
318 static.insert(Tix.END, 'July')
319 static.insert(Tix.END, 'August')
320 static.insert(Tix.END, 'September')
321 static.insert(Tix.END, 'October')
322 static.insert(Tix.END, 'November')
323 static.insert(Tix.END, 'December')
324
325 editable.insert(Tix.END, 'Angola')
326 editable.insert(Tix.END, 'Bangladesh')
327 editable.insert(Tix.END, 'China')
328 editable.insert(Tix.END, 'Denmark')
329 editable.insert(Tix.END, 'Ecuador')
330
331 history.insert(Tix.END, '/usr/bin/ksh')
332 history.insert(Tix.END, '/usr/local/lib/python')
333 history.insert(Tix.END, '/var/adm')
334
335 static.pack(side=Tix.TOP, padx=5, pady=3)
336 editable.pack(side=Tix.TOP, padx=5, pady=3)
337 history.pack(side=Tix.TOP, padx=5, pady=3)
338
339states = ['Bengal', 'Delhi', 'Karnataka', 'Tamil Nadu']
340
341def spin_cmd(w, inc):
342 idx = states.index(demo_spintxt.get()) + inc
343 if idx < 0:
344 idx = len(states) - 1
345 elif idx >= len(states):
346 idx = 0
347# following doesn't work.
348# return states[idx]
349 demo_spintxt.set(states[idx]) # this works
350
351def spin_validate(w):
352 global states, demo_spintxt
353
354 try:
355 i = states.index(demo_spintxt.get())
Fred Drake7def2562001-05-11 19:44:55 +0000356 except ValueError:
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000357 return states[0]
358 return states[i]
359 # why this procedure works as opposed to the previous one beats me.
360
361def MkControl(w):
362 global demo_spintxt
363
Neal Norwitzac30ead2002-11-14 02:44:08 +0000364 options="label.width %d label.anchor %s entry.width %d" % (10, Tix.E, 13)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000365
366 demo_spintxt = Tix.StringVar()
367 demo_spintxt.set(states[0])
Neal Norwitzac30ead2002-11-14 02:44:08 +0000368 simple = Tix.Control(w, label='Numbers', options=options)
369 spintxt = Tix.Control(w, label='States', variable=demo_spintxt,
370 options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000371 spintxt['incrcmd'] = lambda w=spintxt: spin_cmd(w, 1)
372 spintxt['decrcmd'] = lambda w=spintxt: spin_cmd(w, -1)
373 spintxt['validatecmd'] = lambda w=spintxt: spin_validate(w)
374
375 simple.pack(side=Tix.TOP, padx=5, pady=3)
376 spintxt.pack(side=Tix.TOP, padx=5, pady=3)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000377
Neal Norwitzac30ead2002-11-14 02:44:08 +0000378def MkSelect(w):
379 options = "label.anchor %s" % Tix.CENTER
380
381 sel1 = Tix.Select(w, label='Mere Mortals', allowzero=1, radio=1,
382 orientation=Tix.VERTICAL,
383 labelside=Tix.TOP,
384 options=options)
385 sel2 = Tix.Select(w, label='Geeks', allowzero=1, radio=0,
386 orientation=Tix.VERTICAL,
387 labelside= Tix.TOP,
388 options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000389
390 sel1.add('eat', text='Eat')
391 sel1.add('work', text='Work')
392 sel1.add('play', text='Play')
393 sel1.add('party', text='Party')
394 sel1.add('sleep', text='Sleep')
395
396 sel2.add('eat', text='Eat')
397 sel2.add('prog1', text='Program')
398 sel2.add('prog2', text='Program')
399 sel2.add('prog3', text='Program')
400 sel2.add('sleep', text='Sleep')
401
402 sel1.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
403 sel2.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
404
405def MkOptMenu(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000406 options='menubutton.width 15 label.anchor %s' % Tix.E
407
408 m = Tix.OptionMenu(w, label='File Format : ', options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000409 m.add_command('text', label='Plain Text')
410 m.add_command('post', label='PostScript')
411 m.add_command('format', label='Formatted Text')
412 m.add_command('html', label='HTML')
413 m.add_command('sep')
414 m.add_command('tex', label='LaTeX')
415 m.add_command('rtf', label='Rich Text Format')
416
417 m.pack(fill=Tix.X, padx=5, pady=3)
418
419def MkFileEnt(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000420 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000421 relief=Tix.FLAT, width=240, anchor=Tix.N,
422 text='Press the "open file" icon button and a TixFileSelectDialog will popup.')
423 ent = Tix.FileEntry(w, label='Select a file : ')
424 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
425 ent.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
426
427def MkFileBox(w):
Martin v. Löwis8ec03e02002-03-17 18:19:13 +0000428 """The FileSelectBox is a Motif-style box with various enhancements.
429 For example, you can adjust the size of the two listboxes
430 and your past selections are recorded.
431 """
Neal Norwitzac30ead2002-11-14 02:44:08 +0000432 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000433 relief=Tix.FLAT, width=240, anchor=Tix.N,
Martin v. Löwis8ec03e02002-03-17 18:19:13 +0000434 text='The Tix FileSelectBox is a Motif-style box with various enhancements. For example, you can adjust the size of the two listboxes and your past selections are recorded.')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000435 box = Tix.FileSelectBox(w)
436 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
437 box.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
438
439def MkToolBar(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000440 """The Select widget is also good for arranging buttons in a tool bar.
441 """
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000442 global demo
443
Neal Norwitzac30ead2002-11-14 02:44:08 +0000444 options='frame.borderWidth 1'
445
446 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000447 relief=Tix.FLAT, width=240, anchor=Tix.N,
448 text='The Select widget is also good for arranging buttons in a tool bar.')
449 bar = Tix.Frame(w, bd=2, relief=Tix.RAISED)
Neal Norwitzac30ead2002-11-14 02:44:08 +0000450 font = Tix.Select(w, allowzero=1, radio=0, label='', options=options)
451 para = Tix.Select(w, allowzero=0, radio=1, label='', options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000452
453 font.add('bold', bitmap='@' + demo.dir + '/bitmaps/bold.xbm')
454 font.add('italic', bitmap='@' + demo.dir + '/bitmaps/italic.xbm')
455 font.add('underline', bitmap='@' + demo.dir + '/bitmaps/underline.xbm')
456 font.add('capital', bitmap='@' + demo.dir + '/bitmaps/capital.xbm')
457
458 para.add('left', bitmap='@' + demo.dir + '/bitmaps/leftj.xbm')
459 para.add('right', bitmap='@' + demo.dir + '/bitmaps/rightj.xbm')
460 para.add('center', bitmap='@' + demo.dir + '/bitmaps/centerj.xbm')
461 para.add('justify', bitmap='@' + demo.dir + '/bitmaps/justify.xbm')
462
463 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
464 bar.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
465 font.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
466 para.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
467
468def MkTitle(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000469 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000470 relief=Tix.FLAT, width=240, anchor=Tix.N,
471 text='There are many types of "chooser" widgets that allow the user to input different types of information')
472 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
473
474def MkScroll(nb, name):
475 w = nb.page(name)
Neal Norwitzac30ead2002-11-14 02:44:08 +0000476 options='label.padX 4'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000477
Martin v. Löwis46874282002-12-06 10:33:45 +0000478 sls = Tix.LabelFrame(w, label='Tix.ScrolledListBox', options=options)
479 swn = Tix.LabelFrame(w, label='Tix.ScrolledWindow', options=options)
480 stx = Tix.LabelFrame(w, label='Tix.ScrolledText', options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000481
482 MkSList(sls.frame)
483 MkSWindow(swn.frame)
484 MkSText(stx.frame)
485
486 sls.form(top=0, left=0, right='%33', bottom=-1)
487 swn.form(top=0, left=sls, right='%66', bottom=-1)
488 stx.form(top=0, left=swn, right=-1, bottom=-1)
489
Neal Norwitzac30ead2002-11-14 02:44:08 +0000490
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000491def MkSList(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000492 """This TixScrolledListBox is configured so that it uses scrollbars
493 only when it is necessary. Use the handles to resize the listbox and
494 watch the scrollbars automatically appear and disappear. """
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000495 top = Tix.Frame(w, width=300, height=330)
496 bot = Tix.Frame(w)
Neal Norwitzac30ead2002-11-14 02:44:08 +0000497 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000498 relief=Tix.FLAT, width=200, anchor=Tix.N,
499 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.')
500
501 list = Tix.ScrolledListBox(top, scrollbar='auto')
502 list.place(x=50, y=150, width=120, height=80)
503 list.listbox.insert(Tix.END, 'Alabama')
504 list.listbox.insert(Tix.END, 'California')
505 list.listbox.insert(Tix.END, 'Montana')
506 list.listbox.insert(Tix.END, 'New Jersey')
507 list.listbox.insert(Tix.END, 'New York')
508 list.listbox.insert(Tix.END, 'Pennsylvania')
509 list.listbox.insert(Tix.END, 'Washington')
510
511 rh = Tix.ResizeHandle(top, bg='black',
512 relief=Tix.RAISED,
513 handlesize=8, gridded=1, minwidth=50, minheight=30)
514 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=list: SList_reset(w,x))
515 top.propagate(0)
516 msg.pack(fill=Tix.X)
517 btn.pack(anchor=Tix.CENTER)
518 top.pack(expand=1, fill=Tix.BOTH)
519 bot.pack(fill=Tix.BOTH)
520 list.bind('<Map>', func=lambda arg=0, rh=rh, list=list:
521 list.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(list)))
522
523def SList_reset(rh, list):
524 list.place(x=50, y=150, width=120, height=80)
525 list.update()
526 rh.attach_widget(list)
527
528def MkSWindow(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000529 """The ScrolledWindow widget allows you to scroll any kind of Tk
530 widget. It is more versatile than a scrolled canvas widget.
531 """
532 global demo
Martin v. Löwis652e1912001-11-25 14:50:56 +0000533
Neal Norwitzac30ead2002-11-14 02:44:08 +0000534 text = 'The Tix ScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.'
535
Martin v. Löwis652e1912001-11-25 14:50:56 +0000536 file = os.path.join(demo.dir, 'bitmaps', 'tix.gif')
537 if not os.path.isfile(file):
538 text += ' (Image missing)'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000539
540 top = Tix.Frame(w, width=330, height=330)
541 bot = Tix.Frame(w)
Neal Norwitzac30ead2002-11-14 02:44:08 +0000542 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000543 relief=Tix.FLAT, width=200, anchor=Tix.N,
Martin v. Löwis652e1912001-11-25 14:50:56 +0000544 text=text)
545
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000546 win = Tix.ScrolledWindow(top, scrollbar='auto')
Martin v. Löwis652e1912001-11-25 14:50:56 +0000547
Neal Norwitzac30ead2002-11-14 02:44:08 +0000548 global image1
549 # This image is not showing up in the Label unless it is set to a
550 # global variable - no problem under Tcl/Tix. It is being
551 # garbage collected at the end of this proecedure if not global
Martin v. Löwis652e1912001-11-25 14:50:56 +0000552 image1 = Tix.Image('photo', file=file)
553 lbl = Tix.Label(win.window, image=image1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000554 lbl.pack(expand=1, fill=Tix.BOTH)
555
556 win.place(x=30, y=150, width=190, height=120)
557
558 rh = Tix.ResizeHandle(top, bg='black',
559 relief=Tix.RAISED,
560 handlesize=8, gridded=1, minwidth=50, minheight=30)
561 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SWindow_reset(w,x))
562 top.propagate(0)
563 msg.pack(fill=Tix.X)
564 btn.pack(anchor=Tix.CENTER)
565 top.pack(expand=1, fill=Tix.BOTH)
566 bot.pack(fill=Tix.BOTH)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000567
568def SWindow_reset(rh, win):
569 win.place(x=30, y=150, width=190, height=120)
570 win.update()
571 rh.attach_widget(win)
572
573def MkSText(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000574 """The TixScrolledWindow widget allows you to scroll any kind of Tk
575 widget. It is more versatile than a scrolled canvas widget."""
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000576 top = Tix.Frame(w, width=330, height=330)
577 bot = Tix.Frame(w)
Neal Norwitzac30ead2002-11-14 02:44:08 +0000578 msg = Tix.Message(top,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000579 relief=Tix.FLAT, width=200, anchor=Tix.N,
Neal Norwitzac30ead2002-11-14 02:44:08 +0000580 text='The Tix ScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000581
582 win = Tix.ScrolledText(top, scrollbar='auto')
583# win.text['wrap'] = 'none'
584 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.')
585 win.place(x=30, y=150, width=190, height=100)
586
587 rh = Tix.ResizeHandle(top, bg='black',
588 relief=Tix.RAISED,
589 handlesize=8, gridded=1, minwidth=50, minheight=30)
590 btn = Tix.Button(bot, text='Reset', command=lambda w=rh, x=win: SText_reset(w,x))
591 top.propagate(0)
592 msg.pack(fill=Tix.X)
593 btn.pack(anchor=Tix.CENTER)
594 top.pack(expand=1, fill=Tix.BOTH)
595 bot.pack(fill=Tix.BOTH)
596 win.bind('<Map>', func=lambda arg=0, rh=rh, win=win:
597 win.tk.call('tixDoWhenIdle', str(rh), 'attachwidget', str(win)))
598
599def SText_reset(rh, win):
600 win.place(x=30, y=150, width=190, height=120)
601 win.update()
602 rh.attach_widget(win)
603
604def MkManager(nb, name):
605 w = nb.page(name)
Neal Norwitzac30ead2002-11-14 02:44:08 +0000606 options='label.padX 4'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000607
Martin v. Löwis46874282002-12-06 10:33:45 +0000608 pane = Tix.LabelFrame(w, label='Tix.PanedWindow', options=options)
609 note = Tix.LabelFrame(w, label='Tix.NoteBook', options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000610
611 MkPanedWindow(pane.frame)
612 MkNoteBook(note.frame)
613
614 pane.form(top=0, left=0, right=note, bottom=-1)
615 note.form(top=0, right=-1, bottom=-1)
616
617def MkPanedWindow(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000618 """The PanedWindow widget allows the user to interactively manipulate
619 the sizes of several panes. The panes can be arranged either vertically
620 or horizontally.
621 """
622 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000623 relief=Tix.FLAT, width=240, anchor=Tix.N,
624 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 +0000625 group = Tix.LabelEntry(w, label='Newsgroup:', options='entry.width 25')
626 group.entry.insert(0,'comp.lang.python')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000627 pane = Tix.PanedWindow(w, orientation='vertical')
628
629 p1 = pane.add('list', min=70, size=100)
630 p2 = pane.add('text', min=70)
631 list = Tix.ScrolledListBox(p1)
632 text = Tix.ScrolledText(p2)
633
Martin v. Löwis652e1912001-11-25 14:50:56 +0000634 list.listbox.insert(Tix.END, " 12324 Re: Tkinter is good for your health")
635 list.listbox.insert(Tix.END, "+ 12325 Re: Tkinter is good for your health")
636 list.listbox.insert(Tix.END, "+ 12326 Re: Tix is even better for your health (Was: Tkinter is good...)")
637 list.listbox.insert(Tix.END, " 12327 Re: Tix is even better for your health (Was: Tkinter is good...)")
638 list.listbox.insert(Tix.END, "+ 12328 Re: Tix is even better for your health (Was: Tkinter is good...)")
639 list.listbox.insert(Tix.END, " 12329 Re: Tix is even better for your health (Was: Tkinter is good...)")
640 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 +0000641
642 text.text['bg'] = list.listbox['bg']
643 text.text['wrap'] = 'none'
644 text.text.insert(Tix.END, """
Martin v. Löwis652e1912001-11-25 14:50:56 +0000645Mon, 19 Jun 1995 11:39:52 comp.lang.python Thread 34 of 220
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000646Lines 353 A new way to put text and bitmaps together iNo responses
647ioi@blue.seas.upenn.edu Ioi K. Lam at University of Pennsylvania
648
649Hi,
650
651I have implemented a new image type called "compound". It allows you
652to glue together a bunch of bitmaps, images and text strings together
653to form a bigger image. Then you can use this image with widgets that
654support the -image option. For example, you can display a text string string
655together with a bitmap, at the same time, inside a TK button widget.
656""")
657 list.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
658 text.pack(expand=1, fill=Tix.BOTH, padx=4, pady=6)
659
660 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
661 group.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
662 pane.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH, expand=1)
663
664def MkNoteBook(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000665 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000666 relief=Tix.FLAT, width=240, anchor=Tix.N,
667 text='The NoteBook widget allows you to layout a complex interface into individual pages.')
Neal Norwitzac30ead2002-11-14 02:44:08 +0000668 # prefix = Tix.OptionName(w)
669 # if not prefix: prefix = ''
670 # w.option_add('*' + prefix + '*TixNoteBook*tagPadX', 8)
671 options = "entry.width %d label.width %d label.anchor %s" % (10, 18, Tix.E)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000672
Neal Norwitzac30ead2002-11-14 02:44:08 +0000673 nb = Tix.NoteBook(w, ipadx=6, ipady=6, options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000674 nb.add('hard_disk', label="Hard Disk", underline=0)
675 nb.add('network', label="Network", underline=0)
676
677 # Frame for the buttons that are present on all pages
678 common = Tix.Frame(nb.hard_disk)
679 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
680 CreateCommonButtons(common)
681
682 # Widgets belonging only to this page
683 a = Tix.Control(nb.hard_disk, value=12, label='Access Time: ')
684 w = Tix.Control(nb.hard_disk, value=400, label='Write Throughput: ')
685 r = Tix.Control(nb.hard_disk, value=400, label='Read Throughput: ')
686 c = Tix.Control(nb.hard_disk, value=1021, label='Capacity: ')
687 a.pack(side=Tix.TOP, padx=20, pady=2)
688 w.pack(side=Tix.TOP, padx=20, pady=2)
689 r.pack(side=Tix.TOP, padx=20, pady=2)
690 c.pack(side=Tix.TOP, padx=20, pady=2)
691
692 common = Tix.Frame(nb.network)
693 common.pack(side=Tix.RIGHT, padx=2, pady=2, fill=Tix.Y)
694 CreateCommonButtons(common)
695
696 a = Tix.Control(nb.network, value=12, label='Access Time: ')
697 w = Tix.Control(nb.network, value=400, label='Write Throughput: ')
698 r = Tix.Control(nb.network, value=400, label='Read Throughput: ')
699 c = Tix.Control(nb.network, value=1021, label='Capacity: ')
700 u = Tix.Control(nb.network, value=10, label='Users: ')
701 a.pack(side=Tix.TOP, padx=20, pady=2)
702 w.pack(side=Tix.TOP, padx=20, pady=2)
703 r.pack(side=Tix.TOP, padx=20, pady=2)
704 c.pack(side=Tix.TOP, padx=20, pady=2)
705 u.pack(side=Tix.TOP, padx=20, pady=2)
706
707 msg.pack(side=Tix.TOP, padx=3, pady=3, fill=Tix.BOTH)
708 nb.pack(side=Tix.TOP, padx=5, pady=5, fill=Tix.BOTH, expand=1)
709
710def CreateCommonButtons(f):
711 ok = Tix.Button(f, text='OK', width = 6)
712 cancel = Tix.Button(f, text='Cancel', width = 6)
713 ok.pack(side=Tix.TOP, padx=2, pady=2)
714 cancel.pack(side=Tix.TOP, padx=2, pady=2)
715
716def MkDirList(nb, name):
717 w = nb.page(name)
Neal Norwitzac30ead2002-11-14 02:44:08 +0000718 options = "label.padX 4"
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000719
Martin v. Löwis46874282002-12-06 10:33:45 +0000720 dir = Tix.LabelFrame(w, label='Tix.DirList', options=options)
721 fsbox = Tix.LabelFrame(w, label='Tix.ExFileSelectBox', options=options)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000722 MkDirListWidget(dir.frame)
723 MkExFileWidget(fsbox.frame)
724 dir.form(top=0, left=0, right='%40', bottom=-1)
725 fsbox.form(top=0, left='%40', right=-1, bottom=-1)
726
727def MkDirListWidget(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000728 """The TixDirList widget gives a graphical representation of the file
729 system directory and makes it easy for the user to choose and access
730 directories.
731 """
732 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000733 relief=Tix.FLAT, width=240, anchor=Tix.N,
Neal Norwitzac30ead2002-11-14 02:44:08 +0000734 text='The Tix DirList widget gives a graphical representation of the file system directory and makes it easy for the user to choose and access directories.')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000735 dirlist = Tix.DirList(w, options='hlist.padY 1 hlist.width 25 hlist.height 16')
736 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
737 dirlist.pack(side=Tix.TOP, padx=3, pady=3)
738
739def MkExFileWidget(w):
Neal Norwitzac30ead2002-11-14 02:44:08 +0000740 """The TixExFileSelectBox widget is more user friendly than the Motif
741 style FileSelectBox. """
742 msg = Tix.Message(w,
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000743 relief=Tix.FLAT, width=240, anchor=Tix.N,
Neal Norwitzac30ead2002-11-14 02:44:08 +0000744 text='The Tix ExFileSelectBox widget is more user friendly than the Motif style FileSelectBox.')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000745 # There's a bug in the ComboBoxes - the scrolledlistbox is destroyed
746 box = Tix.ExFileSelectBox(w, bd=2, relief=Tix.RAISED)
747 msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
748 box.pack(side=Tix.TOP, padx=3, pady=3)
749
750###
751### List of all the demos we want to show off
752comments = {'widget' : 'Widget Demos', 'image' : 'Image Demos'}
753samples = {'Balloon' : 'Balloon',
754 'Button Box' : 'BtnBox',
755 'Combo Box' : 'ComboBox',
756 'Compound Image' : 'CmpImg',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000757 'Directory List' : 'DirList',
758 'Directory Tree' : 'DirTree',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000759 'Control' : 'Control',
760 'Notebook' : 'NoteBook',
761 'Option Menu' : 'OptMenu',
Martin v. Löwis652e1912001-11-25 14:50:56 +0000762 'Paned Window' : 'PanedWin',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000763 'Popup Menu' : 'PopMenu',
764 'ScrolledHList (1)' : 'SHList1',
765 'ScrolledHList (2)' : 'SHList2',
766 'Tree (dynamic)' : 'Tree'
767}
768
Martin v. Löwis20efa682001-11-11 14:07:37 +0000769# There are still a lot of demos to be translated:
770## set root {
771## {d "File Selectors" file }
772## {d "Hierachical ListBox" hlist }
773## {d "Tabular ListBox" tlist {c tixTList}}
774## {d "Grid Widget" grid {c tixGrid}}
775## {d "Manager Widgets" manager }
776## {d "Scrolled Widgets" scroll }
777## {d "Miscellaneous Widgets" misc }
778## {d "Image Types" image }
779## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000780##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000781## set image {
782## {d "Compound Image" cmpimg }
783## {d "XPM Image" xpm {i pixmap}}
784## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000785##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000786## set cmpimg {
Neal Norwitzac30ead2002-11-14 02:44:08 +0000787##done {f "In Buttons" CmpImg.tcl }
Martin v. Löwis20efa682001-11-11 14:07:37 +0000788## {f "In NoteBook" CmpImg2.tcl }
789## {f "Notebook Color Tabs" CmpImg4.tcl }
790## {f "Icons" CmpImg3.tcl }
791## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000792##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000793## set xpm {
794## {f "In Button" Xpm.tcl {i pixmap}}
795## {f "In Menu" Xpm1.tcl {i pixmap}}
796## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000797##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000798## set file {
799##added {f DirList DirList.tcl }
800##added {f DirTree DirTree.tcl }
801## {f DirSelectDialog DirDlg.tcl }
802## {f ExFileSelectDialog EFileDlg.tcl }
803## {f FileSelectDialog FileDlg.tcl }
804## {f FileEntry FileEnt.tcl }
805## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000806##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000807## set hlist {
808## {f HList HList1.tcl }
809## {f CheckList ChkList.tcl {c tixCheckList}}
810##done {f "ScrolledHList (1)" SHList.tcl }
811##done {f "ScrolledHList (2)" SHList2.tcl }
812##done {f Tree Tree.tcl }
813##done {f "Tree (Dynamic)" DynTree.tcl {v win}}
814## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000815##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000816## set tlist {
817## {f "ScrolledTList (1)" STList1.tcl {c tixTList}}
818## {f "ScrolledTList (2)" STList2.tcl {c tixTList}}
819## }
820## global tcl_platform
821## # This demo hangs windows
822## if {$tcl_platform(platform) != "windows"} {
823##na lappend tlist {f "TList File Viewer" STList3.tcl {c tixTList}}
824## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000825##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000826## set grid {
827##na {f "Simple Grid" SGrid0.tcl {c tixGrid}}
828##na {f "ScrolledGrid" SGrid1.tcl {c tixGrid}}
829##na {f "Editable Grid" EditGrid.tcl {c tixGrid}}
830## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000831##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000832## set scroll {
833## {f ScrolledListBox SListBox.tcl }
834## {f ScrolledText SText.tcl }
835## {f ScrolledWindow SWindow.tcl }
836##na {f "Canvas Object View" CObjView.tcl {c tixCObjView}}
837## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000838##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000839## set manager {
Neal Norwitzac30ead2002-11-14 02:44:08 +0000840## {f ListNoteBook ListNBK.tcl }
Martin v. Löwis652e1912001-11-25 14:50:56 +0000841##done {f NoteBook NoteBook.tcl }
842##done {f PanedWindow PanedWin.tcl }
Martin v. Löwis20efa682001-11-11 14:07:37 +0000843## }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000844##
Martin v. Löwis20efa682001-11-11 14:07:37 +0000845## set misc {
846##done {f Balloon Balloon.tcl }
847##done {f ButtonBox BtnBox.tcl }
848##done {f ComboBox ComboBox.tcl }
849##done {f Control Control.tcl }
850## {f LabelEntry LabEntry.tcl }
851## {f LabelFrame LabFrame.tcl }
Neal Norwitzac30ead2002-11-14 02:44:08 +0000852## {f Meter Meter.tcl {c tixMeter}}
Martin v. Löwis20efa682001-11-11 14:07:37 +0000853##done {f OptionMenu OptMenu.tcl }
854##done {f PopupMenu PopMenu.tcl }
855## {f Select Select.tcl }
856## {f StdButtonBox StdBBox.tcl }
857## }
858##
859
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000860stypes = {}
861stypes['widget'] = ['Balloon', 'Button Box', 'Combo Box', 'Control',
Martin v. Löwis20efa682001-11-11 14:07:37 +0000862 'Directory List', 'Directory Tree',
Martin v. Löwis652e1912001-11-25 14:50:56 +0000863 'Notebook', 'Option Menu', 'Popup Menu', 'Paned Window',
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000864 'ScrolledHList (1)', 'ScrolledHList (2)', 'Tree (dynamic)']
865stypes['image'] = ['Compound Image']
866
867def MkSample(nb, name):
868 w = nb.page(name)
Neal Norwitzac30ead2002-11-14 02:44:08 +0000869 options = "label.padX 4"
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000870
Martin v. Löwis652e1912001-11-25 14:50:56 +0000871 pane = Tix.PanedWindow(w, orientation='horizontal')
872 pane.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH)
873 f1 = pane.add('list', expand='1')
874 f2 = pane.add('text', expand='5')
875 f1['relief'] = 'flat'
876 f2['relief'] = 'flat'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000877
Neal Norwitzac30ead2002-11-14 02:44:08 +0000878 lab = Tix.LabelFrame(f1, label='Select a sample program:')
879 lab.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=5, pady=5)
880 lab1 = Tix.LabelFrame(f2, label='Source:')
881 lab1.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=5, pady=5)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000882
Neal Norwitzac30ead2002-11-14 02:44:08 +0000883 slb = Tix.Tree(lab.frame, options='hlist.width 20')
Martin v. Löwis652e1912001-11-25 14:50:56 +0000884 slb.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=5)
885
Neal Norwitzac30ead2002-11-14 02:44:08 +0000886 stext = Tix.ScrolledText(lab1.frame, name='stext')
Martin v. Löwis20efa682001-11-11 14:07:37 +0000887 font = root.tk.eval('tix option get fixed_font')
888 stext.text.config(font=font)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000889
Neal Norwitzac30ead2002-11-14 02:44:08 +0000890 frame = Tix.Frame(lab1.frame, name='frame')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000891
Martin v. Löwis652e1912001-11-25 14:50:56 +0000892 run = Tix.Button(frame, text='Run ...', name='run')
893 view = Tix.Button(frame, text='View Source ...', name='view')
894 run.pack(side=Tix.LEFT, expand=0, fill=Tix.NONE)
895 view.pack(side=Tix.LEFT, expand=0, fill=Tix.NONE)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000896
897 stext.text['bg'] = slb.hlist['bg']
898 stext.text['state'] = 'disabled'
899 stext.text['wrap'] = 'none'
Martin v. Löwis652e1912001-11-25 14:50:56 +0000900 stext.text['width'] = 80
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000901
Neal Norwitzac30ead2002-11-14 02:44:08 +0000902 frame.pack(side=Tix.BOTTOM, expand=0, fill=Tix.X, padx=7)
903 stext.pack(side=Tix.TOP, expand=0, fill=Tix.BOTH, padx=7)
904
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +0000905 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