0001 from Tkinter import * 0002 0003 class ScrolledList: 0004 0005 default = "(None)" 0006 0007 def __init__(self, master, **options): 0008 # Create top frame, with scrollbar and listbox 0009 self.master = master 0010 self.frame = frame = Frame(master) 0011 self.frame.pack(fill="both", expand=1) 0012 self.vbar = vbar = Scrollbar(frame, name="vbar") 0013 self.vbar.pack(side="right", fill="y") 0014 self.listbox = listbox = Listbox(frame, exportselection=0, 0015 background="white") 0016 if options: 0017 listbox.configure(options) 0018 listbox.pack(expand=1, fill="both") 0019 # Tie listbox and scrollbar together 0020 vbar["command"] = listbox.yview 0021 listbox["yscrollcommand"] = vbar.set 0022 # Bind events to the list box 0023 listbox.bind("<ButtonRelease-1>", self.click_event) 0024 listbox.bind("<Double-ButtonRelease-1>", self.double_click_event) 0025 listbox.bind("<ButtonPress-3>", self.popup_event) 0026 listbox.bind("<Key-Up>", self.up_event) 0027 listbox.bind("<Key-Down>", self.down_event) 0028 # Mark as empty 0029 self.clear() 0030 0031 def close(self): 0032 self.frame.destroy() 0033 0034 def clear(self): 0035 self.listbox.delete(0, "end") 0036 self.empty = 1 0037 self.listbox.insert("end", self.default) 0038 0039 def append(self, item): 0040 if self.empty: 0041 self.listbox.delete(0, "end") 0042 self.empty = 0 0043 self.listbox.insert("end", str(item)) 0044 0045 def get(self, index): 0046 return self.listbox.get(index) 0047 0048 def click_event(self, event): 0049 self.listbox.activate("@%d,%d" % (event.x, event.y)) 0050 index = self.listbox.index("active") 0051 self.select(index) 0052 self.on_select(index) 0053 return "break" 0054 0055 def double_click_event(self, event): 0056 index = self.listbox.index("active") 0057 self.select(index) 0058 self.on_double(index) 0059 return "break" 0060 0061 menu = None 0062 0063 def popup_event(self, event): 0064 if not self.menu: 0065 self.make_menu() 0066 menu = self.menu 0067 self.listbox.activate("@%d,%d" % (event.x, event.y)) 0068 index = self.listbox.index("active") 0069 self.select(index) 0070 menu.tk_popup(event.x_root, event.y_root) 0071 0072 def make_menu(self): 0073 menu = Menu(self.listbox, tearoff=0) 0074 self.menu = menu 0075 self.fill_menu() 0076 0077 def up_event(self, event): 0078 index = self.listbox.index("active") 0079 if self.listbox.selection_includes(index): 0080 index = index - 1 0081 else: 0082 index = self.listbox.size() - 1 0083 if index < 0: 0084 self.listbox.bell() 0085 else: 0086 self.select(index) 0087 self.on_select(index) 0088 return "break" 0089 0090 def down_event(self, event): 0091 index = self.listbox.index("active") 0092 if self.listbox.selection_includes(index): 0093 index = index + 1 0094 else: 0095 index = 0 0096 if index >= self.listbox.size(): 0097 self.listbox.bell() 0098 else: 0099 self.select(index) 0100 self.on_select(index) 0101 return "break" 0102 0103 def select(self, index): 0104 self.listbox.focus_set() 0105 self.listbox.activate(index) 0106 self.listbox.selection_clear(0, "end") 0107 self.listbox.selection_set(index) 0108 self.listbox.see(index) 0109 0110 # Methods to override for specific actions 0111 0112 def fill_menu(self): 0113 pass 0114 0115 def on_select(self, index): 0116 pass 0117 0118 def on_double(self, index): 0119 pass 0120 0121 0122 def test(): 0123 root = Tk() 0124 root.protocol("WM_DELETE_WINDOW", root.destroy) 0125 class MyScrolledList(ScrolledList): 0126 def fill_menu(self): self.menu.add_command(label="pass") 0127 def on_select(self, index): print "select", self.get(index) 0128 def on_double(self, index): print "double", self.get(index) 0129 s = MyScrolledList(root) 0130 for i in range(30): 0131 s.append("item %02d" % i) 0132 return root 0133 0134 def main(): 0135 root = test() 0136 root.mainloop() 0137 0138 if __name__ == '__main__': 0139 main() 0140
Generated by PyXR 0.9.4