ยปCore Development>Code coverage>Lib/idlelib/scrolledlist.py

Python code coverage for Lib/idlelib/scrolledlist.py

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