/Demo/tix/tixwidgets.py

http://unladen-swallow.googlecode.com/ · Python · 1003 lines · 697 code · 139 blank · 167 comment · 38 complexity · 4f3e85a6034f890908cd07e129daf860 MD5 · raw file

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