-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdietGUI.py
More file actions
265 lines (232 loc) · 14 KB
/
dietGUI.py
File metadata and controls
265 lines (232 loc) · 14 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
"""
This module takes allows users to select one of the standardised diets.
When that diet is selected, the amount of protein, carbohydrates, fat, and kilojoules is displayed.
All of this is done within a GUI.
"""
# Program Details
__author__ = "Axel Tracy"
__version__ = "0.1.13"
# Add required import statements
from tkinter import *
from tkinter import ttk
# Initialise Dictionaries with Standardised Diets
normal_diet = {
"protein": 32.5,
"carbohydrates": 60.0,
"fat": 40.86
}
oncology_diet = {
"protein": 35.0,
"carbohydrates": 52.5,
"fat": 37.63
}
cardiology_diet = {
"protein": 32.5,
"carbohydrates": 30.0,
"fat": 26.88
}
diabetes_diet = {
"protein": 20.0,
"carbohydrates": 27.5,
"fat": 27.95
}
kidney_diet = {
"protein": 15.0,
"carbohydrates": 55.0,
"fat": 23.65
}
# Calculate Kjs for each diet, that will be displayed
# The function has protein, carbohydrates, and fat as arguments
def calc_kjs(pr, ca, fa):
"""Calculates kJs of each diet, to diplay."""
# The kJ calculation formula from Programming Task 1
kilojoules_unrounded = (4.18 * (4 * pr + 4 * ca + 9.30 * fa))
# Using the round() function to handle complex float;
# Setting precision 2 decimal places
kilojoules_var = round(kilojoules_unrounded, 2)
return kilojoules_var
# While it was originally hoped ome function could minimise the code below, by differentiating which diet was clicked,
# in the end the priority was to get the task working, and the five functions below became the final version.
# A function to be called when Normal Diet is required via command
def click_normal(*args):
"""Displays Normal Diet data in GUI"""
# Set the DoubleVars
kilojoules.set(f'{calc_kjs(normal_diet["protein"], normal_diet["carbohydrates"], normal_diet["fat"]):.2f}')
protein.set(f'{normal_diet["protein"]:.2f}')
carbohydrates.set(f'{normal_diet["carbohydrates"]:.2f}')
fat.set(f'{normal_diet["fat"]:.2f}')
# Create the labels for the DoubleVars and one StringVar
selected_diet_label_var = ttk.Label(frame, text = "Normal", anchor = "e")
protein_label_var = ttk.Label(frame, width = 12, textvariable = protein, anchor = "e")
carbohydrates_label_var = ttk.Label(frame, width = 12, textvariable = carbohydrates, anchor = "e")
fat_label_var = ttk.Label(frame, width = 12, textvariable = fat, anchor = "e")
kilojoules_label_var = ttk.Label(frame, width = 12, textvariable = kilojoules, anchor = "e")
# Display the label for the DoubleVars and one StringVar
selected_diet_label_var.grid(column = 4, row = 3, sticky = (N, S, E, W))
protein_label_var.grid(column = 4, row = 4, sticky = (N, S, E, W))
carbohydrates_label_var.grid(column = 4, row = 5, sticky = (N, S, E, W))
fat_label_var.grid(column = 4, row = 6, sticky = (N, S, E, W))
kilojoules_label_var.grid(column = 4, row = 7, sticky = (N, S, E, W))
# A function to be called when Oncology Diet is required via command
def click_oncology(*args):
"""Displays Oncology Diet data in GUI"""
# Set the DoubleVars
kilojoules.set(f'{calc_kjs(oncology_diet["protein"], oncology_diet["carbohydrates"], oncology_diet["fat"]):.2f}')
protein.set(f'{oncology_diet["protein"]:.2f}')
carbohydrates.set(f'{oncology_diet["carbohydrates"]:.2f}')
fat.set(f'{oncology_diet["fat"]:.2f}')
# Create the labels for the DoubleVars and one StringVar
selected_diet_label_var = ttk.Label(frame, text = "Oncology", anchor = "e")
protein_label_var = ttk.Label(frame, width = 12, textvariable = protein, anchor = "e")
carbohydrates_label_var = ttk.Label(frame, width = 12, textvariable = carbohydrates, anchor = "e")
fat_label_var = ttk.Label(frame, width = 12, textvariable = fat, anchor = "e")
kilojoules_label_var = ttk.Label(frame, width = 12, textvariable = kilojoules, anchor = "e")
# Display the label for the DoubleVars and one StringVar
selected_diet_label_var.grid(column = 4, row = 3, sticky = (N, S, E, W))
protein_label_var.grid(column = 4, row = 4, sticky = (N, S, E, W))
carbohydrates_label_var.grid(column = 4, row = 5, sticky = (N, S, E, W))
fat_label_var.grid(column = 4, row = 6, sticky = (N, S, E, W))
kilojoules_label_var.grid(column = 4, row = 7, sticky = (N, S, E, W))
# A function to be called when Cardiology Diet is required via command
def click_cardiology(*args):
"""Displays Cardiology Diet data in GUI"""
# Set the DoubleVars
kilojoules.set(f'{calc_kjs(cardiology_diet["protein"], cardiology_diet["carbohydrates"], cardiology_diet["fat"]):.2f}')
protein.set(f'{cardiology_diet["protein"]:.2f}')
carbohydrates.set(f'{cardiology_diet["carbohydrates"]:.2f}')
fat.set(f'{cardiology_diet["fat"]:.2f}')
# Create the labels for the DoubleVars and one StringVar
selected_diet_label_var = ttk.Label(frame, text = "Cardiology", anchor = "e")
protein_label_var = ttk.Label(frame, width = 12, textvariable = protein, anchor = "e")
carbohydrates_label_var = ttk.Label(frame, width = 12, textvariable = carbohydrates, anchor = "e")
fat_label_var = ttk.Label(frame, width = 12, textvariable = fat, anchor = "e")
kilojoules_label_var = ttk.Label(frame, width = 12, textvariable = kilojoules, anchor = "e")
# Display the label for the DoubleVars and one StringVar
selected_diet_label_var.grid(column = 4, row = 3, sticky = (N, S, E, W))
protein_label_var.grid(column = 4, row = 4, sticky = (N, S, E, W))
carbohydrates_label_var.grid(column = 4, row = 5, sticky = (N, S, E, W))
fat_label_var.grid(column = 4, row = 6, sticky = (N, S, E, W))
kilojoules_label_var.grid(column = 4, row = 7, sticky = (N, S, E, W))
# A function to be called when Diabetes Diet is required via command
def click_diabetes(*args):
"""Displays Diabetes Diet data in GUI"""
# Set the DoubleVars
kilojoules.set(f'{calc_kjs(diabetes_diet["protein"], diabetes_diet["carbohydrates"], diabetes_diet["fat"]):.2f}')
protein.set(f'{diabetes_diet["protein"]:.2f}')
carbohydrates.set(f'{diabetes_diet["carbohydrates"]:.2f}')
fat.set(f'{diabetes_diet["fat"]:.2f}')
# Create the labels for the DoubleVars and one StringVar
selected_diet_label_var = ttk.Label(frame, text = "Diabetes", anchor = "e")
protein_label_var = ttk.Label(frame, width = 12, textvariable = protein, anchor = "e")
carbohydrates_label_var = ttk.Label(frame, width = 12, textvariable = carbohydrates, anchor = "e")
fat_label_var = ttk.Label(frame, width = 12, textvariable = fat, anchor = "e")
kilojoules_label_var = ttk.Label(frame, width = 12, textvariable = kilojoules, anchor = "e")
# Display the label for the DoubleVars and one StringVar
selected_diet_label_var.grid(column = 4, row = 3, sticky = (N, S, E, W))
protein_label_var.grid(column = 4, row = 4, sticky = (N, S, E, W))
carbohydrates_label_var.grid(column = 4, row = 5, sticky = (N, S, E, W))
fat_label_var.grid(column = 4, row = 6, sticky = (N, S, E, W))
kilojoules_label_var.grid(column = 4, row = 7, sticky = (N, S, E, W))
# A function to be called when Kidney Diet is required via command
def click_kidney(*args):
"""Displays Kidney Diet data in GUI"""
# Set the DoubleVars
kilojoules.set(f'{calc_kjs(kidney_diet["protein"], kidney_diet["carbohydrates"], kidney_diet["fat"]):.2f}')
protein.set(f'{kidney_diet["protein"]:.2f}')
carbohydrates.set(f'{kidney_diet["carbohydrates"]:.2f}')
fat.set(f'{kidney_diet["fat"]:.2f}')
# Create the labels for the DoubleVars and one StringVar
selected_diet_label_var = ttk.Label(frame, text = "Kidney", anchor = "e")
protein_label_var = ttk.Label(frame, width = 12, textvariable = protein, anchor = "e")
carbohydrates_label_var = ttk.Label(frame, width = 12, textvariable = carbohydrates, anchor = "e")
fat_label_var = ttk.Label(frame, width = 12, textvariable = fat, anchor = "e")
kilojoules_label_var = ttk.Label(frame, width = 12, textvariable = kilojoules, anchor = "e")
# Display the label for the DoubleVars and one StringVar
selected_diet_label_var.grid(column = 4, row = 3, sticky = (N, S, E, W))
protein_label_var.grid(column = 4, row = 4, sticky = (N, S, E, W))
carbohydrates_label_var.grid(column = 4, row = 7, sticky = (N, S, E, W))
fat_label_var.grid(column = 4, row = 6, sticky = (N, S, E, W))
kilojoules_label_var.grid(column = 4, row = 7, sticky = (N, S, E, W))
# Create the root of the hierarchy
root = Tk()
# Set default window size
root.geometry("1000x500")
# Give the GUI window a Title
root.title("Codetown Hospital: Diet Information")
# Create the frame of the window
frame = ttk.Frame(root, padding = (3, 3, 12, 12), borderwidth = 5, relief = "sunken")
# Configure the frame to cover the root window and make it sticky to all sides
frame.grid(column=0, row=0, sticky=(N,S,E,W))
# Two for loops to configure rows and columns of the grid and set each row and coloumn to a weight of 1
# Code to enable resizing, on the cells, rows, and columns
for i in range(1, 8):
frame.rowconfigure(i, weight=1)
for i in range(1, 6):
frame.columnconfigure(i, weight=1)
# Create the labels for static text
selected_diet_label = ttk.Label(frame, text = "Selected Diet:", anchor = "e", borderwidth = 2, relief="ridge", padding = "5")
protein_label = ttk.Label(frame, text = "Protein (g):", anchor = "e", borderwidth = 2, relief="ridge", padding = "5")
carbohydrates_label = ttk.Label(frame, text = "Cabohydrates (g):", anchor = "e", borderwidth = 2, relief="ridge", padding = "5")
fat_label = ttk.Label(frame, text = "Fat (g):", anchor = "e", borderwidth = 2, relief="ridge", padding = "5")
kilojoules_label = ttk.Label(frame, text = "Kilojoules (kJs):", anchor = "e", borderwidth = 2, relief="ridge", padding = "5")
select_diet_label = ttk.Label(frame, text = "Select the Diet Option to Display:", anchor = "center", borderwidth = 2, relief="ridge", padding = "5")
# Display the labels for static text
selected_diet_label.grid(column = 2, row = 3, sticky = (N, S, E, W))
protein_label.grid(column = 2, row = 4, sticky = (N, S, E, W))
carbohydrates_label.grid(column = 2, row = 5, sticky = (N, S, E, W))
fat_label.grid(column = 2, row = 6, sticky = (N, S, E, W))
kilojoules_label.grid(column = 2, row = 7, sticky = (N, S, E, W))
select_diet_label.grid(column = 1, row = 1, columnspan = 5, sticky = ((N, S, E, W)))
# Create the Buttons for each Diet
normal_button = ttk.Button(frame, width = 8, text = "Normal", command = click_normal)
oncology_button = ttk.Button(frame, width = 8, text = "Oncology", command = click_oncology)
cardiology_button = ttk.Button(frame, width = 8, text = "Cardiology", command = click_cardiology)
diabetes_button = ttk.Button(frame, width = 8, text = "Diabetes", command = click_diabetes)
kidney_button = ttk.Button(frame, width = 8, text = "Kidney", command = click_kidney)
# Display Buttons for each Diet
normal_button.grid(column = 1, row = 2, sticky = (N, W, E, S))
oncology_button.grid(column = 2, row = 2, sticky=(N, S, E, W))
cardiology_button.grid(column = 3, row = 2, sticky = (N, S, E, W))
diabetes_button.grid(column = 4, row = 2, sticky = (N, S, E, W))
kidney_button.grid(column = 5, row = 2, sticky = (N, S, E, W))
# Create the DoubleVars that will be set in the functions above
protein = DoubleVar(frame)
carbohydrates = DoubleVar(frame)
fat = DoubleVar(frame)
kilojoules = DoubleVar(frame)
# Code to set padding of the cells and window
for child in frame.winfo_children():
child.grid_configure(padx = 3, pady = 3)
# Code to enable resizing, on the root itself
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
# Code required so GUI waits for events, and script doesn't just start, run, end/close in blink of eye
root.mainloop()
# References:
# * https://www.w3schools.com/python/
# * https://www.geeksforgeeks.org/
# * https://www.geeksforgeeks.org/python-passing-dictionary-as-arguments-to-function/
# * https://www.geeksforgeeks.org/python-passing-dictionary-as-keyword-arguments/
# * https://www.reddit.com/r/Python/comments/jp0x4d/taking_a_dictionary_as_an_argument_is_the_root_of/
# * https://blog.furas.pl/python-tkinter-how-to-set-size-for-empty-row-or-column-in-grid-gb.html"
# * https://stackoverflow.com/questions/75250679/tkinter-making-a-large-number-of-cells-with-grid-while-using-geometry-property
# * https://www.reddit.com/r/Tkinter/comments/19ay94h/why_wont_tkinter_resize_my_image/
# * https://pythonroadmap.com/blog/tkinter-grid-manager-tutorial#spanning-rows-and-columns
# * https://stackoverflow.com/questions/40767893/how-to-center-a-label-in-a-tkinter-colspan-using-grid
# * https://stackoverflow.com/questions/25328787/can-you-pack-multiple-tkinter-widgets-at-a-time-rather-than-packing-them-individ
# * https://www.tutorialspoint.com/how-can-i-resize-the-root-window-in-tkinter
# * https://stackoverflow.com/questions/30923448/what-does-the-00-in-geometry-mean-in-tkinter
# * https://stackoverflow.com/questions/21766890/python-tkinter-frame-and-canvas-will-expand-horizontally-but-not-vertically-w
# * https://stackoverflow.com/questions/7591294/how-to-create-a-self-resizing-grid-of-buttons-in-tkinter
# * https://www.geeksforgeeks.org/how-to-align-text-in-tkinter-label/
# * https://stackoverflow.com/questions/31140590/how-to-line-left-justify-label-and-entry-boxes-in-tkinter-grid
# * https://www.w3schools.com/python/python_dictionaries_access.asp
# * https://stackoverflow.com/questions/45310254/fixed-digits-after-decimal-with-f-strings
# * https://www.geeksforgeeks.org/dynamically-resize-buttons-when-resizing-a-window-using-tkinter/
# * https://stackoverflow.com/questions/68109119/how-can-i-set-default-screen-tkinter-not-full-screen
# * https://chatgpt.com/c/66eae6c5-e980-800c-b5bf-3120f9f97fbe
# ChatGPT Qustion Flow 1: "Why is my tkinter sticky attribute not working? It will stretch E and W for a Button, but not N and S?"
# ChatGPT Qustion Flow 2: "If I try Option 1, where in my file should I place that code?"
# ChatGPT Qustion Flow 3: "That Option didn't work, do you have any other ideas what's occuring?"
# ChatGPT Qustion Flow 4: "Does running this code on a Mac have any effect on the tkinter Buttons?"