| 1 | title = 'Pmw.Counter demonstration' |
| 2 | |
| 3 | # Import Pmw from this directory tree. |
| 4 | import sys |
| 5 | sys.path[:0] = ['../../..'] |
| 6 | |
| 7 | import string |
| 8 | import time |
| 9 | import Tkinter |
| 10 | import Pmw |
| 11 | |
| 12 | class Demo: |
| 13 | def __init__(self, parent): |
| 14 | # Need to use long ints here because on the Macintosh the maximum size |
| 15 | # of an integer is smaller than the value returned by time.time(). |
| 16 | now = (long(time.time()) / 300) * 300 |
| 17 | |
| 18 | # Create the Counters. |
| 19 | self._date = Pmw.Counter(parent, |
| 20 | labelpos = 'w', |
| 21 | label_text = 'Date (4-digit year):', |
| 22 | entryfield_value = |
| 23 | time.strftime('%d/%m/%Y', time.localtime(now)), |
| 24 | entryfield_command = self.execute, |
| 25 | entryfield_validate = {'validator' : 'date', 'format' : 'dmy'}, |
| 26 | datatype = {'counter' : 'date', 'format' : 'dmy', 'yyyy' : 1}) |
| 27 | |
| 28 | self._isodate = Pmw.Counter(parent, |
| 29 | labelpos = 'w', |
| 30 | label_text = 'ISO-Date (4-digit year):', |
| 31 | entryfield_value = |
| 32 | time.strftime('%Y-%m-%d', time.localtime(now)), |
| 33 | entryfield_command = self.execute, |
| 34 | entryfield_validate = {'validator' : 'date', 'format' : 'ymd', |
| 35 | 'separator' : '-' }, |
| 36 | datatype = {'counter' : 'date', 'format' : 'ymd', 'yyyy' : 1, |
| 37 | 'separator' : '-' }) |
| 38 | |
| 39 | self._time = Pmw.Counter(parent, |
| 40 | labelpos = 'w', |
| 41 | label_text = 'Time:', |
| 42 | entryfield_value = |
| 43 | time.strftime('%H:%M:%S', time.localtime(now)), |
| 44 | entryfield_validate = {'validator' : 'time', |
| 45 | 'min' : '00:00:00', 'max' : '23:59:59', |
| 46 | 'minstrict' : 0, 'maxstrict' : 0}, |
| 47 | datatype = {'counter' : 'time', 'time24' : 1}, |
| 48 | increment=5*60) |
| 49 | self._real = Pmw.Counter(parent, |
| 50 | labelpos = 'w', |
| 51 | label_text = 'Real (with comma)\nand extra\nlabel lines:', |
| 52 | label_justify = 'left', |
| 53 | entryfield_value = '1,5', |
| 54 | datatype = {'counter' : 'real', 'separator' : ','}, |
| 55 | entryfield_validate = {'validator' : 'real', |
| 56 | 'min' : '-2,0', 'max' : '5,0', |
| 57 | 'separator' : ','}, |
| 58 | increment = 0.1) |
| 59 | self._custom = Pmw.Counter(parent, |
| 60 | labelpos = 'w', |
| 61 | label_text = 'Custom:', |
| 62 | entryfield_value = specialword[:4], |
| 63 | datatype = _custom_counter, |
| 64 | entryfield_validate = _custom_validate) |
| 65 | self._int = Pmw.Counter(parent, |
| 66 | labelpos = 'w', |
| 67 | label_text = 'Vertical integer:', |
| 68 | orient = 'vertical', |
| 69 | entry_width = 2, |
| 70 | entryfield_value = 50, |
| 71 | entryfield_validate = {'validator' : 'integer', |
| 72 | 'min' : 0, 'max' : 99} |
| 73 | ) |
| 74 | |
| 75 | counters = (self._date, self._isodate, self._time, self._real, |
| 76 | self._custom) |
| 77 | Pmw.alignlabels(counters) |
| 78 | |
| 79 | # Pack them all. |
| 80 | for counter in counters: |
| 81 | counter.pack(fill='both', expand=1, padx=10, pady=5) |
| 82 | self._int.pack(padx=10, pady=5) |
| 83 | |
| 84 | def execute(self): |
| 85 | print 'Return pressed, value is', self._date.get() |
| 86 | |
| 87 | specialword = 'Monti Python ik den Holie Grailen (Bok)' |
| 88 | |
| 89 | def _custom_validate(text): |
| 90 | if string.find(specialword, text) == 0: |
| 91 | return 1 |
| 92 | else: |
| 93 | return -1 |
| 94 | |
| 95 | def _custom_counter(text, factor, increment): |
| 96 | # increment is ignored here. |
| 97 | if string.find(specialword, text) == 0: |
| 98 | length = len(text) |
| 99 | if factor == 1: |
| 100 | if length >= len(specialword): |
| 101 | raise ValueError, 'maximum length reached' |
| 102 | return specialword[:length + 1] |
| 103 | else: |
| 104 | if length == 0: |
| 105 | raise ValueError, 'empty string' |
| 106 | return specialword[:length - 1] |
| 107 | else: |
| 108 | raise ValueError, 'bad string ' + text |
| 109 | |
| 110 | ###################################################################### |
| 111 | |
| 112 | # Create demo in root window for testing. |
| 113 | if __name__ == '__main__': |
| 114 | root = Tkinter.Tk() |
| 115 | Pmw.initialise(root) |
| 116 | root.title(title) |
| 117 | |
| 118 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 119 | exitButton.pack(side = 'bottom') |
| 120 | widget = Demo(root) |
| 121 | root.mainloop() |