# -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
# For Tix, see http://tix.sourceforge.net
# This is a demo program of some of the Tix widgets available in Python.
# If you have installed Python & Tix properly, you can execute this as
import os, os.path, sys, Tix
from Tkconstants import *
import traceback, tkMessageBox
self.dir = None # script directory
self.balloon = None # balloon widget
self.useBalloons = Tix.StringVar()
self.useBalloons.set('0')
self.statusbar = None # status bar widget
self.welmsg = None # Msg widget
self.welfont = '' # font name
self.welsize = '' # font size
dirname = os.path.dirname(progname)
if dirname and dirname != os.curdir:
for i in range(len(sys.path)):
sys.path[index] = dirname
sys.path.insert(0, dirname)
sys.path.insert(0, self.dir+'/samples')
w = Tix.Frame(top, bd=2, relief=RAISED)
file = Tix.Menubutton(w, text='File', underline=0, takefocus=0)
help = Tix.Menubutton(w, text='Help', underline=0, takefocus=0)
fm = Tix.Menu(file, tearoff=0)
hm = Tix.Menu(help, tearoff=0)
fm.add_command(label='Exit', underline=1,
command = lambda self=self: self.quitcmd () )
hm.add_checkbutton(label='BalloonHelp', underline=0, command=ToggleHelp,
variable=self.useBalloons)
# The trace variable option doesn't seem to work, instead I use 'command'
#apply(w.tk.call, ('trace', 'variable', self.useBalloons, 'w',
def MkMainNotebook(self):
w = Tix.NoteBook(top, ipadx=5, ipady=5, options="""
# This may be required if there is no *Background option
w.add('wel', label='Welcome', underline=0,
createcmd=lambda w=w, name='wel': MkWelcome(w, name))
w.add('cho', label='Choosers', underline=0,
createcmd=lambda w=w, name='cho': MkChoosers(w, name))
w.add('scr', label='Scrolled Widgets', underline=0,
createcmd=lambda w=w, name='scr': MkScroll(w, name))
w.add('mgr', label='Manager Widgets', underline=0,
createcmd=lambda w=w, name='mgr': MkManager(w, name))
w.add('dir', label='Directory List', underline=0,
createcmd=lambda w=w, name='dir': MkDirList(w, name))
w.add('exp', label='Run Sample Programs', underline=0,
createcmd=lambda w=w, name='exp': MkSample(w, name))
w = Tix.Frame(top, relief=Tix.RAISED, bd=1)
demo.statusbar = Tix.Label(w, relief=Tix.SUNKEN, bd=1)
demo.statusbar.form(padx=3, pady=3, left=0, right='%70')
z = root.winfo_toplevel()
z.wm_title('Tix Widget Demonstration')
if z.winfo_screenwidth() <= 800:
z.geometry('790x590+10+10')
z.geometry('890x640+10+10')
demo.balloon = Tix.Balloon(root)
frame1 = self.MkMainMenu()
frame2 = self.MkMainNotebook()
frame3 = self.MkMainStatus()
frame1.pack(side=TOP, fill=X)
frame3.pack(side=BOTTOM, fill=X)
frame2.pack(side=TOP, expand=1, fill=BOTH, padx=4, pady=4)
demo.balloon['statusbar'] = demo.statusbar
z.wm_protocol("WM_DELETE_WINDOW", lambda self=self: self.quitcmd())
# To show Tcl errors - uncomment this to see the listbox bug.
# Tkinter defines a Tcl tkerror procedure that in effect
# silences all background Tcl error reporting.
# root.tk.eval('if {[info commands tkerror] != ""} {rename tkerror pytkerror}')
"""Quit our mainloop. It is up to you to call root.destroy() after."""
"""This is an explict replacement for _tkinter mainloop()
It lets you catch keyboard interrupts easier, and avoids
the 20 msec. dead sleep() which burns a constant CPU."""
# There are 2 whiles here. The outer one lets you continue
# This is the replacement for _tkinter mainloop()
# It blocks waiting for the next Tcl event using select.
self.root.tk.dooneevent(TCL_ALL_EVENTS)
# Tkinter uses SystemExit to exit
except KeyboardInterrupt:
if tkMessageBox.askquestion ('Interrupt', 'Really Quit?') == 'yes':
# Otherwise it's some other error - be nice and say why
t, v, tb = sys.exc_info()
for line in traceback.format_exception(t,v,tb):
try: tkMessageBox.showerror ('Error', text)
bar.pack(side=TOP, fill=X, padx=2, pady=2)
text.pack(side=TOP, fill=BOTH, expand=1)
w = Tix.Frame(top, bd=2, relief=Tix.GROOVE)
b1 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
b2 = Tix.ComboBox(w, command=lambda w=top: MainTextFont(w))
b1.slistbox.listbox['height'] = 3
b2.slistbox.listbox['height'] = 3
b1.insert(Tix.END, 'Courier')
b1.insert(Tix.END, 'Helvetica')
b1.insert(Tix.END, 'Lucida')
b1.insert(Tix.END, 'Times Roman')
b1.pack(side=Tix.LEFT, padx=4, pady=4)
b2.pack(side=Tix.LEFT, padx=4, pady=4)
demo.balloon.bind_widget(b1, msg='Choose\na font',
statusmsg='Choose a font for this page')
demo.balloon.bind_widget(b2, msg='Point size',
statusmsg='Choose the font size for this page')
w = Tix.ScrolledWindow(top, scrollbar='auto')
text = 'Welcome to TIX in Python'
bd=0, width=30, anchor=Tix.N, text=text)
bd=0, width=400, anchor=Tix.N,
text='Tix is a set of mega-widgets based on TK. This program \
demonstrates the widgets in the Tix widget set. You can choose the pages \
in this window to look at the corresponding widgets. \n\n\
To quit this program, choose the "File | Exit" command.\n\n\
For more information, see http://tix.sourceforge.net.')
title.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
msg.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10)
font = demo.welfont['value']
point = demo.welsize['value']
if font == 'Times Roman':
fontstr = '%s %s' % (font, point)
demo.welmsg['font'] = fontstr
if demo.useBalloons.get() == '1':
demo.balloon['state'] = 'both'
demo.balloon['state'] = 'none'
def MkChoosers(nb, name):
til = Tix.LabelFrame(w, label='Chooser Widgets', options=options)
cbx = Tix.LabelFrame(w, label='tixComboBox', options=options)
ctl = Tix.LabelFrame(w, label='tixControl', options=options)
sel = Tix.LabelFrame(w, label='tixSelect', options=options)
opt = Tix.LabelFrame(w, label='tixOptionMenu', options=options)
fil = Tix.LabelFrame(w, label='tixFileEntry', options=options)
fbx = Tix.LabelFrame(w, label='tixFileSelectBox', options=options)
tbr = Tix.LabelFrame(w, label='Tool Bar', options=options)
# First column: comBox and selector
cbx.form(top=0, left=0, right='%33')
sel.form(left=0, right='&'+str(cbx), top=cbx)
opt.form(left=0, right='&'+str(cbx), top=sel, bottom=-1)
# Second column: title .. etc
til.form(left=cbx, top=0,right='%66')
ctl.form(left=cbx, right='&'+str(til), top=til)
fil.form(left=cbx, right='&'+str(til), top=ctl)
tbr.form(left=cbx, right='&'+str(til), top=fil, bottom=-1)
# Third column: file selection
fbx.form(right=-1, top=0, left='%66')
options="label.width %d label.anchor %s entry.width %d" % (10, Tix.E, 14)
static = Tix.ComboBox(w, label='Static', editable=0, options=options)
editable = Tix.ComboBox(w, label='Editable', editable=1, options=options)
history = Tix.ComboBox(w, label='History', editable=1, history=1,
anchor=Tix.E, options=options)
static.insert(Tix.END, 'January')
static.insert(Tix.END, 'February')
static.insert(Tix.END, 'March')
static.insert(Tix.END, 'April')
static.insert(Tix.END, 'May')
static.insert(Tix.END, 'June')
static.insert(Tix.END, 'July')
static.insert(Tix.END, 'August')
static.insert(Tix.END, 'September')
static.insert(Tix.END, 'October')
static.insert(Tix.END, 'November')
static.insert(Tix.END, 'December')
editable.insert(Tix.END, 'Angola')
editable.insert(Tix.END, 'Bangladesh')
editable.insert(Tix.END, 'China')
editable.insert(Tix.END, 'Denmark')
editable.insert(Tix.END, 'Ecuador')
history.insert(Tix.END, '/usr/bin/ksh')
history.insert(Tix.END, '/usr/local/lib/python')
history.insert(Tix.END, '/var/adm')
static.pack(side=Tix.TOP, padx=5, pady=3)
editable.pack(side=Tix.TOP, padx=5, pady=3)
history.pack(side=Tix.TOP, padx=5, pady=3)
states = ['Bengal', 'Delhi', 'Karnataka', 'Tamil Nadu']
idx = states.index(demo_spintxt.get()) + inc
# following doesn't work.
demo_spintxt.set(states[idx]) # this works
global states, demo_spintxt
i = states.index(demo_spintxt.get())
# why this procedure works as opposed to the previous one beats me.
options="label.width %d label.anchor %s entry.width %d" % (10, Tix.E, 13)
demo_spintxt = Tix.StringVar()
demo_spintxt.set(states[0])
simple = Tix.Control(w, label='Numbers', options=options)
spintxt = Tix.Control(w, label='States', variable=demo_spintxt,
spintxt['incrcmd'] = lambda w=spintxt: spin_cmd(w, 1)
spintxt['decrcmd'] = lambda w=spintxt: spin_cmd(w, -1)
spintxt['validatecmd'] = lambda w=spintxt: spin_validate(w)
simple.pack(side=Tix.TOP, padx=5, pady=3)
spintxt.pack(side=Tix.TOP, padx=5, pady=3)
options = "label.anchor %s" % Tix.CENTER
sel1 = Tix.Select(w, label='Mere Mortals', allowzero=1, radio=1,
orientation=Tix.VERTICAL,
sel2 = Tix.Select(w, label='Geeks', allowzero=1, radio=0,
orientation=Tix.VERTICAL,
sel1.add('eat', text='Eat')
sel1.add('work', text='Work')
sel1.add('play', text='Play')
sel1.add('party', text='Party')
sel1.add('sleep', text='Sleep')
sel2.add('eat', text='Eat')
sel2.add('prog1', text='Program')
sel2.add('prog2', text='Program')
sel2.add('prog3', text='Program')
sel2.add('sleep', text='Sleep')
sel1.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
sel2.pack(side=Tix.LEFT, padx=5, pady=3, fill=Tix.X)
options='menubutton.width 15 label.anchor %s' % Tix.E
m = Tix.OptionMenu(w, label='File Format : ', options=options)
m.add_command('text', label='Plain Text')
m.add_command('post', label='PostScript')
m.add_command('format', label='Formatted Text')
m.add_command('html', label='HTML')
m.add_command('tex', label='LaTeX')
m.add_command('rtf', label='Rich Text Format')
m.pack(fill=Tix.X, padx=5, pady=3)
relief=Tix.FLAT, width=240, anchor=Tix.N,
text='Press the "open file" icon button and a TixFileSelectDialog will popup.')
ent = Tix.FileEntry(w, label='Select a file : ')
msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
ent.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
"""The 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.
relief=Tix.FLAT, width=240, anchor=Tix.N,
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.')
box = Tix.FileSelectBox(w)
msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
box.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
"""The Select widget is also good for arranging buttons in a tool bar.
options='frame.borderWidth 1'
relief=Tix.FLAT, width=240, anchor=Tix.N,
text='The Select widget is also good for arranging buttons in a tool bar.')
bar = Tix.Frame(w, bd=2, relief=Tix.RAISED)
font = Tix.Select(w, allowzero=1, radio=0, label='', options=options)
para = Tix.Select(w, allowzero=0, radio=1, label='', options=options)
font.add('bold', bitmap='@' + demo.dir + '/bitmaps/bold.xbm')
font.add('italic', bitmap='@' + demo.dir + '/bitmaps/italic.xbm')
font.add('underline', bitmap='@' + demo.dir + '/bitmaps/underline.xbm')
font.add('capital', bitmap='@' + demo.dir + '/bitmaps/capital.xbm')
para.add('left', bitmap='@' + demo.dir + '/bitmaps/leftj.xbm')
para.add('right', bitmap='@' + demo.dir + '/bitmaps/rightj.xbm')
para.add('center', bitmap='@' + demo.dir + '/bitmaps/centerj.xbm')
para.add('justify', bitmap='@' + demo.dir + '/bitmaps/justify.xbm')
msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
bar.pack(side=Tix.TOP, fill=Tix.X, padx=3, pady=3)
font.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
para.pack({'in':bar}, side=Tix.LEFT, padx=3, pady=3)
relief=Tix.FLAT, width=240, anchor=Tix.N,
text='There are many types of "chooser" widgets that allow the user to input different types of information')
msg.pack(side=Tix.TOP, expand=1, fill=Tix.BOTH, padx=3, pady=3)
sls = Tix.LabelFrame(w, label='Tix.ScrolledListBox', options=options)
swn = Tix.LabelFrame(w, label='Tix.ScrolledWindow', options=options)
stx = Tix.LabelFrame(w, label='Tix.ScrolledText', options=options)
sls.form(top=0, left=0, right='%33', bottom=-1)
swn.form(top=0, left=sls, right='%66', bottom=-1)
stx.form(top=0, left=swn, right=-1, bottom=-1)
"""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. """
top = Tix.Frame(w, width=300, height=330)
relief=Tix.FLAT, width=200, anchor=Tix.N,
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.')
list = Tix.ScrolledListBox(top, scrollbar='auto')