Skip to content

Commit 82d7488

Browse files
committed
updated wxpython solution
1 parent 4e2c4cd commit 82d7488

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

Examples/wxpython/address_book_solution/entry_form.py

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,23 @@ def __init__(self, a_entry, *args, **kwargs):
2020

2121
self._entry = a_entry
2222

23-
# create text boxes to edit: first name, last name, phone, email.
24-
self.fname_text = wx.TextCtrl(self)
25-
self.lname_text = wx.TextCtrl(self)
26-
self.phone_text = wx.TextCtrl(self)
27-
self.email_text = wx.TextCtrl(self)
28-
2923
# use a FlexGridSizer:
3024
S = wx.FlexGridSizer(rows=0, cols=2, vgap=8, hgap=8)
3125
S.AddGrowableCol(idx=1, proportion=1)
3226

33-
S.Add(wx.StaticText(self, label="First Name:"), 0,
34-
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
35-
S.Add(self.fname_text, flag=wx.EXPAND)
36-
37-
S.Add(wx.StaticText(self, label="Last Name:"), 0,
38-
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
39-
S.Add(self.lname_text, flag=wx.EXPAND)
40-
41-
S.Add(wx.StaticText(self, label="Phone Number:"), 0,
42-
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
43-
S.Add(self.phone_text, flag=wx.EXPAND)
44-
45-
S.Add(wx.StaticText(self, label="Email Address:"), 0,
46-
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
47-
S.Add(self.email_text, flag=wx.EXPAND)
48-
27+
# create text boxes to edit: first name, last name, phone, email.
28+
self.inputs = {}
29+
# Would be even better to have this in the database
30+
ordered_inputs = [('first_name','First Name'),
31+
('last_name', 'Last Name'),
32+
('phone', 'Phone'),
33+
('email', 'Email')]
34+
for key, name in ordered_inputs:
35+
self.inputs[key] = wx.TextCtrl(self)
36+
S.Add(wx.StaticText(self, label=name), 0,
37+
wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
38+
S.Add(self.inputs[key], flag=wx.EXPAND)
39+
4940
# Save and Cancel buttons
5041
sav_but = wx.Button(self, label="Save Record")
5142
sav_but.Bind(wx.EVT_BUTTON, self.onSave)
@@ -89,20 +80,16 @@ def load_data(self):
8980
load the data into the form from the data dict
9081
"""
9182
data = self._entry
92-
self.fname_text.Value = data.setdefault('first_name', "")
93-
self.lname_text.Value = data.setdefault('last_name', "")
94-
self.phone_text.Value = data.setdefault('phone', "")
95-
self.email_text.Value = data.setdefault('email', "")
83+
for key, value in data.items():
84+
self.inputs[key].Value = data.setdefault(key, "")
9685

9786
def save_data(self):
9887
"""
9988
save the data from the form from the data dict
10089
"""
10190
data = self._entry
102-
data['first_name'] = self.fname_text.Value
103-
data['last_name'] = self.lname_text.Value
104-
data['phone'] = self.phone_text.Value
105-
data['email'] = self.email_text.Value
91+
for key, value in data.items():
92+
data[key] = self.inputs[key].Value
10693

10794

10895
# I like to have a little test app so it can be run on its own

0 commit comments

Comments
 (0)