|
1 |
| -import pickle |
2 |
| -import os.path |
3 |
| -from tkinter import * # Import tkinter |
4 |
| -import tkinter.messagebox |
5 |
| - |
6 |
| -class Address: |
7 |
| - def __init__(self, name, street, city, state, zip): |
8 |
| - self.name = name |
9 |
| - self.street = street |
10 |
| - self.city = city |
11 |
| - self.state = state |
12 |
| - self.zip = zip |
13 |
| - |
14 |
| -class AddressBook: |
15 |
| - def __init__(self): |
16 |
| - window = Tk() # Create a window |
17 |
| - window.title("AddressBook") # Set title |
18 |
| - |
19 |
| - self.nameVar = StringVar() |
20 |
| - self.streetVar = StringVar() |
21 |
| - self.cityVar = StringVar() |
22 |
| - self.stateVar = StringVar() |
23 |
| - self.zipVar = StringVar() |
24 |
| - |
25 |
| - frame1 = Frame(window) |
26 |
| - frame1.pack() |
27 |
| - Label(frame1, text = "Name").grid(row = 1, |
28 |
| - column = 1, sticky = W) |
29 |
| - Entry(frame1, textvariable = self.nameVar, |
30 |
| - width = 40).grid(row = 1, column = 2) |
31 |
| - |
32 |
| - frame2 = Frame(window) |
33 |
| - frame2.pack() |
34 |
| - Label(frame2, text = "Street").grid(row = 1, |
35 |
| - column = 1, sticky = W) |
36 |
| - Entry(frame2, textvariable = self.streetVar, |
37 |
| - width = 40).grid(row = 1, column = 2) |
38 |
| - |
39 |
| - frame3 = Frame(window) |
40 |
| - frame3.pack() |
41 |
| - Label(frame3, text = "City", width = 5).grid(row = 1, |
42 |
| - column = 1, sticky = W) |
43 |
| - Entry(frame3, |
44 |
| - textvariable = self.cityVar).grid(row = 1, column = 2) |
45 |
| - Label(frame3, text = "State").grid(row = 1, |
46 |
| - column = 3, sticky = W) |
47 |
| - Entry(frame3, textvariable = self.stateVar, |
48 |
| - width = 5).grid(row = 1, column = 4) |
49 |
| - Label(frame3, text = "ZIP").grid(row = 1, |
50 |
| - column = 5, sticky = W) |
51 |
| - Entry(frame3, textvariable = self.zipVar, |
52 |
| - width = 5).grid(row = 1, column = 6) |
53 |
| - |
54 |
| - frame4 = Frame(window) |
55 |
| - frame4.pack() |
56 |
| - Button(frame4, text = "Add", |
57 |
| - command = self.processAdd).grid(row = 1, column = 1) |
58 |
| - btFirst = Button(frame4, text = "First", |
59 |
| - command = self.processFirst).grid(row = 1, column = 2) |
60 |
| - btNext = Button(frame4, text = "Next", |
61 |
| - command = self.processNext).grid(row = 1, column = 3) |
62 |
| - btPrevious = Button(frame4, text = "Previous", command = |
63 |
| - self.processPrevious).grid(row = 1, column = 4) |
64 |
| - btLast = Button(frame4, text = "Last", |
65 |
| - command = self.processLast).grid(row = 1, column = 5) |
66 |
| - |
67 |
| - self.addressList = self.loadAddress() |
68 |
| - self.current = 0 |
69 |
| - |
70 |
| - if len(self.addressList) > 0: |
71 |
| - self.setAddress() |
72 |
| - |
73 |
| - window.mainloop() # Create an event loop |
74 |
| - |
75 |
| - def saveAddress(self): |
76 |
| - outfile = open("address.dat", "wb") |
77 |
| - pickle.dump(self.addressList, outfile) |
78 |
| - tkinter.messagebox.showinfo( |
79 |
| - "Address saved", "A new address is saved") |
80 |
| - outfile.close() |
81 |
| - |
82 |
| - def loadAddress(self): |
83 |
| - if not os.path.isfile("address.dat"): |
84 |
| - return [] # Return an empty list |
85 |
| - |
86 |
| - try: |
87 |
| - infile = open("address.dat", "rb") |
88 |
| - addressList = pickle.load(infile) |
89 |
| - except EOFError: |
90 |
| - addressList = [] |
91 |
| - |
92 |
| - infile.close() |
93 |
| - return addressList |
94 |
| - |
95 |
| - def processAdd(self): |
96 |
| - address = Address(self.nameVar.get(), |
97 |
| - self.streetVar.get(), self.cityVar.get(), |
98 |
| - self.stateVar.get(), self.zipVar.get()) |
99 |
| - self.addressList.append(address) |
100 |
| - self.saveAddress() |
101 |
| - |
102 |
| - def processFirst(self): |
103 |
| - self.current = 0 |
104 |
| - self.setAddress() |
105 |
| - |
106 |
| - def processNext(self): |
107 |
| - if self.current < len(self.addressList) - 1: |
108 |
| - self.current += 1 |
109 |
| - self.setAddress() |
110 |
| - |
111 |
| - def processPrevious(self): |
112 |
| - pass # Left as exercise |
113 |
| - |
114 |
| - def processLast(self): |
115 |
| - pass # Left as exercise |
116 |
| - |
117 |
| - def setAddress(self): |
118 |
| - self.nameVar.set(self.addressList[self.current].name) |
119 |
| - self.streetVar.set(self.addressList[self.current].street) |
120 |
| - self.cityVar.set(self.addressList[self.current].city) |
121 |
| - self.stateVar.set(self.addressList[self.current].state) |
122 |
| - self.zipVar.set(self.addressList[self.current].zip) |
123 |
| - |
124 |
| -AddressBook() # Create GUI |
| 1 | +import pickle |
| 2 | +import os.path |
| 3 | +from tkinter import * # Import tkinter |
| 4 | +import tkinter.messagebox |
| 5 | + |
| 6 | +class Address: |
| 7 | + def __init__(self, name, street, city, state, zip): |
| 8 | + self.name = name |
| 9 | + self.street = street |
| 10 | + self.city = city |
| 11 | + self.state = state |
| 12 | + self.zip = zip |
| 13 | + |
| 14 | +class AddressBook: |
| 15 | + def __init__(self): |
| 16 | + window = Tk() # Create a window |
| 17 | + window.title("AddressBook") # Set title |
| 18 | + |
| 19 | + self.nameVar = StringVar() |
| 20 | + self.streetVar = StringVar() |
| 21 | + self.cityVar = StringVar() |
| 22 | + self.stateVar = StringVar() |
| 23 | + self.zipVar = StringVar() |
| 24 | + |
| 25 | + frame1 = Frame(window) |
| 26 | + frame1.pack() |
| 27 | + Label(frame1, text = "Name").grid(row = 1, |
| 28 | + column = 1, sticky = W) |
| 29 | + Entry(frame1, textvariable = self.nameVar, |
| 30 | + width = 40).grid(row = 1, column = 2) |
| 31 | + |
| 32 | + frame2 = Frame(window) |
| 33 | + frame2.pack() |
| 34 | + Label(frame2, text = "Street").grid(row = 1, |
| 35 | + column = 1, sticky = W) |
| 36 | + Entry(frame2, textvariable = self.streetVar, |
| 37 | + width = 40).grid(row = 1, column = 2) |
| 38 | + |
| 39 | + frame3 = Frame(window) |
| 40 | + frame3.pack() |
| 41 | + Label(frame3, text = "City", width = 5).grid(row = 1, |
| 42 | + column = 1, sticky = W) |
| 43 | + Entry(frame3, |
| 44 | + textvariable = self.cityVar).grid(row = 1, column = 2) |
| 45 | + Label(frame3, text = "State").grid(row = 1, |
| 46 | + column = 3, sticky = W) |
| 47 | + Entry(frame3, textvariable = self.stateVar, |
| 48 | + width = 5).grid(row = 1, column = 4) |
| 49 | + Label(frame3, text = "ZIP").grid(row = 1, |
| 50 | + column = 5, sticky = W) |
| 51 | + Entry(frame3, textvariable = self.zipVar, |
| 52 | + width = 5).grid(row = 1, column = 6) |
| 53 | + |
| 54 | + frame4 = Frame(window) |
| 55 | + frame4.pack() |
| 56 | + Button(frame4, text = "Add", |
| 57 | + command = self.processAdd).grid(row = 1, column = 1) |
| 58 | + btFirst = Button(frame4, text = "First", |
| 59 | + command = self.processFirst).grid(row = 1, column = 2) |
| 60 | + btNext = Button(frame4, text = "Next", |
| 61 | + command = self.processNext).grid(row = 1, column = 3) |
| 62 | + btPrevious = Button(frame4, text = "Previous", command = |
| 63 | + self.processPrevious).grid(row = 1, column = 4) |
| 64 | + btLast = Button(frame4, text = "Last", |
| 65 | + command = self.processLast).grid(row = 1, column = 5) |
| 66 | + |
| 67 | + self.addressList = self.loadAddress() |
| 68 | + self.current = 0 |
| 69 | + |
| 70 | + if len(self.addressList) > 0: |
| 71 | + self.setAddress() |
| 72 | + |
| 73 | + window.mainloop() # Create an event loop |
| 74 | + |
| 75 | + def saveAddress(self): |
| 76 | + outfile = open("address.dat", "wb") |
| 77 | + pickle.dump(self.addressList, outfile) |
| 78 | + tkinter.messagebox.showinfo( |
| 79 | + "Address saved", "A new address is saved") |
| 80 | + outfile.close() |
| 81 | + |
| 82 | + def loadAddress(self): |
| 83 | + if not os.path.isfile("address.dat"): |
| 84 | + return [] # Return an empty list |
| 85 | + |
| 86 | + try: |
| 87 | + infile = open("address.dat", "rb") |
| 88 | + addressList = pickle.load(infile) |
| 89 | + except EOFError: |
| 90 | + addressList = [] |
| 91 | + |
| 92 | + infile.close() |
| 93 | + return addressList |
| 94 | + |
| 95 | + def processAdd(self): |
| 96 | + address = Address(self.nameVar.get(), |
| 97 | + self.streetVar.get(), self.cityVar.get(), |
| 98 | + self.stateVar.get(), self.zipVar.get()) |
| 99 | + self.addressList.append(address) |
| 100 | + self.saveAddress() |
| 101 | + |
| 102 | + def processFirst(self): |
| 103 | + self.current = 0 |
| 104 | + self.setAddress() |
| 105 | + |
| 106 | + def processNext(self): |
| 107 | + if self.current < len(self.addressList) - 1: |
| 108 | + self.current += 1 |
| 109 | + self.setAddress() |
| 110 | + |
| 111 | + def processPrevious(self): |
| 112 | + pass # Left as exercise |
| 113 | + |
| 114 | + def processLast(self): |
| 115 | + pass # Left as exercise |
| 116 | + |
| 117 | + def setAddress(self): |
| 118 | + self.nameVar.set(self.addressList[self.current].name) |
| 119 | + self.streetVar.set(self.addressList[self.current].street) |
| 120 | + self.cityVar.set(self.addressList[self.current].city) |
| 121 | + self.stateVar.set(self.addressList[self.current].state) |
| 122 | + self.zipVar.set(self.addressList[self.current].zip) |
| 123 | + |
| 124 | +AddressBook() # Create GUI |
0 commit comments