|
3 | 3 | class EmployeeApp: |
4 | 4 | def __init__(self, master): |
5 | 5 | """ |
6 | | - initialize the main window and configure its layout. |
| 6 | + Initialize the main window and configure its layout. |
7 | 7 | |
8 | | - parameters: |
9 | | - master (Tk): the root Tkinter window instance. |
| 8 | + Parameters: |
| 9 | + master (Tk): The root Tkinter window instance. |
10 | 10 | """ |
11 | | - # initialize the main window |
12 | 11 | self.master = master |
13 | | - master.geometry("1400x750") |
14 | | - master.resizable(0, 0) # make the window non-resizable |
15 | | - master.title("Employee Database") |
16 | | - master.attributes("-topmost", True) # keep the window on top |
17 | | - |
18 | | - # configure grid layout for the master window |
19 | | - master.columnconfigure(2, weight=1) |
20 | | - master.rowconfigure(3, weight=1) |
21 | | - |
22 | | - # define light grey color for borders |
23 | | - light_grey = "#C0C0C0" |
24 | | - |
25 | | - # create and configure frames for various sections of the form |
26 | | - self.create_personal_info_frame(light_grey) |
27 | | - self.create_contact_info_frame(light_grey) |
28 | | - self.create_job_info_frame(light_grey) |
29 | | - self.create_emergency_contact_frame(light_grey) |
30 | | - self.create_buttons_frame(light_grey) |
31 | | - |
32 | | - # configure grid layout for the frames to expand properly |
| 12 | + self.setup_main_window() |
| 13 | + self.create_frames() |
| 14 | + self.create_form_fields() |
| 15 | + self.create_buttons_frame("#C0C0C0") |
33 | 16 | self.configure_frame_grid() |
34 | | - |
35 | | - # adjust window size based on the content |
36 | 17 | self.adjust_window_size() |
37 | | - |
38 | | - # ==================== |
39 | 18 |
|
40 | | - def create_personal_info_frame(self, border_color): |
| 19 | + def setup_main_window(self): |
41 | 20 | """ |
42 | | - create and place the personal information frame. |
| 21 | + Configure the main window properties. |
| 22 | + """ |
| 23 | + self.master.geometry("1400x770") |
| 24 | + self.master.resizable(False, False) |
| 25 | + self.master.title("Employee Database") |
| 26 | + self.master.attributes("-topmost", True) |
| 27 | + self.master.columnconfigure(2, weight=1) |
| 28 | + self.master.rowconfigure(3, weight=1) |
43 | 29 |
|
44 | | - parameters: |
45 | | - border_color (str): the color to use for the frame's border. |
| 30 | + def create_frames(self): |
| 31 | + """ |
| 32 | + Create the frames for the various sections of the form. |
46 | 33 | """ |
47 | | - # create and place the personal information frame |
| 34 | + border_color = "#C0C0C0" |
48 | 35 | self.personal_frame = self.create_frame(border_color, 0) |
49 | | - self.create_label(self.personal_frame, "Employee Information", 0, 0, 2) |
| 36 | + self.contact_frame = self.create_frame(border_color, 1) |
| 37 | + self.job_frame = self.create_frame(border_color, 2) |
| 38 | + self.emergency_contact_frame = self.create_frame(border_color, 3) |
50 | 39 |
|
51 | | - # create and place personal information fields |
| 40 | + def create_form_fields(self): |
| 41 | + """ |
| 42 | + Create form fields within the respective frames. |
| 43 | + """ |
| 44 | + self.create_personal_info_frame() |
| 45 | + self.create_contact_info_frame() |
| 46 | + self.create_job_info_frame() |
| 47 | + self.create_emergency_contact_frame() |
| 48 | + |
| 49 | + def create_personal_info_frame(self): |
| 50 | + """ |
| 51 | + Create and place the personal information frame. |
| 52 | + """ |
| 53 | + self.create_label(self.personal_frame, "Employee Information", 0, 0, 2) |
52 | 54 | self.create_label_entry(self.personal_frame, "First Name:", 1) |
53 | 55 | self.create_label_entry(self.personal_frame, "Last Name:", 2) |
54 | 56 | self.create_label_entry(self.personal_frame, "Date of Birth:", 3) |
55 | 57 | self.create_label_entry(self.personal_frame, "Gender:", 4) |
56 | 58 |
|
57 | | - # ==================== |
58 | | - |
59 | | - def create_contact_info_frame(self, border_color): |
| 59 | + def create_contact_info_frame(self): |
60 | 60 | """ |
61 | | - create and place the contact information frame. |
62 | | -
|
63 | | - parameters: |
64 | | - border_color (str): the color to use for the frame's border. |
| 61 | + Create and place the contact information frame. |
65 | 62 | """ |
66 | | - # create and place the contact information frame |
67 | | - self.contact_frame = self.create_frame(border_color, 1) |
68 | 63 | self.create_label(self.contact_frame, "Contact Information", 0, 0, 2) |
69 | | - |
70 | | - # create and place contact information fields |
71 | 64 | self.create_label_entry(self.contact_frame, "Email Address:", 1) |
72 | 65 | self.create_label_entry(self.contact_frame, "Phone Number:", 2) |
73 | 66 | self.create_label_entry(self.contact_frame, "Home Address:", 3) |
74 | 67 | self.create_label_entry(self.contact_frame, "Zip Code:", 4) |
75 | | - |
76 | | - # ==================== |
77 | 68 |
|
78 | | - def create_job_info_frame(self, border_color): |
| 69 | + def create_job_info_frame(self): |
79 | 70 | """ |
80 | | - create and place the job information frame. |
81 | | -
|
82 | | - parameters: |
83 | | - border_color (str): the color to use for the frame's border. |
| 71 | + Create and place the job information frame. |
84 | 72 | """ |
85 | | - # create and place the job information frame |
86 | | - self.job_frame = self.create_frame(border_color, 2) |
87 | 73 | self.create_label(self.job_frame, "Job Information", 0, 0, 2) |
88 | | - |
89 | | - # create and place job information fields |
90 | 74 | self.create_label_entry(self.job_frame, "Job Title:", 1) |
91 | 75 | self.create_label_entry(self.job_frame, "Department:", 2) |
92 | 76 | self.create_label_entry(self.job_frame, "Hire Date:", 3) |
93 | 77 | self.create_label_entry(self.job_frame, "Salary:", 4) |
94 | 78 |
|
95 | | - # ==================== |
96 | | - |
97 | | - def create_emergency_contact_frame(self, border_color): |
| 79 | + def create_emergency_contact_frame(self): |
98 | 80 | """ |
99 | | - create and place the emergency contact frame. |
100 | | -
|
101 | | - parameters: |
102 | | - border_color (str): the color to use for the frame's border. |
| 81 | + Create and place the emergency contact frame. |
103 | 82 | """ |
104 | | - # create and place the emergency contact frame |
105 | | - self.emergency_contact_frame = self.create_frame(border_color, 3) |
106 | 83 | self.create_label(self.emergency_contact_frame, "Emergency Information", 0, 0, 2) |
107 | | - |
108 | | - # create and place emergency contact fields |
109 | 84 | self.create_label_entry(self.emergency_contact_frame, "First Name:", 1) |
110 | 85 | self.create_label_entry(self.emergency_contact_frame, "Last Name:", 2) |
111 | 86 | self.create_label_entry(self.emergency_contact_frame, "Relationship:", 3) |
112 | 87 | self.create_label_entry(self.emergency_contact_frame, "Phone Number:", 4) |
113 | 88 |
|
114 | | - # ==================== |
115 | | - |
116 | 89 | def create_buttons_frame(self, border_color): |
117 | 90 | """ |
118 | 91 | Create and configure the buttons frame. |
119 | | -
|
120 | | - parameters: |
121 | | - border_color (str): the color to use for the frame's border. |
122 | 92 | """ |
123 | | - # Create a frame for buttons with specified border color |
124 | 93 | self.buttons_frame = Frame(self.master, relief="solid", highlightbackground=border_color, highlightthickness=1) |
125 | | - # Place the frame in the grid at the bottom, right of the emergency_contact_frame |
126 | 94 | self.buttons_frame.grid(row=3, column=1, padx=10, pady=10, sticky="nsew") |
127 | | - # Add labels or buttons to the frame |
128 | 95 | self.create_label(self.buttons_frame, "Options", 0, 0, 1) |
129 | | - # create button within the frame |
130 | | - self.create_button() |
131 | | - |
132 | | - # ==================== |
133 | | - |
134 | | - def create_button(self): |
| 96 | + self.create_buttons() |
| 97 | + |
| 98 | + def create_buttons(self): |
135 | 99 | """ |
136 | | - Create a button and place it in the buttons frame. |
| 100 | + Create buttons and place them in the buttons frame. |
137 | 101 | """ |
138 | | - # create button and add it to buttons frame |
139 | | - add_button = Button(self.buttons_frame, text="Add", width=10, height=2) |
140 | | - add_button.grid(row=1, column=0, padx=5, pady=5) |
141 | | - |
142 | | - delete_button = Button(self.buttons_frame, text="Delete", width=10, height=2) |
143 | | - delete_button.grid(row=2, column=0, padx=5, pady=5) |
144 | | - |
145 | | - view_button = Button(self.buttons_frame, text="View", width=10, height=2) |
146 | | - view_button.grid(row=1, column=1, padx=5, pady=5) |
147 | | - |
148 | | - export_button = Button(self.buttons_frame, text="Export", width=10, height=2) |
149 | | - export_button.grid(row=2, column=1, padx=5, pady=5) |
150 | | - |
151 | | - # ==================== |
152 | | - |
| 102 | + buttons = [("Add", 1, 0), ("Delete", 2, 0)] |
| 103 | + for (text, row, col) in buttons: |
| 104 | + button = Button(self.buttons_frame, text=text, width=10, height=2) |
| 105 | + button.grid(row=row, column=col, padx=5, pady=5) |
| 106 | + |
153 | 107 | def create_frame(self, border_color, row): |
154 | 108 | """ |
155 | | - create a frame with a specified border color and row placement. |
| 109 | + Create a frame with a specified border color and row placement. |
156 | 110 |
|
157 | | - parameters: |
158 | | - border_color (str): the color to use for the frame's border. |
159 | | - row (int): the row index where the frame will be placed in the grid. |
| 111 | + Parameters: |
| 112 | + border_color (str): The color to use for the frame's border. |
| 113 | + row (int): The row index where the frame will be placed in the grid. |
160 | 114 |
|
161 | | - returns: |
162 | | - Frame: the created Tkinter frame. |
| 115 | + Returns: |
| 116 | + Frame: The created Tkinter frame. |
163 | 117 | """ |
164 | | - # create a frame with a specified border color and row placement |
165 | | - frame = Frame(self.master, relief="solid", highlightbackground=border_color, highlightthickness=1, highlightcolor="#C0C0C0") |
| 118 | + frame = Frame(self.master, relief="solid", highlightbackground=border_color, highlightthickness=1, highlightcolor=border_color) |
166 | 119 | frame.grid(row=row, column=0, padx=10, pady=10, sticky="nsew") |
167 | 120 | return frame |
168 | | - |
169 | | - # ==================== |
170 | 121 |
|
171 | 122 | def create_label(self, parent, text, row, column, columnspan=1): |
172 | 123 | """ |
173 | | - create and place a label in a specified parent widget. |
| 124 | + Create and place a label in a specified parent widget. |
174 | 125 |
|
175 | | - parameters: |
176 | | - parent (Frame): the parent widget where the label will be placed. |
177 | | - text (str): the text to display on the label. |
178 | | - row (int): the row index where the label will be placed in the grid. |
179 | | - column (int): the column index where the label will be placed in the grid. |
180 | | - columnspan (int, optional): the number of columns the label should span. default is 1. |
| 126 | + Parameters: |
| 127 | + parent (Frame): The parent widget where the label will be placed. |
| 128 | + text (str): The text to display on the label. |
| 129 | + row (int): The row index where the label will be placed in the grid. |
| 130 | + column (int): The column index where the label will be placed in the grid. |
| 131 | + columnspan (int, optional): The number of columns the label should span. Default is 1. |
181 | 132 | """ |
182 | | - # create and place a label in a specified parent widget |
183 | 133 | label = Label(parent, text=text, font=("Arial", 10)) |
184 | 134 | label.grid(row=row, column=column, columnspan=columnspan, padx=5, pady=5, sticky="w") |
185 | 135 |
|
186 | | - # ==================== |
187 | | - |
188 | 136 | def create_label_entry(self, parent, text, row): |
189 | 137 | """ |
190 | | - create and place a label and entry widget pair. |
| 138 | + Create and place a label and entry widget pair. |
191 | 139 |
|
192 | | - parameters: |
193 | | - parent (Frame): the parent widget where the label and entry will be placed. |
194 | | - text (str): the text to display on the label. |
195 | | - row (int): the row index where the label and entry will be placed in the grid. |
| 140 | + Parameters: |
| 141 | + parent (Frame): The parent widget where the label and entry will be placed. |
| 142 | + text (str): The text to display on the label. |
| 143 | + row (int): The row index where the label and entry will be placed in the grid. |
196 | 144 |
|
197 | | - returns: |
198 | | - Entry: the created Tkinter entry widget. |
| 145 | + Returns: |
| 146 | + Entry: The created Tkinter entry widget. |
199 | 147 | """ |
200 | | - # create and place a label and entry widget pair |
201 | 148 | label = Label(parent, text=text, font=("Arial", 10)) |
202 | 149 | label.grid(row=row, column=0, padx=5, pady=5, sticky="w") |
203 | 150 | entry = Entry(parent, font=("Arial", 10)) |
204 | 151 | entry.grid(row=row, column=1, padx=10, pady=5, sticky="e") |
205 | 152 | return entry |
206 | 153 |
|
207 | | - # ==================== |
208 | | - |
209 | 154 | def configure_frame_grid(self): |
210 | 155 | """ |
211 | | - configure grid layout for all frames to expand properly. |
| 156 | + Configure grid layout for all frames to expand properly. |
212 | 157 | """ |
213 | | - # configure grid layout for all frames to expand properly |
214 | | - self.personal_frame.columnconfigure(1, weight=1) |
215 | | - self.contact_frame.columnconfigure(1, weight=1) |
216 | | - self.job_frame.columnconfigure(1, weight=1) |
217 | | - self.emergency_contact_frame.columnconfigure(1, weight=1) |
| 158 | + frames = [self.personal_frame, self.contact_frame, self.job_frame, self.emergency_contact_frame] |
| 159 | + for frame in frames: |
| 160 | + frame.columnconfigure(1, weight=1) |
218 | 161 |
|
219 | | - # ==================== |
220 | | - |
221 | 162 | def adjust_window_size(self): |
222 | 163 | """ |
223 | | - update the window layout to calculate the total height and adjust the window size. |
| 164 | + Update the window layout to calculate the total height and adjust the window size. |
224 | 165 | """ |
225 | | - # update the window layout to calculate the total height |
226 | 166 | self.master.update_idletasks() |
227 | | - |
228 | | - # calculate the total height needed for all frames |
229 | | - total_height = ( |
230 | | - self.personal_frame.winfo_height() + |
231 | | - self.contact_frame.winfo_height() + |
232 | | - self.job_frame.winfo_height() + |
233 | | - self.emergency_contact_frame.winfo_height() + |
234 | | - 50 # additional padding between frames |
235 | | - ) |
236 | | - |
237 | | - # set the window height to fit all frames |
| 167 | + total_height = sum(frame.winfo_height() for frame in [ |
| 168 | + self.personal_frame, self.contact_frame, self.job_frame, self.emergency_contact_frame |
| 169 | + ]) + 50 # Additional padding between frames |
238 | 170 | self.master.geometry(f"1400x{total_height}") |
239 | 171 |
|
240 | | - # ==================== |
241 | | - |
242 | 172 | def main(): |
243 | 173 | """ |
244 | | - initialize the tkinter window application. |
| 174 | + Initialize the Tkinter window application. |
245 | 175 | """ |
246 | | - # create the main window |
247 | 176 | window = Tk() |
248 | | - # create an instance of the EmployeeApp class |
249 | 177 | app = EmployeeApp(window) |
250 | | - # start the tkinter event loop |
251 | 178 | window.mainloop() |
252 | 179 |
|
253 | | -# ==================== |
254 | | - |
255 | 180 | if __name__ == "__main__": |
256 | | - # run the main function if the script is executed directly |
257 | 181 | main() |
258 | | - |
0 commit comments