summaryrefslogtreecommitdiff
path: root/uitools/LabelEntry.py
blob: 203427a42f67099cc65448c5d1f4f08664e40142 (plain)
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
######################################################################
# This class provides an Entry with a label on its left side.
# The text of the label can be set by means of the 'text'
# configuration key.
#
# Mitch Chapman
#---------------------------------------------------------------------
# $Log: LabelEntry.py,v $
# Revision 1.1  1996/12/01 22:58:54  mchapman
# Initial revision
#
######################################################################

__version__ = "$Revision: 1.1 $"

import Tkinter; Tk=Tkinter

######################################################################
# A MultiColEntry is a label together with one or more entries.
# Clients have direct access to the constituent widgets:
#   frame   -- the frame containing the other widgets.
#   label   -- the label appearing on the left side
#   entries -- The entries arrayed across from left to right
######################################################################
class MultiColEntry:
    ##################################################################
    # Initialize a new instance.
    ##################################################################
    def __init__(self, master=None, columns=2, entryClass=Tk.Entry):
	f = self.frame = Tk.Frame(master)
	l = self.label = Tk.Label(f, anchor='ne')
	l.pack(side='left')
	self.entries = []
	for i in range(columns):
	    e = entryClass(f)
	    self.entries.append(e)
	    e.pack(side='left', fill='x', expand='yes')
	    
    ##################################################################
    # Set the values of one or more entry fields.
    # It's the caller's responsibility not to exceed the number of
    # entries in self.
    ##################################################################
    def setValues(self, startIndex=0, *newValues):
        i = startIndex
        for v in newValues:
            e = self.entries[i]
            e.delete('0', 'end')
	    e.insert('end', v)


######################################################################
# A labelled entry is a MultiColEntry with only one entry column.
# The instance variable "entry" provides a more-convenient way to
# refer to the sole entry widget.
######################################################################
class LabelEntry(MultiColEntry):
    ##################################################################
    # Initialize a new instance.
    ##################################################################
    def __init__(self, master=None, entryClass=Tk.Entry):
	MultiColEntry.__init__(self, master, 1, entryClass)
	self.entry = self.entries[0]

    ##################################################################
    # Set the whole value of an Entry.
    ##################################################################
    def setValue(self, newValue):
	self.entry.delete(0, 'end')
	self.entry.insert('end', newValue)

    ##################################################################
    # Delete LabelEntry text.
    ##################################################################
    def delete(self, first, last=None):
	self.entry.delete(first, last)

    ##################################################################
    # Insert LabelEntry text.
    ##################################################################
    def insert(self, index, string):
	self.entry.insert(index, string)

######################################################################
# A ROLabelEntry is read-only, so far as the user is concerned.
######################################################################
class ROLabelEntry(LabelEntry):
    def __init__(self, master=None):
	LabelEntry.__init__(self, master, Tk.Entry)
	self.entry['state'] = 'disabled'

    ##################################################################
    # Delete text from the entry.
    ##################################################################
    def delete(self, first, last=None):
	self.entry['state'] = 'normal'
	self.entry.delete(first, last)
	self.entry['state'] = 'disabled'

    ##################################################################
    # Insert text into the entry.
    ##################################################################
    def insert(self, index, string):
	self.entry['state'] = 'normal'
	self.entry.insert(index, string)
	self.entry['state'] = 'disabled'
    

######################################################################
# Main function for unit testing.
######################################################################
def main():
    f = Tk.Frame()

    le = LabelEntry(f)
    le.label['text'] = "New Value:"
    newValue = "This is the new value."
    le.entry['width'] = len(newValue)
    le.insert('end', newValue)

    readOnly = ROLabelEntry(f)
    readOnly.label['text'] = 'Label:'
    readOnly.entry['relief'] = 'groove'
    for w in [readOnly.label, readOnly.entry]:
	w['width'] = 40
	
    readOnly.insert('0', 'This is the value')

    mc = MultiColEntry(f, 8)
    mc.label['text'] = "Multi:"
    map(lambda e: e.config(width=8), mc.entries)

    for w in [le, readOnly, mc]:
        w.label['width'] = 12
	w.frame.pack(fill='x')

    f.update()
    f.pack()
    f.wait_visibility()
    f.after(2000, readOnly.insert, 'end', ' after 2 seconds')

    ##################################################################
    # Insert text in a multi-column widget.
    ##################################################################
    def insertMulti(mc):
	map(lambda e: e.insert('end', 'This is ' + `e`), mc.entries)

    mc.setValues(2, "This is entry 2.", "This is entry 3.", "This is entry 4.")

    f.after(4000, insertMulti, mc)
    f.mainloop()

if __name__ == "__main__":
    main()