def __init__(self
, master
, **options
):
# Create top frame, with scrollbar and listbox
self
.frame
= frame
= Frame(master
)
self
.frame
.pack(fill
="both", expand
=1)
self
.vbar
= vbar
= Scrollbar(frame
, name
="vbar")
self
.vbar
.pack(side
="right", fill
="y")
self
.listbox
= listbox
= Listbox(frame
, exportselection
=0,
listbox
.configure(options
)
listbox
.pack(expand
=1, fill
="both")
# Tie listbox and scrollbar together
vbar
["command"] = listbox
.yview
listbox
["yscrollcommand"] = vbar
.set
# Bind events to the list box
listbox
.bind("<ButtonRelease-1>", self
.click_event
)
listbox
.bind("<Double-ButtonRelease-1>", self
.double_click_event
)
listbox
.bind("<ButtonPress-3>", self
.popup_event
)
listbox
.bind("<Key-Up>", self
.up_event
)
listbox
.bind("<Key-Down>", self
.down_event
)
self
.listbox
.delete(0, "end")
self
.listbox
.insert("end", self
.default
)
self
.listbox
.delete(0, "end")
self
.listbox
.insert("end", str(item
))
return self
.listbox
.get(index
)
def click_event(self
, event
):
self
.listbox
.activate("@%d,%d" % (event
.x
, event
.y
))
index
= self
.listbox
.index("active")
def double_click_event(self
, event
):
index
= self
.listbox
.index("active")
def popup_event(self
, event
):
self
.listbox
.activate("@%d,%d" % (event
.x
, event
.y
))
index
= self
.listbox
.index("active")
menu
.tk_popup(event
.x_root
, event
.y_root
)
menu
= Menu(self
.listbox
, tearoff
=0)
def up_event(self
, event
):
index
= self
.listbox
.index("active")
if self
.listbox
.selection_includes(index
):
index
= self
.listbox
.size() - 1
def down_event(self
, event
):
index
= self
.listbox
.index("active")
if self
.listbox
.selection_includes(index
):
if index
>= self
.listbox
.size():
self
.listbox
.activate(index
)
self
.listbox
.selection_clear(0, "end")
self
.listbox
.selection_set(index
)
# Methods to override for specific actions
def on_select(self
, index
):
def on_double(self
, index
):
root
.protocol("WM_DELETE_WINDOW", root
.destroy
)
class MyScrolledList(ScrolledList
):
def fill_menu(self
): self
.menu
.add_command(label
="pass")
def on_select(self
, index
): print "select", self
.get(index
)
def on_double(self
, index
): print "double", self
.get(index
)
s
.append("item %02d" % i
)
if __name__
== '__main__':