Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 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. |
| 5 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 6 | from Tkinter import * |
| 7 | from WindowList import ListedToplevel |
| 8 | from Separator import HSeparator |
| 9 | from ScrolledList import ScrolledList |
| 10 | |
| 11 | class MultiScrolledLists: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 13 | def __init__(self, root, nlists=2): |
| 14 | assert nlists >= 1 |
| 15 | self.root = root |
| 16 | self.nlists = nlists |
| 17 | self.path = [] |
| 18 | # create top |
| 19 | self.top = top = ListedToplevel(root) |
| 20 | top.protocol("WM_DELETE_WINDOW", self.close) |
| 21 | top.bind("<Escape>", self.close) |
| 22 | self.settitle() |
| 23 | # create frames and separators in between |
| 24 | self.frames = [] |
| 25 | self.separators = [] |
| 26 | last = top |
| 27 | for i in range(nlists-1): |
| 28 | sepa = HSeparator(last) |
| 29 | self.separators.append(sepa) |
| 30 | frame, last = sepa.parts() |
| 31 | self.frames.append(frame) |
| 32 | self.frames.append(last) |
| 33 | # create labels and lists |
| 34 | self.labels = [] |
| 35 | self.lists = [] |
| 36 | for i in range(nlists): |
| 37 | frame = self.frames[i] |
| 38 | label = Label(frame, text=self.subtitle(i), |
| 39 | relief="groove", borderwidth=2) |
| 40 | label.pack(fill="x") |
| 41 | self.labels.append(label) |
| 42 | list = ScrolledList(frame, width=self.width(i), |
| 43 | height=self.height(i)) |
| 44 | self.lists.append(list) |
| 45 | list.on_select = \ |
| 46 | lambda index, i=i, self=self: self.on_select(index, i) |
| 47 | list.on_double = \ |
| 48 | lambda index, i=i, self=self: self.on_double(index, i) |
| 49 | # fill leftmost list (rest get filled on demand) |
| 50 | self.fill(0) |
| 51 | # XXX one after_idle isn't enough; two are... |
| 52 | top.after_idle(self.call_pack_propagate_1) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 54 | def call_pack_propagate_1(self): |
| 55 | self.top.after_idle(self.call_pack_propagate) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 57 | def call_pack_propagate(self): |
| 58 | for frame in self.frames: |
| 59 | frame.pack_propagate(0) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 61 | def close(self, event=None): |
| 62 | self.top.destroy() |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 64 | def settitle(self): |
| 65 | short = self.shorttitle() |
| 66 | long = self.longtitle() |
| 67 | if short and long: |
| 68 | title = short + " - " + long |
| 69 | elif short: |
| 70 | title = short |
| 71 | elif long: |
| 72 | title = long |
| 73 | else: |
| 74 | title = "Untitled" |
| 75 | icon = short or long or title |
| 76 | self.top.wm_title(title) |
| 77 | self.top.wm_iconname(icon) |
| 78 | |
| 79 | def longtitle(self): |
| 80 | # override this |
| 81 | return "Multi Scrolled Lists" |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 83 | def shorttitle(self): |
| 84 | # override this |
| 85 | return None |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 87 | def width(self, i): |
| 88 | # override this |
| 89 | return 20 |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 91 | def height(self, i): |
| 92 | # override this |
| 93 | return 10 |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 95 | def subtitle(self, i): |
| 96 | # override this |
| 97 | return "Column %d" % i |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 99 | def fill(self, i): |
| 100 | for k in range(i, self.nlists): |
| 101 | self.lists[k].clear() |
| 102 | self.labels[k].configure(text=self.subtitle(k)) |
| 103 | list = self.lists[i] |
| 104 | l = self.items(i) |
| 105 | for s in l: |
| 106 | list.append(s) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 108 | def on_select(self, index, i): |
| 109 | item = self.lists[i].get(index) |
| 110 | del self.path[i:] |
| 111 | self.path.append(item) |
| 112 | if i+1 < self.nlists: |
| 113 | self.fill(i+1) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 115 | def items(self, i): |
| 116 | # override this |
| 117 | l = [] |
| 118 | for k in range(10): |
| 119 | s = str(k) |
| 120 | if i > 0: |
| 121 | s = self.path[i-1] + "." + s |
| 122 | l.append(s) |
| 123 | return l |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | dc424be | 1999-03-10 05:13:29 +0000 | [diff] [blame] | 125 | def on_double(self, index, i): |
| 126 | pass |
| 127 | |
| 128 | |
| 129 | def main(): |
| 130 | root = Tk() |
| 131 | quit = Button(root, text="Exit", command=root.destroy) |
| 132 | quit.pack() |
| 133 | MultiScrolledLists(root, 4) |
| 134 | root.mainloop() |
| 135 | |
| 136 | if __name__ == "__main__": |
| 137 | main() |