| 1 | title = 'Using Tk option database to configure Tk widgets' |
| 2 | |
| 3 | # Import Pmw from this directory tree. |
| 4 | import sys |
| 5 | sys.path[:0] = ['../../..'] |
| 6 | |
| 7 | import string |
| 8 | import Tkinter |
| 9 | import Pmw |
| 10 | |
| 11 | info = """ |
| 12 | The Tk widgets contained in this |
| 13 | simple megawidget have been |
| 14 | configured using the Tk option |
| 15 | database. |
| 16 | *DemoClass*Listbox.cursor is 'heart' |
| 17 | *DemoClass*Entry.cursor is 'hand1' |
| 18 | *DemoClass*background is 'pink' |
| 19 | *DemoClass*highlightBackground is 'green' |
| 20 | *DemoClass*foreground is 'blue' |
| 21 | """ |
| 22 | |
| 23 | class DemoClass(Pmw.MegaWidget): |
| 24 | |
| 25 | # Demo Pmw megawidget. |
| 26 | |
| 27 | def __init__(self, parent = None, **kw): |
| 28 | |
| 29 | # Define the megawidget options. |
| 30 | optiondefs = () |
| 31 | self.defineoptions(kw, optiondefs) |
| 32 | |
| 33 | # Initialise the base class (after defining the options). |
| 34 | Pmw.MegaWidget.__init__(self, parent) |
| 35 | |
| 36 | interior = self.interior() |
| 37 | listbox = Tkinter.Listbox(interior, height = 12, width = 40) |
| 38 | listbox.pack(fill='both', expand='yes') |
| 39 | for line in string.split(info, '\n'): |
| 40 | listbox.insert('end', line) |
| 41 | |
| 42 | entry = Tkinter.Entry(interior) |
| 43 | entry.pack(fill='y') |
| 44 | entry.insert(0, 'Hello, World!') |
| 45 | |
| 46 | # Check keywords and initialise options. |
| 47 | self.initialiseoptions() |
| 48 | |
| 49 | class Demo: |
| 50 | def __init__(self, parent): |
| 51 | |
| 52 | # Test Tk option database settings. |
| 53 | parent.option_add('*DemoClass*Listbox.cursor', 'heart') |
| 54 | parent.option_add('*DemoClass*Entry.cursor', 'hand1') |
| 55 | parent.option_add('*DemoClass*background', 'pink') |
| 56 | parent.option_add('*DemoClass*highlightBackground', 'green') |
| 57 | parent.option_add('*DemoClass*foreground', 'blue') |
| 58 | |
| 59 | # Create and pack the megawidget. |
| 60 | demo = DemoClass(parent) |
| 61 | demo.pack(fill = 'both', expand = 1) |
| 62 | |
| 63 | ###################################################################### |
| 64 | |
| 65 | # Create demo in root window for testing. |
| 66 | if __name__ == '__main__': |
| 67 | root = Tkinter.Tk() |
| 68 | Pmw.initialise(root) |
| 69 | root.title(title) |
| 70 | |
| 71 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 72 | exitButton.pack(side = 'bottom') |
| 73 | widget = Demo(root) |
| 74 | root.mainloop() |