| 1 |
ninoborges |
8 |
"""
|
| 2 |
|
|
Module : TkList
|
| 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/TkList.py
|
| 7 |
|
|
|
| 8 |
|
|
Description : This module provides a Tkinter widget which is a wrapper
|
| 9 |
|
|
around both the Listbox widget and the generic python
|
| 10 |
|
|
list data structure. All list operations are supported,
|
| 11 |
|
|
including slicing, sorting, reverse, addition, etc.
|
| 12 |
|
|
|
| 13 |
|
|
Requirements : Tkinter, UserLib
|
| 14 |
|
|
|
| 15 |
|
|
Installation : Put this file in your PYTHONPATH
|
| 16 |
|
|
|
| 17 |
|
|
Usage : widget = TkList([initiallist = None, [master = None]])
|
| 18 |
|
|
|
| 19 |
|
|
Note 1: The widget displays the str() string representation of the
|
| 20 |
|
|
objects it contains in its list. Thus, you can do things like
|
| 21 |
|
|
a.append(Button) even if Button is a class. The widget show
|
| 22 |
|
|
something like <class Button at 100c33..> in its listbox.
|
| 23 |
|
|
This is more useful with user-defined classes.
|
| 24 |
|
|
|
| 25 |
|
|
Note 2: The easiest way to see the features of this widget is to run
|
| 26 |
|
|
this file.
|
| 27 |
|
|
|
| 28 |
|
|
Note 3: While TkList was inspired by UserList, I added a few functions
|
| 29 |
|
|
not found in UserList but found in python lists, such as
|
| 30 |
|
|
list addition and list multiplication
|
| 31 |
|
|
|
| 32 |
|
|
Missing Features: I'm sure.
|
| 33 |
|
|
|
| 34 |
|
|
Bug reports: please send me email at david_ascher@brown.edu
|
| 35 |
|
|
|
| 36 |
|
|
"""
|
| 37 |
|
|
from types import IntType
|
| 38 |
|
|
from Tkinter import Frame, Scrollbar, Listbox
|
| 39 |
|
|
from UserList import UserList
|
| 40 |
|
|
from Tkconstants import *
|
| 41 |
|
|
|
| 42 |
|
|
class TkList(Frame, UserList):
|
| 43 |
|
|
def __init__(self, thelist = None, master=None ):
|
| 44 |
|
|
UserList.__init__(self, thelist)
|
| 45 |
|
|
self.frame = Frame.__init__(self, master)
|
| 46 |
|
|
self.lb = Listbox(self)
|
| 47 |
|
|
for item in self.data:
|
| 48 |
|
|
self.lb.insert(END, str(item))
|
| 49 |
|
|
self.lb.pack(side = LEFT, expand = 1, fill = BOTH)
|
| 50 |
|
|
sb = Scrollbar(self, orient = VERTICAL)
|
| 51 |
|
|
sb.pack(side = RIGHT, fill = Y)
|
| 52 |
|
|
sb['command'] = self.lb.yview
|
| 53 |
|
|
self.lb['yscrollcommand'] = sb.set
|
| 54 |
|
|
|
| 55 |
|
|
def __add__(self, item):
|
| 56 |
|
|
""" If one adds two TkLists, the contents of the second get
|
| 57 |
|
|
added to the first"""
|
| 58 |
|
|
if type(item) == type(self):
|
| 59 |
|
|
self.__add__(item.data)
|
| 60 |
|
|
elif type(item) == type(self.data):
|
| 61 |
|
|
for i in item:
|
| 62 |
|
|
self.__add__(i)
|
| 63 |
|
|
else:
|
| 64 |
|
|
self.data = self.data + [item]
|
| 65 |
|
|
self.lb.insert(END, str(item))
|
| 66 |
|
|
return self
|
| 67 |
|
|
|
| 68 |
|
|
def __radd__(self, item):
|
| 69 |
|
|
if type(item) == type(self):
|
| 70 |
|
|
self.__add__(item.data)
|
| 71 |
|
|
elif type(item) == type(self.data):
|
| 72 |
|
|
for i in item:
|
| 73 |
|
|
self.__add__(i)
|
| 74 |
|
|
else:
|
| 75 |
|
|
self.data = [item] + self.data
|
| 76 |
|
|
self.lb.insert(0, str(item))
|
| 77 |
|
|
return self
|
| 78 |
|
|
|
| 79 |
|
|
def __mul__(self, item):
|
| 80 |
|
|
if type(item) == IntType:
|
| 81 |
|
|
self.data = self.data * item
|
| 82 |
|
|
self.lb.delete(0,END)
|
| 83 |
|
|
for item in self.data:
|
| 84 |
|
|
self.lb.insert(END, str(item))
|
| 85 |
|
|
return self
|
| 86 |
|
|
else:
|
| 87 |
|
|
raise TypeError, "__mul__ nor __rmul__ defined for these operands"
|
| 88 |
|
|
|
| 89 |
|
|
__rmul__ = __mul__
|
| 90 |
|
|
|
| 91 |
|
|
def __setitem__(self, i, item):
|
| 92 |
|
|
self.data[i] = item
|
| 93 |
|
|
self.lb.delete(i) # this was a bug
|
| 94 |
|
|
self.lb.insert(i, str(item))
|
| 95 |
|
|
|
| 96 |
|
|
def __getitem__(self, i):
|
| 97 |
|
|
return self.data[i]
|
| 98 |
|
|
|
| 99 |
|
|
def __delitem__(self, i):
|
| 100 |
|
|
del self.data[i]
|
| 101 |
|
|
self.lb.delete(i)
|
| 102 |
|
|
|
| 103 |
|
|
def __getslice__(self, i, j):
|
| 104 |
|
|
self.data[:] = self.data[i:j]
|
| 105 |
|
|
for index in range(len(self.data)):
|
| 106 |
|
|
self.lb.insert(index, str(self.data[index]))
|
| 107 |
|
|
l = len(self.data)
|
| 108 |
|
|
self.lb.delete(l,END)
|
| 109 |
|
|
return self
|
| 110 |
|
|
|
| 111 |
|
|
def __setslice__(self, i, j, list):
|
| 112 |
|
|
if type(list) == type(self.data): # e.g. a[1:3] = [1,2,3]
|
| 113 |
|
|
self.data[i:j] = list
|
| 114 |
|
|
else:
|
| 115 |
|
|
try:
|
| 116 |
|
|
self.data[i:j] = list.data
|
| 117 |
|
|
except:
|
| 118 |
|
|
self.data[i:j] = [list]
|
| 119 |
|
|
self.lb.delete(0,END)
|
| 120 |
|
|
l = len(self.data)
|
| 121 |
|
|
for index in range(l):
|
| 122 |
|
|
self.lb.insert(END, str(self.data[index]))
|
| 123 |
|
|
|
| 124 |
|
|
def __delslice__(self, i, j):
|
| 125 |
|
|
del self.data[i:j]
|
| 126 |
|
|
for count in range(i,j):
|
| 127 |
|
|
self.lb.delete(i)
|
| 128 |
|
|
|
| 129 |
|
|
def append(self, item):
|
| 130 |
|
|
self.data.append(item)
|
| 131 |
|
|
self.lb.insert(END, str(item))
|
| 132 |
|
|
|
| 133 |
|
|
def insert(self, i, item):
|
| 134 |
|
|
self.data.insert(i, item)
|
| 135 |
|
|
self.lb.insert(i, str(item))
|
| 136 |
|
|
|
| 137 |
|
|
def remove(self, item):
|
| 138 |
|
|
index = self.data.index(item)
|
| 139 |
|
|
del self.data[index]
|
| 140 |
|
|
self.lb.delete(index)
|
| 141 |
|
|
|
| 142 |
|
|
def reverse(self):
|
| 143 |
|
|
self.data.reverse()
|
| 144 |
|
|
self.lb.delete(0,END)
|
| 145 |
|
|
for item in self.data:
|
| 146 |
|
|
self.lb.insert(END, str(item))
|
| 147 |
|
|
|
| 148 |
|
|
def sort(self, *args):
|
| 149 |
|
|
apply(self.data.sort, args)
|
| 150 |
|
|
self.lb.delete(0,END)
|
| 151 |
|
|
for item in self.data:
|
| 152 |
|
|
self.lb.insert(END, str(item))
|
| 153 |
|
|
|
| 154 |
|
|
|
| 155 |
|
|
if __name__ == '__main__':
|
| 156 |
|
|
from types import *
|
| 157 |
|
|
import sys #, rand
|
| 158 |
|
|
|
| 159 |
|
|
from Tkinter import *
|
| 160 |
|
|
from string import *
|
| 161 |
|
|
from TkUtil import *
|
| 162 |
|
|
|
| 163 |
|
|
|
| 164 |
|
|
class Demo(Frame):
|
| 165 |
|
|
def addbutton(self):
|
| 166 |
|
|
self.thelist.append(Button)
|
| 167 |
|
|
def double(self):
|
| 168 |
|
|
self.thelist = self.thelist * 2
|
| 169 |
|
|
|
| 170 |
|
|
def push(self):
|
| 171 |
|
|
name = 'tomato' , `rand.rand()`
|
| 172 |
|
|
self.thelist[0:0] = name
|
| 173 |
|
|
|
| 174 |
|
|
def pop(self):
|
| 175 |
|
|
if self.thelist:
|
| 176 |
|
|
item = self.thelist[0]
|
| 177 |
|
|
self.thelist = self.thelist[1:]
|
| 178 |
|
|
return item
|
| 179 |
|
|
else:
|
| 180 |
|
|
return None
|
| 181 |
|
|
|
| 182 |
|
|
def DefineWidgets(self, master = None):
|
| 183 |
|
|
self.thelist = TkList(['foo', 1.23, None, Button, Radiobutton])
|
| 184 |
|
|
self.thelist.pack(side=LEFT, expand=1, fill=BOTH)
|
| 185 |
|
|
buttons = [{ 'text': 'Pop', 'command' : self.pop},
|
| 186 |
|
|
{ 'text': 'Push', 'command' : self.push},
|
| 187 |
|
|
{ 'text': 'Sort', 'command' : self.thelist.sort},
|
| 188 |
|
|
{ 'text': 'Reverse', 'command' : self.thelist.reverse},
|
| 189 |
|
|
{ 'text': 'Append Class', 'command' : self.addbutton},
|
| 190 |
|
|
{ 'text': 'Double', 'command' : self.double},
|
| 191 |
|
|
{ 'text': 'Quit', 'command' : sys.exit},
|
| 192 |
|
|
]
|
| 193 |
|
|
self.buttons = VertButtonGroup(buttons)
|
| 194 |
|
|
self.buttons.pack(side=RIGHT, expand = 1, fill=BOTH)
|
| 195 |
|
|
|
| 196 |
|
|
def __init__(self, master = None):
|
| 197 |
|
|
Frame.__init__(self,master)
|
| 198 |
|
|
self.DefineWidgets(master)
|
| 199 |
|
|
self.pack(expand=1, fill=BOTH)
|
| 200 |
|
|
|
| 201 |
|
|
t = Demo()
|
| 202 |
|
|
t.mainloop()
|