| 1 | title = 'Pmw.CounterDialog demonstration' |
| 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 | class Demo: |
| 12 | def __init__(self, parent): |
| 13 | # Create the dialog to prompt for the number of times to ring the bell. |
| 14 | self.dialog = Pmw.CounterDialog(parent, |
| 15 | label_text = 'Enter the number of times to\n' + \ |
| 16 | 'sound the bell (1 to 5)\n', |
| 17 | counter_labelpos = 'n', |
| 18 | entryfield_value = 2, |
| 19 | counter_datatype = 'numeric', |
| 20 | entryfield_validate = |
| 21 | {'validator' : 'numeric', 'min' : 1, 'max' : 5}, |
| 22 | buttons = ('OK', 'Cancel'), |
| 23 | defaultbutton = 'OK', |
| 24 | title = 'Bell ringing', |
| 25 | command = self.execute) |
| 26 | self.dialog.withdraw() |
| 27 | |
| 28 | # Create button to launch the dialog. |
| 29 | w = Tkinter.Button(parent, text = 'Show counter dialog', |
| 30 | command = self.dialog.activate) |
| 31 | w.pack(padx = 8, pady = 8) |
| 32 | |
| 33 | def execute(self, result): |
| 34 | if result is None or result == 'Cancel': |
| 35 | print 'Bell ringing cancelled' |
| 36 | self.dialog.deactivate() |
| 37 | else: |
| 38 | count = self.dialog.get() |
| 39 | if not self.dialog.valid(): |
| 40 | print 'Invalid entry: "' + count + '"' |
| 41 | else: |
| 42 | print 'Ringing the bell ' + count + ' times' |
| 43 | for num in range(string.atoi(count)): |
| 44 | if num != 0: |
| 45 | self.dialog.after(200) |
| 46 | self.dialog.bell() |
| 47 | self.dialog.deactivate() |
| 48 | |
| 49 | ###################################################################### |
| 50 | |
| 51 | # Create demo in root window for testing. |
| 52 | if __name__ == '__main__': |
| 53 | root = Tkinter.Tk() |
| 54 | Pmw.initialise(root) |
| 55 | root.title(title) |
| 56 | |
| 57 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 58 | exitButton.pack(side = 'bottom') |
| 59 | widget = Demo(root) |
| 60 | root.mainloop() |