Skip to content

Commit 2750ea6

Browse files
committed
2 parents c41aea9 + 339e459 commit 2750ea6

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
The basic from for the address book
5+
6+
This gets a Panel to itself
7+
"""
8+
9+
import wx
10+
11+
class A_Book_Form(wx.Panel):
12+
def __init__(self, a_entry, *args, **kwargs):
13+
wx.Panel.__init__(self, *args, **kwargs)
14+
15+
self.a_entry = a_entry
16+
17+
## create text boxes to edit: first name, last name, phone, email.
18+
self.fname_text = wx.TextCtrl(self)
19+
self.lname_text = wx.TextCtrl(self)
20+
self.phone_text = wx.TextCtrl(self)
21+
self.email_text = wx.TextCtrl(self)
22+
23+
## use a FlexGridSizer:
24+
S = wx.FlexGridSizer(rows=0, cols=2, vgap=8, hgap=8)
25+
S.AddGrowableCol(idx=1, proportion=1)
26+
27+
S.Add(wx.StaticText(self, label="First Name:"), 0,
28+
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
29+
S.Add(self.fname_text, flag=wx.EXPAND)
30+
31+
S.Add(wx.StaticText(self, label="Last Name:"), 0,
32+
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
33+
S.Add(self.lname_text, flag=wx.EXPAND)
34+
35+
S.Add(wx.StaticText(self, label="Phone Number:"), 0,
36+
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
37+
S.Add(self.phone_text, flag=wx.EXPAND)
38+
39+
S.Add(wx.StaticText(self, label="Email Address:"), 0,
40+
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
41+
S.Add(self.email_text, flag=wx.EXPAND)
42+
43+
#Put the whole thing in another sizer to give it some space
44+
Outer_Sizer = wx.BoxSizer(wx.VERTICAL)
45+
Outer_Sizer.Add(S, 0, wx.ALL|wx.EXPAND, 10)
46+
self.SetSizerAndFit(Outer_Sizer)
47+
48+
self.load_data()
49+
50+
def load_data(self):
51+
"""
52+
load the data into the form from the data dict
53+
"""
54+
data = self.a_entry
55+
self.fname_text.Value = data[u'first_name']
56+
self.lname_text.Value = data[u'last_name']
57+
self.phone_text.Value = data[u'phone']
58+
self.email_text.Value = data[u'email']
59+
60+
def save_data(self):
61+
"""
62+
save the data from the form from the data dict
63+
"""
64+
data = self.a_entry
65+
data[u'first_name'] = self.fname_text.Value
66+
data[u'last_name'] = self.lname_text.Value
67+
data[u'phone'] = self.phone_text.Value
68+
data[u'email'] = self.email_text.Value
69+
70+
71+
# I like to have a little test app so it can be done on its own
72+
if __name__ == "__main__":
73+
74+
# a sample entry:
75+
entry = {u'email': u'[email protected]',
76+
u'first_name': u'Chris',
77+
u'last_name': u'Barker',
78+
u'phone': u'123-456-7890'}
79+
80+
app = wx.App(False)
81+
f = wx.Frame(None)
82+
p = A_Book_Form(entry, f)
83+
f.Show()
84+
app.MainLoop()

week-09/code/address_book/address_book_data.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@ def __init__(self):
1616
self.filename = "a_book.json"
1717

1818
def save_to_file(self, filename=None):
19-
if filename != None :
19+
if filename is not None :
2020
self.filename = filename
21-
json.dump(self.book, open(filename, 'wb'), indent=4 )
21+
json.dump(self.book, open(self.filename, 'wb'), indent=4 )
2222

2323
def load_from_file(self, filename=None):
24-
if filename != None :
24+
if filename is not None :
2525
self.filename = filename
26-
self.book = json.load( open(filename, 'rb') )
26+
self.book = json.load( open(self.filename, 'rb') )
2727

2828

2929
if __name__ == "__main__":
3030
import pprint
3131
a_book = AddressBook()
3232
a_book.load_from_file()
3333

34+
print "the data in the address book is:"
3435
pprint.pprint(a_book.book)
3536

week-09/presentation-week09.tex

+11-1
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,17 @@ \section{Miscellaneous}
918918

919919
\vfill
920920
{\Large Make a very simple address book app:}
921-
\vfill
921+
922+
\begin{enumerate}
923+
\item Really basic data model is in \verb`address_book_data.py`
924+
\item Make a form to edit an entry -- subclass a \verb`wx.Panel` (a_book_form.py)
925+
\item Put the form in a \verb`wx.Frame` -- the frame should have a way to switch between entries
926+
\item Add file_save and file_open menus to the frame
927+
\item Add some validation!
928+
\end{enumerate}
929+
930+
931+
922932

923933
\verb`code\address_book\`
924934

0 commit comments

Comments
 (0)