Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | title = 'Using Tk option database to configure Pmw megawidgets' |
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 | self.parent = parent | |
13 | ||
14 | header = Tkinter.Label(parent, text = 'Select some Tk option ' + | |
15 | 'database values from\nthe lists, then click ' + | |
16 | '\'Create dialog\' to create\na MessageDialog with ' + | |
17 | 'these values as defaults.') | |
18 | header.pack(padx = 10, pady = 10) | |
19 | ||
20 | # Create and pack the ComboBoxes to select options. | |
21 | buttons = ( | |
22 | "('OK',)", | |
23 | "('Read', 'Write')", | |
24 | "('OK', 'Cancel')", | |
25 | "('OK', 'Apply', 'Cancel', 'Help')", | |
26 | ) | |
27 | ||
28 | if Tkinter.TkVersion >= 8.4: | |
29 | disabledState = 'readonly' | |
30 | else: | |
31 | disabledState = 'disabled' | |
32 | ||
33 | self._buttons = Pmw.ComboBox(parent, label_text = 'buttons:', | |
34 | labelpos = 'w', | |
35 | entry_state = disabledState, | |
36 | scrolledlist_items = buttons) | |
37 | self._buttons.pack(fill = 'x', expand = 1, padx = 8, pady = 8) | |
38 | self._buttons.selectitem(3) | |
39 | ||
40 | buttonboxpos = ('n', 's', 'e', 'w',) | |
41 | self._buttonboxpos = Pmw.ComboBox(parent, label_text = 'buttonboxpos:', | |
42 | labelpos = 'w', | |
43 | entry_state = disabledState, | |
44 | scrolledlist_items = buttonboxpos) | |
45 | self._buttonboxpos.pack(fill = 'x', expand = 1, padx = 8, pady = 8) | |
46 | self._buttonboxpos.selectitem(2) | |
47 | ||
48 | pad = ('0', '8', '20', '50',) | |
49 | self._pad = Pmw.ComboBox(parent, label_text = 'padx, pady:', | |
50 | labelpos = 'w', | |
51 | entry_state = disabledState, | |
52 | scrolledlist_items = pad) | |
53 | self._pad.pack(fill = 'x', expand = 1, padx = 8, pady = 8) | |
54 | self._pad.selectitem(1) | |
55 | ||
56 | Pmw.alignlabels((self._buttons, self._buttonboxpos, self._pad)) | |
57 | ||
58 | # Create button to launch the dialog. | |
59 | w = Tkinter.Button(parent, text = 'Create dialog', | |
60 | command = self._createDialog) | |
61 | w.pack(padx = 8, pady = 8) | |
62 | ||
63 | self.dialog = None | |
64 | ||
65 | def _createDialog(self): | |
66 | ||
67 | # Set the option database. | |
68 | buttons = self._buttons.get() | |
69 | buttonboxpos = self._buttonboxpos.get() | |
70 | pad = self._pad.get() | |
71 | self.parent.option_add('*MessageDialog.buttons', buttons) | |
72 | self.parent.option_add('*MessageDialog.buttonboxpos', buttonboxpos) | |
73 | self.parent.option_add('*ButtonBox.padx', pad) | |
74 | self.parent.option_add('*ButtonBox.pady', pad) | |
75 | ||
76 | # Create the dialog. | |
77 | if self.dialog is not None: | |
78 | self.dialog.destroy() | |
79 | ||
80 | text = ('This dialog was created by setting the Tk ' + | |
81 | 'option database:\n\n *MessageDialog.buttons: ' + buttons + | |
82 | '\n *MessageDialog.buttonboxpos: ' + buttonboxpos + | |
83 | '\n *ButtonBox.padx: ' + pad + | |
84 | '\n *ButtonBox.pady: ' + pad) | |
85 | self.dialog = Pmw.MessageDialog(self.parent, | |
86 | defaultbutton = 0, | |
87 | title = 'Pmw option database demonstration', | |
88 | message_justify = 'left', | |
89 | message_text = text) | |
90 | self.dialog.iconname('Test dialog') | |
91 | ||
92 | # Return the defaults to normal, otherwise all other demos | |
93 | # will be affected. | |
94 | self.parent.option_add('*MessageDialog.buttons', "('OK',)") | |
95 | self.parent.option_add('*MessageDialog.buttonboxpos', 's') | |
96 | self.parent.option_add('*ButtonBox.padx', 8) | |
97 | self.parent.option_add('*ButtonBox.pady', 8) | |
98 | ||
99 | ###################################################################### | |
100 | ||
101 | # Create demo in root window for testing. | |
102 | if __name__ == '__main__': | |
103 | root = Tkinter.Tk() | |
104 | Pmw.initialise(root, useTkOptionDb = 1) | |
105 | root.title(title) | |
106 | ||
107 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) | |
108 | exitButton.pack(side = 'bottom') | |
109 | widget = Demo(root) | |
110 | root.mainloop() |