| 1 | title = 'Demonstration of how to create a megawidget' |
| 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 TextDisplay(Pmw.MegaWidget): |
| 11 | |
| 12 | # Demo Pmw megawidget. |
| 13 | |
| 14 | def __init__(self, parent = None, **kw): |
| 15 | |
| 16 | # Define the megawidget options. |
| 17 | optiondefs = () |
| 18 | self.defineoptions(kw, optiondefs) |
| 19 | |
| 20 | # Initialise the base class (after defining the options). |
| 21 | Pmw.MegaWidget.__init__(self, parent) |
| 22 | |
| 23 | # Create the components. |
| 24 | interior = self.interior() |
| 25 | |
| 26 | self._text = self.createcomponent('text', |
| 27 | (), None, |
| 28 | Tkinter.Text, (interior,), state = 'disabled') |
| 29 | self._text.pack(side='left', fill='both', expand='yes') |
| 30 | |
| 31 | self._scrollbar = self.createcomponent('scrollbar', |
| 32 | (), None, |
| 33 | Tkinter.Scrollbar, (interior,), command = self._text.yview) |
| 34 | self._scrollbar.pack(side='right', fill='y') |
| 35 | self._text.configure(yscrollcommand = self._scrollbar.set) |
| 36 | |
| 37 | # Check keywords and initialise options. |
| 38 | self.initialiseoptions() |
| 39 | |
| 40 | def display(self, info): |
| 41 | self._text.configure(state = 'normal') |
| 42 | self._text.delete('1.0', 'end') |
| 43 | self._text.insert('1.0', info) |
| 44 | self._text.configure(state = 'disabled') |
| 45 | |
| 46 | def append(self, info): |
| 47 | self._text.configure(state = 'normal') |
| 48 | self._text.insert('end', info) |
| 49 | self._text.configure(state = 'disabled') |
| 50 | |
| 51 | class Demo: |
| 52 | def __init__(self, parent): |
| 53 | # Create and pack the megawidget. |
| 54 | text = TextDisplay(parent, |
| 55 | text_background = 'aliceblue', |
| 56 | text_width = 40, |
| 57 | text_height = 10, |
| 58 | text_wrap = 'none', |
| 59 | ) |
| 60 | text.pack(fill = 'both', expand = 1) |
| 61 | text.display('This is an example of a simple Pmw megawidget.\n\n' + |
| 62 | 'Public attributes of the Tkinter module:\n\n') |
| 63 | for name in dir(Tkinter): |
| 64 | if name[0] != '_': |
| 65 | text.append(' ' + name + '\n') |
| 66 | |
| 67 | ###################################################################### |
| 68 | |
| 69 | # Create demo in root window for testing. |
| 70 | if __name__ == '__main__': |
| 71 | root = Tkinter.Tk() |
| 72 | Pmw.initialise(root) |
| 73 | root.title(title) |
| 74 | |
| 75 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 76 | exitButton.pack(side = 'bottom') |
| 77 | widget = Demo(root) |
| 78 | root.mainloop() |