| 1 | title = 'Pmw.ScrolledListBox demonstration' |
| 2 | |
| 3 | # Import Pmw from this directory tree. |
| 4 | import sys |
| 5 | sys.path[:0] = ['../../..'] |
| 6 | |
| 7 | import Tkinter |
| 8 | import Pmw |
| 9 | |
| 10 | class Demo: |
| 11 | def __init__(self, parent): |
| 12 | # Create the ScrolledListBox. |
| 13 | self.box = Pmw.ScrolledListBox(parent, |
| 14 | items=('Sydney', 'Melbourne', 'Brisbane'), |
| 15 | labelpos='nw', |
| 16 | label_text='Cities', |
| 17 | listbox_height = 6, |
| 18 | selectioncommand=self.selectionCommand, |
| 19 | dblclickcommand=self.defCmd, |
| 20 | usehullsize = 1, |
| 21 | hull_width = 200, |
| 22 | hull_height = 200, |
| 23 | ) |
| 24 | |
| 25 | # Create a group widget to contain the scrollmode options. |
| 26 | w = Pmw.Group(parent, tag_text='Scroll mode') |
| 27 | w.pack(side = 'bottom', padx = 5, pady = 5) |
| 28 | |
| 29 | hmode = Pmw.OptionMenu(w.interior(), |
| 30 | labelpos = 'w', |
| 31 | label_text = 'Horizontal:', |
| 32 | items = ['none', 'static', 'dynamic'], |
| 33 | command = self.sethscrollmode, |
| 34 | menubutton_width = 8, |
| 35 | ) |
| 36 | hmode.pack(side = 'top', padx = 5, pady = 5) |
| 37 | hmode.invoke('dynamic') |
| 38 | |
| 39 | vmode = Pmw.OptionMenu(w.interior(), |
| 40 | labelpos = 'w', |
| 41 | label_text = 'Vertical:', |
| 42 | items = ['none', 'static', 'dynamic'], |
| 43 | command = self.setvscrollmode, |
| 44 | menubutton_width = 8, |
| 45 | ) |
| 46 | vmode.pack(side = 'top', padx = 5, pady = 5) |
| 47 | vmode.invoke('dynamic') |
| 48 | |
| 49 | buttonBox = Pmw.ButtonBox(parent) |
| 50 | buttonBox.pack(side = 'bottom') |
| 51 | buttonBox.add('yview', text = 'Show\nyview', command = self.showYView) |
| 52 | buttonBox.add('scroll', text = 'Page\ndown', command = self.pageDown) |
| 53 | buttonBox.add('center', text = 'Center', command = self.centerPage) |
| 54 | |
| 55 | # Pack this last so that the buttons do not get shrunk when |
| 56 | # the window is resized. |
| 57 | self.box.pack(fill = 'both', expand = 1, padx = 5, pady = 5) |
| 58 | |
| 59 | # Do this after packing the scrolled list box, so that the |
| 60 | # window does not resize as soon as it appears (because |
| 61 | # alignlabels has to do an update_idletasks). |
| 62 | Pmw.alignlabels((hmode, vmode)) |
| 63 | |
| 64 | # Add some more entries to the listbox. |
| 65 | items = ('Andamooka', 'Coober Pedy', 'Innamincka', 'Oodnadatta') |
| 66 | self.box.setlist(items) |
| 67 | self.box.insert(2, 'Wagga Wagga', 'Perth', 'London') |
| 68 | self.box.insert('end', 'Darwin', 'Auckland', 'New York') |
| 69 | index = list(self.box.get(0, 'end')).index('London') |
| 70 | self.box.delete(index) |
| 71 | self.box.delete(7, 8) |
| 72 | self.box.insert('end', 'Bulli', 'Alice Springs', 'Woy Woy') |
| 73 | self.box.insert('end', 'Wallumburrawang', 'Willandra Billabong') |
| 74 | |
| 75 | def sethscrollmode(self, tag): |
| 76 | self.box.configure(hscrollmode = tag) |
| 77 | |
| 78 | def setvscrollmode(self, tag): |
| 79 | self.box.configure(vscrollmode = tag) |
| 80 | |
| 81 | def selectionCommand(self): |
| 82 | sels = self.box.getcurselection() |
| 83 | if len(sels) == 0: |
| 84 | print 'No selection' |
| 85 | else: |
| 86 | print 'Selection:', sels[0] |
| 87 | |
| 88 | def defCmd(self): |
| 89 | sels = self.box.getcurselection() |
| 90 | if len(sels) == 0: |
| 91 | print 'No selection for double click' |
| 92 | else: |
| 93 | print 'Double click:', sels[0] |
| 94 | |
| 95 | def showYView(self): |
| 96 | print self.box.yview() |
| 97 | |
| 98 | def pageDown(self): |
| 99 | self.box.yview('scroll', 1, 'page') |
| 100 | |
| 101 | def centerPage(self): |
| 102 | top, bottom = self.box.yview() |
| 103 | size = bottom - top |
| 104 | middle = 0.5 - size / 2 |
| 105 | self.box.yview('moveto', middle) |
| 106 | |
| 107 | ###################################################################### |
| 108 | |
| 109 | # Create demo in root window for testing. |
| 110 | if __name__ == '__main__': |
| 111 | root = Tkinter.Tk() |
| 112 | Pmw.initialise(root) |
| 113 | root.title(title) |
| 114 | |
| 115 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 116 | exitButton.pack(side = 'bottom') |
| 117 | widget = Demo(root) |
| 118 | root.mainloop() |