1 | n/a | from tkinter import * |
---|
2 | n/a | from tkinter.ttk import Scrollbar |
---|
3 | n/a | |
---|
4 | n/a | from idlelib import macosx |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class ScrolledList: |
---|
8 | n/a | |
---|
9 | n/a | default = "(None)" |
---|
10 | n/a | |
---|
11 | n/a | def __init__(self, master, **options): |
---|
12 | n/a | # Create top frame, with scrollbar and listbox |
---|
13 | n/a | self.master = master |
---|
14 | n/a | self.frame = frame = Frame(master) |
---|
15 | n/a | self.frame.pack(fill="both", expand=1) |
---|
16 | n/a | self.vbar = vbar = Scrollbar(frame, name="vbar") |
---|
17 | n/a | self.vbar.pack(side="right", fill="y") |
---|
18 | n/a | self.listbox = listbox = Listbox(frame, exportselection=0, |
---|
19 | n/a | background="white") |
---|
20 | n/a | if options: |
---|
21 | n/a | listbox.configure(options) |
---|
22 | n/a | listbox.pack(expand=1, fill="both") |
---|
23 | n/a | # Tie listbox and scrollbar together |
---|
24 | n/a | vbar["command"] = listbox.yview |
---|
25 | n/a | listbox["yscrollcommand"] = vbar.set |
---|
26 | n/a | # Bind events to the list box |
---|
27 | n/a | listbox.bind("<ButtonRelease-1>", self.click_event) |
---|
28 | n/a | listbox.bind("<Double-ButtonRelease-1>", self.double_click_event) |
---|
29 | n/a | if macosx.isAquaTk(): |
---|
30 | n/a | listbox.bind("<ButtonPress-2>", self.popup_event) |
---|
31 | n/a | listbox.bind("<Control-Button-1>", self.popup_event) |
---|
32 | n/a | else: |
---|
33 | n/a | listbox.bind("<ButtonPress-3>", self.popup_event) |
---|
34 | n/a | listbox.bind("<Key-Up>", self.up_event) |
---|
35 | n/a | listbox.bind("<Key-Down>", self.down_event) |
---|
36 | n/a | # Mark as empty |
---|
37 | n/a | self.clear() |
---|
38 | n/a | |
---|
39 | n/a | def close(self): |
---|
40 | n/a | self.frame.destroy() |
---|
41 | n/a | |
---|
42 | n/a | def clear(self): |
---|
43 | n/a | self.listbox.delete(0, "end") |
---|
44 | n/a | self.empty = 1 |
---|
45 | n/a | self.listbox.insert("end", self.default) |
---|
46 | n/a | |
---|
47 | n/a | def append(self, item): |
---|
48 | n/a | if self.empty: |
---|
49 | n/a | self.listbox.delete(0, "end") |
---|
50 | n/a | self.empty = 0 |
---|
51 | n/a | self.listbox.insert("end", str(item)) |
---|
52 | n/a | |
---|
53 | n/a | def get(self, index): |
---|
54 | n/a | return self.listbox.get(index) |
---|
55 | n/a | |
---|
56 | n/a | def click_event(self, event): |
---|
57 | n/a | self.listbox.activate("@%d,%d" % (event.x, event.y)) |
---|
58 | n/a | index = self.listbox.index("active") |
---|
59 | n/a | self.select(index) |
---|
60 | n/a | self.on_select(index) |
---|
61 | n/a | return "break" |
---|
62 | n/a | |
---|
63 | n/a | def double_click_event(self, event): |
---|
64 | n/a | index = self.listbox.index("active") |
---|
65 | n/a | self.select(index) |
---|
66 | n/a | self.on_double(index) |
---|
67 | n/a | return "break" |
---|
68 | n/a | |
---|
69 | n/a | menu = None |
---|
70 | n/a | |
---|
71 | n/a | def popup_event(self, event): |
---|
72 | n/a | if not self.menu: |
---|
73 | n/a | self.make_menu() |
---|
74 | n/a | menu = self.menu |
---|
75 | n/a | self.listbox.activate("@%d,%d" % (event.x, event.y)) |
---|
76 | n/a | index = self.listbox.index("active") |
---|
77 | n/a | self.select(index) |
---|
78 | n/a | menu.tk_popup(event.x_root, event.y_root) |
---|
79 | n/a | |
---|
80 | n/a | def make_menu(self): |
---|
81 | n/a | menu = Menu(self.listbox, tearoff=0) |
---|
82 | n/a | self.menu = menu |
---|
83 | n/a | self.fill_menu() |
---|
84 | n/a | |
---|
85 | n/a | def up_event(self, event): |
---|
86 | n/a | index = self.listbox.index("active") |
---|
87 | n/a | if self.listbox.selection_includes(index): |
---|
88 | n/a | index = index - 1 |
---|
89 | n/a | else: |
---|
90 | n/a | index = self.listbox.size() - 1 |
---|
91 | n/a | if index < 0: |
---|
92 | n/a | self.listbox.bell() |
---|
93 | n/a | else: |
---|
94 | n/a | self.select(index) |
---|
95 | n/a | self.on_select(index) |
---|
96 | n/a | return "break" |
---|
97 | n/a | |
---|
98 | n/a | def down_event(self, event): |
---|
99 | n/a | index = self.listbox.index("active") |
---|
100 | n/a | if self.listbox.selection_includes(index): |
---|
101 | n/a | index = index + 1 |
---|
102 | n/a | else: |
---|
103 | n/a | index = 0 |
---|
104 | n/a | if index >= self.listbox.size(): |
---|
105 | n/a | self.listbox.bell() |
---|
106 | n/a | else: |
---|
107 | n/a | self.select(index) |
---|
108 | n/a | self.on_select(index) |
---|
109 | n/a | return "break" |
---|
110 | n/a | |
---|
111 | n/a | def select(self, index): |
---|
112 | n/a | self.listbox.focus_set() |
---|
113 | n/a | self.listbox.activate(index) |
---|
114 | n/a | self.listbox.selection_clear(0, "end") |
---|
115 | n/a | self.listbox.selection_set(index) |
---|
116 | n/a | self.listbox.see(index) |
---|
117 | n/a | |
---|
118 | n/a | # Methods to override for specific actions |
---|
119 | n/a | |
---|
120 | n/a | def fill_menu(self): |
---|
121 | n/a | pass |
---|
122 | n/a | |
---|
123 | n/a | def on_select(self, index): |
---|
124 | n/a | pass |
---|
125 | n/a | |
---|
126 | n/a | def on_double(self, index): |
---|
127 | n/a | pass |
---|
128 | n/a | |
---|
129 | n/a | |
---|
130 | n/a | def _scrolled_list(parent): # htest # |
---|
131 | n/a | top = Toplevel(parent) |
---|
132 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
---|
133 | n/a | top.geometry("+%d+%d" % (x+200, y + 175)) |
---|
134 | n/a | class MyScrolledList(ScrolledList): |
---|
135 | n/a | def fill_menu(self): self.menu.add_command(label="right click") |
---|
136 | n/a | def on_select(self, index): print("select", self.get(index)) |
---|
137 | n/a | def on_double(self, index): print("double", self.get(index)) |
---|
138 | n/a | |
---|
139 | n/a | scrolled_list = MyScrolledList(top) |
---|
140 | n/a | for i in range(30): |
---|
141 | n/a | scrolled_list.append("Item %02d" % i) |
---|
142 | n/a | |
---|
143 | n/a | if __name__ == '__main__': |
---|
144 | n/a | # At the moment, test_scrolledlist merely creates instance, like htest. |
---|
145 | n/a | from idlelib.idle_test.htest import run |
---|
146 | n/a | run(_scrolled_list) |
---|