Ditched '_find_SET()', since it was a no-value-added wrapper around
[python/dscho.git] / Tools / idle / MultiScrolledLists.py
blob32f6246172949936c8073ea09341a2c22d5baa39
1 # One or more ScrolledLists with HSeparators between them.
2 # There is a hierarchical relationship between them:
3 # the right list displays the substructure of the selected item
4 # in the left list.
6 import string
7 from Tkinter import *
8 from WindowList import ListedToplevel
9 from Separator import HSeparator
10 from ScrolledList import ScrolledList
12 class MultiScrolledLists:
14 def __init__(self, root, nlists=2):
15 assert nlists >= 1
16 self.root = root
17 self.nlists = nlists
18 self.path = []
19 # create top
20 self.top = top = ListedToplevel(root)
21 top.protocol("WM_DELETE_WINDOW", self.close)
22 top.bind("<Escape>", self.close)
23 self.settitle()
24 # create frames and separators in between
25 self.frames = []
26 self.separators = []
27 last = top
28 for i in range(nlists-1):
29 sepa = HSeparator(last)
30 self.separators.append(sepa)
31 frame, last = sepa.parts()
32 self.frames.append(frame)
33 self.frames.append(last)
34 # create labels and lists
35 self.labels = []
36 self.lists = []
37 for i in range(nlists):
38 frame = self.frames[i]
39 label = Label(frame, text=self.subtitle(i),
40 relief="groove", borderwidth=2)
41 label.pack(fill="x")
42 self.labels.append(label)
43 list = ScrolledList(frame, width=self.width(i),
44 height=self.height(i))
45 self.lists.append(list)
46 list.on_select = \
47 lambda index, i=i, self=self: self.on_select(index, i)
48 list.on_double = \
49 lambda index, i=i, self=self: self.on_double(index, i)
50 # fill leftmost list (rest get filled on demand)
51 self.fill(0)
52 # XXX one after_idle isn't enough; two are...
53 top.after_idle(self.call_pack_propagate_1)
55 def call_pack_propagate_1(self):
56 self.top.after_idle(self.call_pack_propagate)
58 def call_pack_propagate(self):
59 for frame in self.frames:
60 frame.pack_propagate(0)
62 def close(self, event=None):
63 self.top.destroy()
65 def settitle(self):
66 short = self.shorttitle()
67 long = self.longtitle()
68 if short and long:
69 title = short + " - " + long
70 elif short:
71 title = short
72 elif long:
73 title = long
74 else:
75 title = "Untitled"
76 icon = short or long or title
77 self.top.wm_title(title)
78 self.top.wm_iconname(icon)
80 def longtitle(self):
81 # override this
82 return "Multi Scrolled Lists"
84 def shorttitle(self):
85 # override this
86 return None
88 def width(self, i):
89 # override this
90 return 20
92 def height(self, i):
93 # override this
94 return 10
96 def subtitle(self, i):
97 # override this
98 return "Column %d" % i
100 def fill(self, i):
101 for k in range(i, self.nlists):
102 self.lists[k].clear()
103 self.labels[k].configure(text=self.subtitle(k))
104 list = self.lists[i]
105 l = self.items(i)
106 for s in l:
107 list.append(s)
109 def on_select(self, index, i):
110 item = self.lists[i].get(index)
111 del self.path[i:]
112 self.path.append(item)
113 if i+1 < self.nlists:
114 self.fill(i+1)
116 def items(self, i):
117 # override this
118 l = []
119 for k in range(10):
120 s = str(k)
121 if i > 0:
122 s = self.path[i-1] + "." + s
123 l.append(s)
124 return l
126 def on_double(self, index, i):
127 pass
130 def main():
131 root = Tk()
132 quit = Button(root, text="Exit", command=root.destroy)
133 quit.pack()
134 MultiScrolledLists(root, 4)
135 root.mainloop()
137 if __name__ == "__main__":
138 main()