-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvehicle_GUI.py
More file actions
202 lines (161 loc) · 7.05 KB
/
vehicle_GUI.py
File metadata and controls
202 lines (161 loc) · 7.05 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#Vehicle_GUI.py
# Similar to Assignment 2 Project 3: Employee Management System Project with a GUI
# Used Vehicle_Inventory_Management_System_GUI
import vehicle
import pickle
import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog
# Global constant for the filename
FILENAME = 'vehicles.dat'
# Initialising variable
dataLabel = ""
# Initialising the GUI
root = tk.Tk()
root.title("Vehicle Management System")
canvas = tk.Canvas(root)
def load_vehicles():
try:
# Open the vehicles.dat file.
input_file = open(FILENAME, 'rb')
# Unpickle the dictionary.
vehicle_dct = pickle.load(input_file)
# Close the idNumber_inventory.dat file.
input_file.close()
except IOError:
# Could not open the file, so create
# an empty dictionary.
vehicle_dct = {}
# Return the dictionary
return vehicle_dct
# The save_vehicles funtion pickles the specified
# object and saves it to the vehicles file.
def save_vehicles(myvehicles):
# Open the file for writing.
output_file = open(FILENAME, 'wb')
# Pickle the dictionary and save it.
pickle.dump(myvehicles, output_file)
# Close the file.
output_file.close()
# The look_up function looks up an item in the
# specified dictionary.
def look_up():
displayResult.delete('1.0', 'end')
disp_string = ""
myvehicles = load_vehicles()
# Get an ID number to look up.
#idNumber = int(input('Enter an ID number: '))
idNumber = int(simpledialog.askstring(title="vehicle model ID Number)",
prompt="Please enter an vehicle model ID number: "))
# Look it up in the dictionary.
if idNumber in myvehicles:
displayResult.insert('1.0', myvehicles.get(idNumber))
else:
messagebox.showinfo("Error", "That ID number couldn't be found.")
# Save the y.
# return vehicle_dct myvehicle dictionary to a file.
save_vehicles(myvehicles)
# The add function adds a new entry into the
# specified dictionary.
def add():
displayResult.delete('1.0', 'end')
myvehicles = load_vehicles()
# Get the vehicle info.
make = simpledialog.askstring(title="vehicle Make)",
prompt="Please enter the Vehicle's make: ")
idNumber = int(simpledialog.askstring(title="Vehicle ID Number)",
prompt="Please enter an Vehicle ID number: "))
mileage = simpledialog.askstring(title="Vehicle Mileage)",
prompt="Please enter the Vehicle Mileage: ")
price = simpledialog.askstring(title="Vehicle Price)",
prompt="Please enter the Vehicle price: ")
# Create an Vehicle object named entry.
entry = vehicle.Automobile(make, idNumber, mileage, price)
# If the name does not exist in the dictionary,
# add it as a key with the entry object as the
# associated value.
if idNumber not in myvehicles:
myvehicles[idNumber] = entry
messagebox.showinfo("Successful", "The entry has been added.")
else:
messagebox.showinfo("Error", "That ID number already exists.")
# Save the myvehicles dictionary to a file.
save_vehicles(myvehicles)
# The change function changes an existing
# entry in the specified dictionary.
def change():
displayResult.delete('1.0', 'end')
myvehicles = load_vehicles()
# Get an ID number to look up.
idNumber = int(simpledialog.askstring(title="vehicle Model ID Number)",
prompt="Please enter an vehicle model ID number: "))
if idNumber in myvehicles:
# Get a new make
make = simpledialog.askstring(title="Vehicle make)",
prompt="Please enter the new vehicle's make: ")
# Select a new mileage.
mileage = simpledialog.askstring(title="Vehicle's mileage)",
prompt="Please enter the new Vehicle's mileage: ")
# Select a new price.
price = simpledialog.askstring(title="Vehicle Price)",
prompt="Please enter the new Vehicle's price: ")
# Create an vehicle object named entry.
entry = vehicle.Automobile(make, idNumber, mileage, price)
# Update the entry.
myvehicles[idNumber] = entry
messagebox.showinfo("Successful", "The information has been updated.")
else:
messagebox.showinfo("Error", "That ID number couldn't be found.")
# Save the myvehicles dictionary to a file.
save_vehicles(myvehicles)
# The delete function deletes an entry from the
# specified dictionary.
def delete():
displayResult.delete('1.0', 'end')
myvehicles = load_vehicles()
# Get an ID number to look up.
idNumber = int(simpledialog.askstring(title="vehicle ID Number)",
prompt="Please enter an vehicleID number: "))
# If the ID number is found, delete the entry.
if idNumber in myvehicles:
del myvehicles[idNumber]
messagebox.showinfo("Successful", "Entry deleted")
else:
messagebox.showinfo("Error", "That ID number couldn't be found.")
# Save the myvehicles dictionary to a file.
save_vehicles(myvehicles)
def display():
disp_string = ""
displayResult.delete('1.0', 'end')
myvehicles = load_vehicles()
for model_id in myvehicles:
disp_string += str(myvehicles[model_id])
displayResult.insert('1.0', myvehicles.get(model_id))
# GUI Layout Initialisation
lblTitle = tk.Label(root, text = "Used Vehicle Inventory System")
lblTitle.configure(bg= 'grey', font=("Calibri", 20, "bold"))
btnLookUp = tk.Button(root, text="Look up vehicle info", command=lambda: look_up())
btnLookUp.configure(font=("Calibri", 10, "bold"))
btnAdd = tk.Button(root, text="Add a new vehicle", command=lambda: add())
btnAdd.configure(font=("Calibri", 10, "bold"))
btnChange = tk.Button(root, text="Update vehicle Info", command=lambda: change())
btnChange.configure(font=("Calibri", 10, "bold"))
btnDelete = tk.Button(root, text="Delete a vehicle", command=lambda: delete())
btnDelete.configure(font=("Calibri", 10, "bold"))
btnDisplay = tk.Button(root, text="Display all vehicles", command=lambda: display())
btnDisplay.configure(font=("Calibri", 10, "bold"))
btnExit = tk.Button(root, text="Quit the program", command=root.destroy)
btnExit.configure(font=("Calibri", 10, "bold"))
displayResult = tk.Text(root, bg = 'light blue', height=30, width=40, wrap=tk.WORD)
displayResult.configure(font=("Calibri", 10, "bold"))
# GUI layout with grid layout manager
lblTitle.grid(row=0,columnspan = 3, column =0,padx=10, pady = 10)
btnLookUp.grid(row=1, column=0, padx=10, pady = 10)
btnAdd.grid(row=2, column=0, padx=10, pady = 10)
btnChange.grid(row=3, column=0, padx=10, pady = 10)
btnDelete.grid(row=4, column=0, padx=10, pady = 10)
btnDisplay.grid(row=5, column=0, padx=10, pady = 10)
btnExit.grid(row=6, column=0, padx=10, pady = 10)
displayResult.grid(row=1, rowspan=6, column=1, padx=10, pady = 10)
root.mainloop()