Skip to content

Commit 501f1cb

Browse files
got the entry_form on the man frame -- still more to do!
1 parent 2750ea6 commit 501f1cb

File tree

6 files changed

+160
-191
lines changed

6 files changed

+160
-191
lines changed

week-09/code/address_book/a_book.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
[
22
{
3+
"phone": "123-456-7890",
34
"first_name": "Chris",
45
"last_name": "Barker",
5-
"phone": "123-456-7890",
66
"email": "[email protected]"
77
},
88
{
9+
"phone": "510-555-1234",
910
"first_name": "Fred",
1011
"last_name": "Jones",
11-
"phone": "510-555-1234",
1212
"email": "FredJones@some_company.com"
1313
},
1414
{
15+
"phone": "423-321-9876",
1516
"first_name": "Nancy",
1617
"last_name": "Wilson",
17-
"phone": "423-321-9876",
1818
"email": "[email protected]"
1919
}
2020
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Example of the very basic, minimal wxPython address book application
5+
6+
This module defines the main Frame
7+
"""
8+
9+
import os
10+
11+
import wx
12+
from address_book_data import AddressBook
13+
from entry_form import AddBookForm
14+
15+
class AddBookFrame(wx.Frame):
16+
def __init__(self, add_book, *args, **kwargs):
17+
kwargs.setdefault('title', "Micro Address Book")
18+
wx.Frame.__init__(self, *args, **kwargs)
19+
20+
self.add_book = add_book
21+
self.current_index = 1
22+
23+
# put the Panel on the frame
24+
self.entryPanel = AddBookForm(add_book.book[self.current_index], self)
25+
26+
# Build up the menu bar:
27+
menuBar = wx.MenuBar()
28+
29+
fileMenu = wx.Menu()
30+
openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a file" )
31+
self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem)
32+
33+
closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" )
34+
self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem)
35+
36+
exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application")
37+
self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem)
38+
menuBar.Append(fileMenu, "&File")
39+
40+
helpMenu = wx.Menu()
41+
helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help")
42+
menuBar.Append(helpMenu, "&Help")
43+
44+
self.SetMenuBar(menuBar)
45+
46+
def onOpen(self, evt=None):
47+
"""This method opens an existing file"""
48+
print "Open a file: "
49+
# Create the dialog. In this case the current directory is forced as the starting
50+
# directory for the dialog, and no default file name is forced. This can easily
51+
# be changed in your program. This is an 'open' dialog, and allows multiple
52+
# file selections as well.
53+
#
54+
# Finally, if the directory is changed in the process of getting files, this
55+
# dialog is set up to change the current working directory to the path chosen.
56+
dlg = wx.FileDialog(
57+
self, message="Choose a file",
58+
defaultDir=os.getcwd(),
59+
defaultFile="",
60+
wildcard="*.json",
61+
style=wx.OPEN | wx.CHANGE_DIR
62+
)
63+
64+
# Show the dialog and retrieve the user response. If it is the OK response,
65+
# process the data.
66+
if dlg.ShowModal() == wx.ID_OK:
67+
# This returns a Python list of files that were selected.
68+
path = dlg.GetPath()
69+
print "I'd be opening file in onOpen ", path
70+
self.add_book.save_to_file( path )
71+
else :
72+
print "The file dialog was canceled before anything was selected"
73+
74+
# Destroy the dialog. Don't do this until you are done with it!
75+
# BAD things can happen otherwise!
76+
dlg.Destroy()
77+
78+
def onClose(self, evt=None):
79+
print "close menu selected"
80+
self.add_book.close()
81+
82+
def onExit(self, evt=None):
83+
print "Exit the program here"
84+
print "The event passed to onExit is type ", type(evt),
85+
self.Close()
86+
87+
88+
class AddBookApp(wx.App):
89+
def OnInit(self):
90+
"""
91+
App initilization goes here -- not much to do, in this case
92+
"""
93+
a_book = AddressBook()
94+
a_book.load_from_file()
95+
96+
f = AddBookFrame(a_book, parent=None)
97+
f.Show()
98+
99+
return True
100+
101+
if __name__ == "__main__":
102+
103+
app = AddBookApp(False)
104+
105+
106+
107+
## set up the WIT -- to help debug sizers
108+
# import wx.lib.inspection
109+
# wx.lib.inspection.InspectionTool().Show()
110+
app.MainLoop()
111+

week-09/code/address_book/address_book_data.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
class AddressBook(object):
1111
"""
1212
very simple data model -- just a list of dicts
13+
14+
each dict represents an entry in the address book
1315
"""
1416
def __init__(self):
15-
self.book = []
17+
self.book = [{},]
1618
self.filename = "a_book.json"
1719

1820
def save_to_file(self, filename=None):
@@ -25,6 +27,13 @@ def load_from_file(self, filename=None):
2527
self.filename = filename
2628
self.book = json.load( open(self.filename, 'rb') )
2729

30+
def close(self):
31+
"""
32+
clear out the data...
33+
leave it with one empty dict
34+
"""
35+
del self.book[:]
36+
self.book.append({})
2837

2938
if __name__ == "__main__":
3039
import pprint

week-09/code/address_book/demo_addr_book.py

-178
This file was deleted.

0 commit comments

Comments
 (0)