| 1 |
"""
|
| 2 |
Module : TkUtil
|
| 3 |
Author : David Ascher <david_ascher@brown.edu>
|
| 4 |
Date : November 2, 1995
|
| 5 |
Version : 0.1
|
| 6 |
URL : http://maigret.cog.brown.edu/python/Tk/TkUtil.py
|
| 7 |
|
| 8 |
Contents:
|
| 9 |
---------
|
| 10 |
ScrollListBox:
|
| 11 |
a scrollbar+listbox combo
|
| 12 |
|
| 13 |
HorizRadioGroup, VertRadioGroup:
|
| 14 |
horizontal and vertical radiobutton groups,
|
| 15 |
|
| 16 |
HorizButtonSet, VertButtonSet:
|
| 17 |
horizontal and vertical sets of buttons
|
| 18 |
|
| 19 |
MenuBar:
|
| 20 |
quick and easy menubar management using too complex data structures,
|
| 21 |
but it works.
|
| 22 |
|
| 23 |
------------
|
| 24 |
Requirements : Tkinter
|
| 25 |
|
| 26 |
Installation : Put this file in your PYTHONPATH
|
| 27 |
|
| 28 |
Missing Features: This is a work in progress. There are plenty of
|
| 29 |
things that need to be fixed, such as making sure that people can
|
| 30 |
define the various options to widgets in the widgetdefs.
|
| 31 |
|
| 32 |
Bug reports: please send me email at david_ascher@brown.edu
|
| 33 |
|
| 34 |
"""
|
| 35 |
|
| 36 |
from Tkinter import *
|
| 37 |
from UserList import UserList
|
| 38 |
from types import IntType
|
| 39 |
import sys
|
| 40 |
|
| 41 |
class MenuBar(Frame):
|
| 42 |
def __init__(self, menus, master=None):
|
| 43 |
Frame.__init__(self, master, relief=RAISED, bd=2)
|
| 44 |
for menu in menus:
|
| 45 |
title = menu['title']
|
| 46 |
items = menu['items']
|
| 47 |
del menu['items']
|
| 48 |
del menu['title']
|
| 49 |
button = Menubutton(self, text = title)
|
| 50 |
button.pack( side = LEFT, padx = '1m')
|
| 51 |
|
| 52 |
themenu = Menu(button, menu, tearoff=0) # should make tearoff be configureable
|
| 53 |
|
| 54 |
for item in items:
|
| 55 |
try:
|
| 56 |
mtype = item['type']
|
| 57 |
del item['type']
|
| 58 |
except: mtype = 'command'
|
| 59 |
try:
|
| 60 |
mlabel = item['label']
|
| 61 |
del item['label']
|
| 62 |
except: mlabel = None
|
| 63 |
try:
|
| 64 |
mcommand = item['command']
|
| 65 |
del item['command']
|
| 66 |
except: mcommand = None
|
| 67 |
cnf = item
|
| 68 |
if mlabel: cnf['label'] = mlabel
|
| 69 |
if mcommand: cnf['command'] = mcommand
|
| 70 |
|
| 71 |
themenu.add(mtype, cnf)
|
| 72 |
|
| 73 |
button['menu'] = themenu
|
| 74 |
|
| 75 |
|
| 76 |
class RadioGroup(Frame):
|
| 77 |
def __init__(self, elements, default, theanchor = W, master = None):
|
| 78 |
Frame.__init__(self, master)
|
| 79 |
self.elements = []
|
| 80 |
for i in range(len(elements)):
|
| 81 |
e = elements[i]
|
| 82 |
thetext = e['text']
|
| 83 |
thevar = e['variable']
|
| 84 |
b = Radiobutton(self, text=thetext, variable=thevar,
|
| 85 |
value=`i`, anchor=theanchor)
|
| 86 |
self.elements.append(b)
|
| 87 |
if i == default:
|
| 88 |
self.elements[i].select()
|
| 89 |
|
| 90 |
if theanchor == W:
|
| 91 |
theside = LEFT
|
| 92 |
else:
|
| 93 |
theside = TOP
|
| 94 |
|
| 95 |
self.elements[i].pack(side=theside, expand=1, fill=BOTH)
|
| 96 |
|
| 97 |
|
| 98 |
class HorizRadioGroup(RadioGroup):
|
| 99 |
def __init__(self, elements, thedefault, master = None):
|
| 100 |
RadioGroup.__init__(self, elements, thedefault, W, master)
|
| 101 |
|
| 102 |
class VertRadioGroup(RadioGroup):
|
| 103 |
def __init__(self, elements, default, master = None):
|
| 104 |
return RadioGroup.__init__(self, elements, default, N, master)
|
| 105 |
|
| 106 |
class ButtonGroup(Frame):
|
| 107 |
def __init__(self, elements, theanchor = W, master = None):
|
| 108 |
Frame.__init__(self, master)
|
| 109 |
self.elements = []
|
| 110 |
for i in range(len(elements)):
|
| 111 |
e = elements[i]
|
| 112 |
thetext = e['text']
|
| 113 |
thecommand = e['command']
|
| 114 |
|
| 115 |
b = Button(self, text=thetext, command=thecommand)
|
| 116 |
self.elements.append(b)
|
| 117 |
if theanchor == W:
|
| 118 |
theside = LEFT
|
| 119 |
else:
|
| 120 |
theside = TOP
|
| 121 |
|
| 122 |
self.elements[i].pack(side=theside, expand=1,
|
| 123 |
anchor = S, fill=BOTH)
|
| 124 |
|
| 125 |
|
| 126 |
class HorizButtonGroup(ButtonGroup):
|
| 127 |
def __init__(self, elements, master = None):
|
| 128 |
ButtonGroup.__init__(self, elements, W, master)
|
| 129 |
|
| 130 |
class VertButtonGroup(ButtonGroup):
|
| 131 |
def __init__(self, elements, master = None):
|
| 132 |
return ButtonGroup.__init__(self, elements, N, master)
|
| 133 |
|
| 134 |
|
| 135 |
class ScrollListBox(Frame):
|
| 136 |
def __init__(self, thelist, master=None ):
|
| 137 |
Frame.__init__(self, master)
|
| 138 |
self.lb = Listbox(self)
|
| 139 |
for item in thelist:
|
| 140 |
self.lb.insert(END, item)
|
| 141 |
self.lb.pack(side = LEFT, expand = 1, fill = BOTH)
|
| 142 |
sb = Scrollbar(self, orient = VERTICAL)
|
| 143 |
sb.pack(side = LEFT, fill = Y)
|
| 144 |
sb['command'] = self.lb.yview
|
| 145 |
self.lb['yscrollcommand'] = sb.set
|
| 146 |
self.lb.focus()
|
| 147 |
|
| 148 |
def _do(self, name, args=()):
|
| 149 |
return apply(self.tk.call, (self._w, name) + args)
|
| 150 |
|
| 151 |
def insert(self, *args):
|
| 152 |
self.lb._do('insert', args)
|
| 153 |
|
| 154 |
|
| 155 |
if __name__ == '__main__':
|
| 156 |
def newfile():
|
| 157 |
print 'New File...'
|
| 158 |
def openfile():
|
| 159 |
print 'Open File...'
|
| 160 |
def spam1():
|
| 161 |
print 'Spam! 1'
|
| 162 |
print 'the variable is: Choice', foovar.get()+1
|
| 163 |
def spam2():
|
| 164 |
print 'Spam! 2'
|
| 165 |
print 'the variable is: Choice', foovar.get()+1
|
| 166 |
def spam3():
|
| 167 |
print 'Spam! 3'
|
| 168 |
print 'the variable is: Choice', foovar.get()+1
|
| 169 |
|
| 170 |
mdef = [{'title':'File',
|
| 171 |
'items':[{'command':newfile, 'label':'New'},
|
| 172 |
{'command':openfile, 'label':'Open'},
|
| 173 |
{'type':'separator'},
|
| 174 |
{'command':sys.exit, 'label':'Quit'},
|
| 175 |
],
|
| 176 |
},
|
| 177 |
{'title':'Edit',
|
| 178 |
'items':[{'label':'Cut'},
|
| 179 |
{'label':'Copy'},
|
| 180 |
{'label':'Paste', 'state':'disabled'}
|
| 181 |
]
|
| 182 |
},
|
| 183 |
]
|
| 184 |
m = MenuBar(mdef)
|
| 185 |
m.pack(side=TOP, fill = BOTH)
|
| 186 |
foovar = IntVar()
|
| 187 |
|
| 188 |
elements = [
|
| 189 |
{'text' : 'Choice 1',
|
| 190 |
'variable' : foovar},
|
| 191 |
{'text' : 'Choice 2',
|
| 192 |
'variable' : foovar},
|
| 193 |
{'text' : 'Choice 3',
|
| 194 |
'variable' : foovar},
|
| 195 |
]
|
| 196 |
l = ['the','world','is','round']
|
| 197 |
l = l * 10
|
| 198 |
b = ScrollListBox(l)
|
| 199 |
elements2 = [
|
| 200 |
{'text' : 'Choice 1',
|
| 201 |
'command' : spam1},
|
| 202 |
{'text' : 'Choice 2',
|
| 203 |
'command' : spam2},
|
| 204 |
{'text' : 'Choice 3',
|
| 205 |
'command' : spam3},
|
| 206 |
]
|
| 207 |
b.pack(side=LEFT, expand=1, fill=BOTH)
|
| 208 |
a = HorizRadioGroup(elements, 0)
|
| 209 |
a.pack(side=TOP, fill=BOTH)
|
| 210 |
c = HorizButtonGroup(elements2)
|
| 211 |
c.pack(side=BOTTOM, fill=X)
|
| 212 |
|
| 213 |
mainloop()
|
| 214 |
|