File: Counter.html

package info (click to toggle)
python-pmw 0.8.5-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,820 kB
  • ctags: 3,468
  • sloc: python: 14,898; makefile: 37; sh: 17
file content (432 lines) | stat: -rw-r--r-- 16,958 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432

    <html>
    <head>
    <meta name="description" content="Pmw - a toolkit for building high-level compound widgets in Python">
    <meta name="content" content="python, megawidget, mega widget, compound widget, gui, tkinter">
    <title>Pmw.Counter reference manual</title>
    </head>

    <body bgcolor="#ffffff" text="#000000" link="#0000ee"
	vlink="551a8b" alink="ff0000">

    <h1 ALIGN="CENTER">Pmw.Counter</h1>
    <p>
    
<center><IMG SRC=Counter.gif ALT="" WIDTH=400 HEIGHT=110></center>
<dl>
<dt> <h3>Name</h3><dd>
<p>Pmw.Counter() - 
    entry field with up and down arrow buttons
</p>


<dt> <h3>Inherits</h3><dd>
<a href="MegaWidget.html">Pmw.MegaWidget</a><br>
<dt> <h3>Description</h3><dd>
<p>
    This class consists of an entry field with arrow buttons to
    increment and decrement the value in the entry field.  Standard
    counting types include numbers, times and dates.  A user defined
    counting function may also be supplied for specialised counting. 
    Counting can be used in combination with the entry field's
    validation.  The components may be laid out horizontally or
    vertically.</p>

<p>    Each time an arrow button is pressed the value displayed in the
    entry field is incremented or decremented by the value of the
    <strong>increment</strong> option.  If the new value is invalid (according to the
    entry field's <strong>validate</strong> option, perhaps due to exceeding minimum
    or maximum limits), the old value is restored.</p>

<p>    When an arrow button is pressed and the value displayed is not an
    exact multiple of the <strong>increment</strong>, it is "truncated" up or down to
    the nearest increment.</p>

<p></p>


<dt> <h3>Options</h3><dd>
Options for this megawidget and its base
classes are described below.<p>
<a name=option.autorepeat></a>
<dl><dt> <strong>autorepeat
</strong><dd>
Initialisation option. If true, the counter will continue to count up or down while an
    arrow button is held pressed down. The default is <strong>1</strong>.</p>


</dt></dl>
<a name=option.buttonaspect></a>
<dl><dt> <strong>buttonaspect
</strong><dd>
Initialisation option. Specifies the width of the arrow buttons as a proportion of their
    height.  Values less than <strong>1.0</strong> will produce thin arrow buttons. 
    Values greater than <strong>1.0</strong> will produce fat arrow buttons. The default is <strong>1.0</strong>.</p>


</dt></dl>
<a name=option.datatype></a>
<dl><dt> <strong>datatype
</strong><dd>
Specifies how the counter should count up and down.</p>
<p>    The most general way to specify the <strong>datatype</strong> option is as a
    dictionary.  The kind of counting is specified by the <strong>'counter'</strong>
    dictionary field, which may be either a function or the name of
    one of the standard counters described below.  The default is
    <strong>'numeric'</strong>.</p>

<p>    Any other fields in the dictionary are passed on to the <em>counter</em>
    function as keyword arguments.</p>

<p>    If <strong>datatype</strong> is not a dictionary, then it is equivalent to
    specifying it as a dictionary with a single <strong>'counter'</strong> field. 
    For example, <code>datatype = 'real'</code> is equivalent to
    <code>datatype = {'counter' : 'real'}</code>.</p>

<p>    The standard counters are:</p>

<dl><dt><strong>'numeric'</strong><dd>An integer number, as accepted by <code>string.atol()</code>.</p>


<dt><strong>'integer'</strong><dd>Same as <strong>'numeric'</strong>.</p>


<dt><strong>'real'</strong><dd>A real number, as accepted by <code>string.atof()</code>.  This
        counter accepts a <strong>'separator'</strong> argument, which specifies
        the charactor used to represent the decimal point.  The
        default <strong>'separator'</strong> is <strong>'.'</strong>.</p>


<dt><strong>'time'</strong><dd>A time specification, as accepted by
        <code>Pmw.timestringtoseconds()</code>.  This counter accepts a
        <strong>'separator'</strong> argument, which specifies the charactor used to
        separate the time fields.  The default separator is <strong>':'</strong>.</p>


<dt><strong>'date'</strong><dd>A date specification, as accepted by
        <code>Pmw.datestringtojdn()</code>.  This counter accepts a <strong>'separator'</strong>
        argument, which specifies the charactor used to separate the
        three date fields.  The default is <strong>'/'</strong>.  This counter also
        accepts a <strong>'format'</strong> argument, which is passed to
        <code>Pmw.datestringtojdn()</code> to specify the desired ordering of the
        fields.  The default is <strong>'ymd'</strong>.</p>

</dl>
<p>    If <strong>'counter'</strong> is a function, then it will be called whenever the
    counter is incremented or decremented.  The function is called
    with at least three arguments, the first three being (<em>text</em>,
    <em>factor</em>, <em>increment</em>), where <em>text</em> is the current contents of
    the entry field, <em>factor</em> is <strong>1</strong> when incrementing or <strong>-1</strong> when
    decrementing, and <em>increment</em> is the value of the <strong>increment</strong>
    megawidget option.</p>

<p>    The other arguments are keyword arguments made up of the fields of
    the <strong>datatype</strong> dictionary (excluding the <strong>'counter'</strong> field).</p>

<p>    The <em>counter</em> function should return a string representing the
    incremented or decremented value.  It should raise a a
    <strong>ValueError</strong> exception if the <em>text</em> is invalid.  In this case the
    bell is rung and the entry text is is not changed.</p>

<p>    The default for <strong>datatype</strong> is <strong>numeric</strong>.</p>



</dt></dl>
<a name=option.increment></a>
<dl><dt> <strong>increment
</strong><dd>
Specifies how many units should be added or subtracted when the
    counter is incremented or decremented.  If the currently displayed
    value is not a multiple of <strong>increment</strong>, the value is changed to
    the next multiple greater or less than the current value.</p>
<p>    For the number datatypes, the value of <strong>increment</strong> is a number. 
    For the <strong>'time'</strong> datatype, the value is in seconds.  For the
    <strong>'date'</strong> datatype, the value is in days. The default is <strong>1</strong>.</p>



</dt></dl>
<a name=option.initwait></a>
<dl><dt> <strong>initwait
</strong><dd>
Initialisation option. Specifies the initial delay (in milliseconds) before a depressed
    arrow button automatically starts to repeat counting. The default is <strong>300</strong>.</p>


</dt></dl>
<a name=option.labelmargin></a>
<dl><dt> <strong>labelmargin
</strong><dd>
Initialisation option. If the <strong>labelpos</strong> option is not <strong>None</strong>, this specifies the
        distance between the <strong>label</strong> component and the rest of the
        megawidget. The default is <strong>0</strong>.</p>


</dt></dl>
<a name=option.labelpos></a>
<dl><dt> <strong>labelpos
</strong><dd>
Initialisation option. Specifies where to place the <strong>label</strong> component.  If not
        <strong>None</strong>, it should be a concatenation of one or two of the
        letters <strong>'n'</strong>, <strong>'s'</strong>, <strong>'e'</strong> and <strong>'w'</strong>.  The first letter
        specifies on which side of the megawidget to place the label. 
        If a second letter is specified, it indicates where on that
        side to place the label.  For example, if <strong>labelpos</strong> is <strong>'w'</strong>,
        the label is placed in the center of the left hand side; if
        it is <strong>'wn'</strong>, the label is placed at the top of the left
        hand side; if it is <strong>'ws'</strong>, the label is placed at the
        bottom of the left hand side.</p>
<p>        If <strong>None</strong>, a label component is not created. The default is <strong>None</strong>.</p>



</dt></dl>
<a name=option.orient></a>
<dl><dt> <strong>orient
</strong><dd>
Initialisation option. Specifies whether the arrow buttons should appear to the left and
    right of the entry field (<strong>'horizontal'</strong>) or above and below
    (<strong>'vertical'</strong>). The default is <strong>'horizontal'</strong>.</p>


</dt></dl>
<a name=option.padx></a>
<dl><dt> <strong>padx
</strong><dd>
Initialisation option. Specifies a padding distance to leave around the arrow buttons in
    the x direction. The default is <strong>0</strong>.</p>


</dt></dl>
<a name=option.pady></a>
<dl><dt> <strong>pady
</strong><dd>
Initialisation option. Specifies a padding distance to leave around the arrow buttons in
    the y direction. The default is <strong>0</strong>.</p>


</dt></dl>
<a name=option.repeatrate></a>
<dl><dt> <strong>repeatrate
</strong><dd>
Initialisation option. Specifies the delay (in milliseconds) between automatic counts
    while an arrow button is held pressed down. The default is <strong>50</strong>.</p>


</dt></dl>
<dt> <h3>Components</h3><dd>
Components created by this megawidget and its base
classes are described below.<p>
<a name=component.downarrow></a>
<dl><dt> <strong>downarrow
</strong><dd>
The arrow button used for decrementing the counter.  Depending on
    the value of <strong>orient</strong>, it will appear on the left or below the
    entry field. By default, this component is a Tkinter.Canvas. Its component group is <strong>Arrow</strong>.</p>


</dt></dl>
<a name=component.entryfield></a>
<dl><dt> <strong>entryfield
</strong><dd>
The entry field widget where the text is entered, displayed and
    validated. By default, this component is a <a href="EntryField.html">Pmw.EntryField</a>.</p>


</dt></dl>
<a name=component.frame></a>
<dl><dt> <strong>frame
</strong><dd>
If the <strong>label</strong> component has been created (that is, the <strong>labelpos</strong>
    option is not <strong>None</strong>), the <strong>frame</strong> component is created to act as
    the container of the entry field and arrow buttons.  If there is
    no <strong>label</strong> component, then no <strong>frame</strong> component is created and the
    <strong>hull</strong> component acts as the container.  In either case the border
    around the container of the entry field and arrow buttons will be
    raised (but not around the label). By default, this component is a Tkinter.Frame.</p>


</dt></dl>
<a name=component.hull></a>
<dl><dt> <strong>hull
</strong><dd>
This acts as the body for the entire megawidget.  Other components
    are created as children of the hull to further specialise the
    widget. By default, this component is a Tkinter.Frame.</p>


</dt></dl>
<a name=component.label></a>
<dl><dt> <strong>label
</strong><dd>
If the <strong>labelpos</strong> option is not <strong>None</strong>, this component is
        created as a text label for the megawidget.  See the
        <strong>labelpos</strong> option for details.  Note that to set, for example,
        the <strong>text</strong> option of the label, you need to use the <strong>label_text</strong>
        component option. By default, this component is a Tkinter.Label.</p>


</dt></dl>
<a name=component.uparrow></a>
<dl><dt> <strong>uparrow
</strong><dd>
The arrow button used for incrementing the counter.  Depending on
    the value of <strong>orient</strong>, it will appear on the right or above the
    entry field. By default, this component is a Tkinter.Canvas. Its component group is <strong>Arrow</strong>.</p>


</dt></dl>
<dt> <h3>Component aliases</h3><dd>
Sub-components of components of this megawidget
may be accessed via the following aliases.<p>
<dl><dt> <strong>entry
</strong><dd>
Alias for <strong>entryfield_entry</strong>.
</dt></dl>
<a name=methods></a>
<dt> <h3>Methods</h3><dd>
Only methods specific to this megawidget are described below.
For a description of its inherited methods, see the
manuals for its base classes.
In addition, methods from the
<strong><a href="EntryField.html#methods">Pmw.EntryField</a></strong> class
are forwarded by this megawidget to the
<strong>entryfield</strong> component.
<p>
<a name=method.decrement></a>
<dl><dt> <strong>decrement</strong>()<dd>
Decrement the counter once, as if the down arrow had been pressed.</p>


</dt></dl>
<a name=method.increment></a>
<dl><dt> <strong>increment</strong>()<dd>
Increment the counter once, as if the up arrow had been pressed.</p>


</dt></dl>
<dt> <h3>Example</h3><dd>
The image at the top of this manual is a snapshot
of the window (or part of the window) produced
by the following code.<p>
<pre>
class Demo:
    def __init__(self, parent):
        # Need to use long ints here because on the Macintosh the maximum size
        # of an integer is smaller than the value returned by time.time().
        now = (long(time.time()) / 300) * 300

        # Create the Counters.
        self._date = Pmw.Counter(parent,
                labelpos = 'w',
                label_text = 'Date (4-digit year):',
                entryfield_value =
                        time.strftime('%d/%m/%Y', time.localtime(now)),
                entryfield_command = self.execute,
                entryfield_validate = {'validator' : 'date', 'format' : 'dmy'},
                datatype = {'counter' : 'date', 'format' : 'dmy', 'yyyy' : 1})

        self._isodate = Pmw.Counter(parent,
                labelpos = 'w',
                label_text = 'ISO-Date (4-digit year):',
                entryfield_value =
                        time.strftime('%Y-%m-%d', time.localtime(now)),
                entryfield_command = self.execute,
                entryfield_validate = {'validator' : 'date', 'format' : 'ymd',
                        'separator' : '-' },
                datatype = {'counter' : 'date', 'format' : 'ymd', 'yyyy' : 1,
                        'separator' : '-' })

        self._time = Pmw.Counter(parent,
                labelpos = 'w',
                label_text = 'Time:',
                entryfield_value =
                        time.strftime('%H:%M:%S', time.localtime(now)),
                entryfield_validate = {'validator' : 'time',
                        'min' : '00:00:00', 'max' : '23:59:59',
                        'minstrict' : 0, 'maxstrict' : 0},
                datatype = {'counter' : 'time', 'time24' : 1},
                increment=5*60)
        self._real = Pmw.Counter(parent,
                labelpos = 'w',
                label_text = 'Real (with comma):',
                entryfield_value = '1,5',
                datatype = {'counter' : 'real', 'separator' : ','},
                entryfield_validate = {'validator' : 'real',
                        'min' : '-2,0', 'max' : '5,0',
                        'separator' : ','},
                increment = 0.1)
        self._custom = Pmw.Counter(parent,
                labelpos = 'w',
                label_text = 'Custom:',
                entryfield_value = specialword[:4],
                datatype = _custom_counter,
                entryfield_validate = _custom_validate)
        self._int = Pmw.Counter(parent,
                labelpos = 'w',
                label_text = 'Vertical integer:',
                orient = 'vertical',
                entry_width = 2,
                entryfield_value = 50,
                entryfield_validate = {'validator' : 'integer',
                        'min' : 0, 'max' : 99}
        )

        counters = (self._date, self._isodate, self._time, self._real,
                self._custom)
        Pmw.alignlabels(counters)

        # Pack them all.
        for counter in counters:
            counter.pack(fill='x', expand=1, padx=10, pady=5)
        self._int.pack(padx=10, pady=5)

    def execute(self):
        print 'Return pressed, value is', self._date.get()

specialword = 'Monti Python ik den Holie Grailen (Bok)'

def _custom_validate(text):
    if string.find(specialword, text) == 0:
        return 1
    else:
        return -1

def _custom_counter(text, factor, increment):
    # increment is ignored here.
    if string.find(specialword, text) == 0:
        length = len(text)
        if factor == 1:
            if length &gt;= len(specialword):
                raise ValueError, 'maximum length reached'
            return specialword[:length + 1]
        else:
            if length == 0:
                raise ValueError, 'empty string'
            return specialword[:length - 1]
    else:
        raise ValueError, 'bad string ' + text

</pre>
</dl>

    <center><P ALIGN="CENTER">
    <IMG SRC = blue_line.gif ALT = "" WIDTH=320 HEIGHT=5>
    </p></center>
    

    <font size=-1>
    <center><P ALIGN="CENTER">
    <a href="index.html">Home</a>. 
    Pmw 0.8.5
    Maintainer
    <a href="mailto:gregm@iname.com">gregm@iname.com</a>.
     9 Feb 2001
    <br>Manual page last reviewed: 24 May 1998
    </p></center>
    </font>

    </body>
    </html>