7 def __init__(self
, master
, **options
):
8 # Create top frame, with scrollbar and listbox
10 self
.frame
= frame
= Frame(master
)
11 self
.frame
.pack(fill
="both", expand
=1)
12 self
.vbar
= vbar
= Scrollbar(frame
, name
="vbar")
13 self
.vbar
.pack(side
="right", fill
="y")
14 self
.listbox
= listbox
= Listbox(frame
, exportselection
=0,
17 listbox
.configure(options
)
18 listbox
.pack(expand
=1, fill
="both")
19 # Tie listbox and scrollbar together
20 vbar
["command"] = listbox
.yview
21 listbox
["yscrollcommand"] = vbar
.set
22 # Bind events to the list box
23 listbox
.bind("<ButtonRelease-1>", self
.click_event
)
24 listbox
.bind("<Double-ButtonRelease-1>", self
.double_click_event
)
25 listbox
.bind("<ButtonPress-3>", self
.popup_event
)
26 listbox
.bind("<Key-Up>", self
.up_event
)
27 listbox
.bind("<Key-Down>", self
.down_event
)
35 self
.listbox
.delete(0, "end")
37 self
.listbox
.insert("end", self
.default
)
39 def append(self
, item
):
41 self
.listbox
.delete(0, "end")
43 self
.listbox
.insert("end", str(item
))
46 return self
.listbox
.get(index
)
48 def click_event(self
, event
):
49 self
.listbox
.activate("@%d,%d" % (event
.x
, event
.y
))
50 index
= self
.listbox
.index("active")
55 def double_click_event(self
, event
):
56 index
= self
.listbox
.index("active")
63 def popup_event(self
, event
):
67 self
.listbox
.activate("@%d,%d" % (event
.x
, event
.y
))
68 index
= self
.listbox
.index("active")
70 menu
.tk_popup(event
.x_root
, event
.y_root
)
73 menu
= Menu(self
.listbox
, tearoff
=0)
77 def up_event(self
, event
):
78 index
= self
.listbox
.index("active")
79 if self
.listbox
.selection_includes(index
):
82 index
= self
.listbox
.size() - 1
90 def down_event(self
, event
):
91 index
= self
.listbox
.index("active")
92 if self
.listbox
.selection_includes(index
):
96 if index
>= self
.listbox
.size():
100 self
.on_select(index
)
103 def select(self
, index
):
104 self
.listbox
.focus_set()
105 self
.listbox
.activate(index
)
106 self
.listbox
.selection_clear(0, "end")
107 self
.listbox
.selection_set(index
)
108 self
.listbox
.see(index
)
110 # Methods to override for specific actions
115 def on_select(self
, index
):
118 def on_double(self
, index
):
124 root
.protocol("WM_DELETE_WINDOW", root
.destroy
)
125 class MyScrolledList(ScrolledList
):
126 def fill_menu(self
): self
.menu
.add_command(label
="pass")
127 def on_select(self
, index
): print "select", self
.get(index
)
128 def on_double(self
, index
): print "double", self
.get(index
)
129 s
= MyScrolledList(root
)
131 s
.append("item %02d" % i
)
138 if __name__
== '__main__':