File: EntryField.py

package info (click to toggle)
python-pmw 0.6.2-0.1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,652 kB
  • ctags: 2,716
  • sloc: python: 10,720; makefile: 44; sh: 24
file content (64 lines) | stat: -rw-r--r-- 1,624 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
""" Demonstration of the Pmw EntryField megawidget.
"""

# Import Pmw from the sibling directory.
import sys
sys.path[:0] = ['../../..']

import Tkinter
import Pmw

class Demo:
    def __init__(self, parent):
	# Create and pack the EntryFields.
	self._any = Pmw.EntryField(parent,
		labelpos = 'w',
		label_text = 'Any:',
		command = self.execute)
	self._real = Pmw.EntryField(parent,
		labelpos = 'w',
		value = '+2.9979e+8',
		label_text = 'Real:',
		validate = 'real',
		modifiedcommand = self.changed)
	self._odd = Pmw.EntryField(parent,
		labelpos = 'w',
		label_text = 'Odd length:',
		value = 'ABC',
		validate = self.custom_validate)

	entries = (self._any, self._real, self._odd)

	for entry in entries:
	    entry.pack(fill='x', expand=1, padx=10, pady=5)
	Pmw.alignlabels(entries)

	self._any.component('entry').focus_set()

    def changed(self):
	print 'Text changed, value is', self._real.get()

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

    # This implements a custom validation routine.  It simply checks
    # if the string is of odd length.
    def custom_validate(self, text):
	print 'text:', text
	if len(text) % 2 == 0:
	  return -1
	else:
	  return 1

######################################################################

# Create demo in root window for testing.
if __name__ == '__main__':
    root = Tkinter.Tk()
    Pmw.initialise(root, fontScheme = 'pmw1')
    root.title('Pmw EntryField demonstration')

    exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
    exitButton.pack(side = 'bottom')
    widget = Demo(root)
    root.mainloop()