|
| 1 | + |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import tkinter as tk |
| 4 | + |
| 5 | + |
| 6 | +class Check: # Simple class to check if a value is float, yes etc. |
| 7 | + def __init__(self, string): |
| 8 | + self.string = string |
| 9 | + |
| 10 | + def isyes(self): |
| 11 | + if self.string.title() == "Y" or self.string.title() == "Yes": |
| 12 | + return True |
| 13 | + else: |
| 14 | + return False |
| 15 | + |
| 16 | + def isfloat(self): |
| 17 | + if self.string.replace(".", "", 1).isdigit(): |
| 18 | + return True |
| 19 | + else: |
| 20 | + return False |
| 21 | + |
| 22 | + |
| 23 | +window = tk.Tk() |
| 24 | + |
| 25 | +pcc_logo = tk.PhotoImage(file="PCC_Logo.png") |
| 26 | +pcc_logo_lbl = tk.Label(window, image=pcc_logo) |
| 27 | +pcc_logo_lbl.grid(column=1, columnspan=3) |
| 28 | + |
| 29 | +pie_items = [] |
| 30 | +pie_items_percentage = [] |
| 31 | +explode = [] |
| 32 | + |
| 33 | + |
| 34 | +def append(): |
| 35 | + def exit_not_appended(): |
| 36 | + not_appended.destroy() |
| 37 | + |
| 38 | + if Check.isfloat(Check(percentage_entry.get())): |
| 39 | + item = str(item_entry.get()) |
| 40 | + pie_items.append(item.title()) |
| 41 | + percentage = float(percentage_entry.get()) |
| 42 | + pie_items_percentage.append(float(percentage).__round__(2)) |
| 43 | + explode_ = explode_entry.get() |
| 44 | + if Check.isyes(Check(explode_)): |
| 45 | + explode.append(0.1) |
| 46 | + else: |
| 47 | + explode.append(0) |
| 48 | + |
| 49 | + item_entry.delete(0, tk.END) |
| 50 | + percentage_entry.delete(0, tk.END) |
| 51 | + explode_entry.delete(0, tk.END) |
| 52 | + |
| 53 | + else: |
| 54 | + not_appended = tk.Tk() |
| 55 | + not_appended_label = tk.Label( |
| 56 | + master=not_appended, |
| 57 | + text=f"Values not added to register\nbecause {percentage_entry.get()} is not a\nvalid decimal number.\nPress any key to exit this\nwarning menu." |
| 58 | + ) |
| 59 | + not_appended_label.bind('<KeyPress>', exit_not_appended) |
| 60 | + not_appended_label.pack() |
| 61 | + |
| 62 | + |
| 63 | +def show_register(): |
| 64 | + copy_pie_items = pie_items |
| 65 | + copy_pie_items_percentage = pie_items_percentage |
| 66 | + copy_explode = explode |
| 67 | + register_window = tk.Tk() |
| 68 | + |
| 69 | + def exit_register(): |
| 70 | + register_window.destroy() |
| 71 | + |
| 72 | + a, b = 0, 0 |
| 73 | + if len(pie_items) != 0: |
| 74 | + added_lbl_1 = tk.Label(master=register_window, text="Name of item") |
| 75 | + added_lbl_2 = tk.Label(master=register_window, text="Percentage") |
| 76 | + added_lbl_3 = tk.Label(master=register_window, text="Emphasis") |
| 77 | + added_lbl_1.grid(row=0, column=1, sticky="W", pady=10) |
| 78 | + added_lbl_2.grid(row=0, column=2, padx=50, pady=10) |
| 79 | + added_lbl_3.grid(row=0, column=3, sticky="E", pady=10) |
| 80 | + |
| 81 | + for pie_item in copy_pie_items: |
| 82 | + appended_lbl1 = tk.Label(master=register_window, text=f"{pie_item.title()}") |
| 83 | + appended_lbl1.grid(row=a + 1, column=1, sticky="W") |
| 84 | + appended_lbl2 = tk.Label(master=register_window, text=f"{copy_pie_items_percentage[a]}") |
| 85 | + appended_lbl2.grid(row=a + 1, column=2) |
| 86 | + if copy_explode[b] == 0.1: |
| 87 | + _explode_ = "Enabled" |
| 88 | + else: |
| 89 | + _explode_ = "Disabled" |
| 90 | + appended_lbl3 = tk.Label(master=register_window, text=f"{_explode_}") |
| 91 | + appended_lbl3.grid(row=a + 1, column=3, sticky="E") |
| 92 | + |
| 93 | + a += 1 |
| 94 | + b += 1 |
| 95 | + |
| 96 | + else: |
| 97 | + added_lbl_1 = tk.Label(master=register_window, text="No items added yet. Maybe add some items?") |
| 98 | + added_lbl_1.grid(row=0, column=1, pady=10) |
| 99 | + |
| 100 | + exit_register_btn = tk.Button( |
| 101 | + master=register_window, |
| 102 | + text="Back to Pie Chart Creator", |
| 103 | + command=exit_register, |
| 104 | + ) |
| 105 | + clear_register_btn = tk.Button( |
| 106 | + master=register_window, |
| 107 | + text="Clear Register", |
| 108 | + command=clear |
| 109 | + ) |
| 110 | + exit_register_btn.grid(row=a + 1, column=1, columnspan=2, sticky="WE", padx=1, pady=1) |
| 111 | + clear_register_btn.grid(row=a + 1, column=3, sticky="WE", padx=1, pady=1) |
| 112 | + |
| 113 | + |
| 114 | +def make_chart(): |
| 115 | + figure, pie_chart = plt.subplots() |
| 116 | + pie_chart.pie(pie_items_percentage, explode=explode, labels=pie_items, autopct='%1.2f%%', shadow=True, |
| 117 | + startangle=90) |
| 118 | + pie_chart.axis('equal') |
| 119 | + plt.show() |
| 120 | + |
| 121 | + |
| 122 | +def _exit_(): |
| 123 | + def finalise_exit(): |
| 124 | + exit_window.destroy() |
| 125 | + window.destroy() |
| 126 | + |
| 127 | + def cancel_exit(): |
| 128 | + exit_window.destroy() |
| 129 | + |
| 130 | + exit_window = tk.Tk() |
| 131 | + exit_warning_lbl = tk.Label(master=exit_window, text="Do you really want to exit?\nAll registered values will be lost") |
| 132 | + exit_yes_btn = tk.Button(master=exit_window, text="Yes", command=finalise_exit) |
| 133 | + exit_no_btn = tk.Button(master=exit_window, text="No", command=cancel_exit) |
| 134 | + exit_warning_lbl.grid(row=1, column=1, columnspan=2, rowspan=2) |
| 135 | + exit_yes_btn.grid(row=3, column=1, sticky="WE", padx=1, pady=1) |
| 136 | + exit_no_btn.grid(row=3, column=2, sticky="WE", padx=1, pady=1) |
| 137 | + |
| 138 | + |
| 139 | +def clear(): |
| 140 | + clear_warning = tk.Tk() |
| 141 | + |
| 142 | + def finalise_clear(): |
| 143 | + len_pie_items = len(pie_items) |
| 144 | + for _ in range(len_pie_items): |
| 145 | + del pie_items[0] |
| 146 | + del pie_items_percentage[0] |
| 147 | + del explode[0] |
| 148 | + clear_warning.destroy() |
| 149 | + |
| 150 | + def back_to_pcc(): |
| 151 | + clear_warning.destroy() |
| 152 | + |
| 153 | + if len(pie_items) != 0: |
| 154 | + clear_warning_lbl = tk.Label( |
| 155 | + master=clear_warning, |
| 156 | + text="If you press the clear register button, all the\nvalues in the item register will be cleared.\nIt is advised to have a look at the register before clearing it.\nThis cannot be undone.", |
| 157 | + ) |
| 158 | + clear_warning_lbl.grid(row=1, column=1, columnspan=2, pady=10) |
| 159 | + finalise_clear_btn = tk.Button(master=clear_warning, text="Clear Register", command=finalise_clear) |
| 160 | + finalise_clear_btn.grid(row=2, column=1, columnspan=2, sticky="WE", padx=1, pady=1) |
| 161 | + back_to_pcc_button = tk.Button(master=clear_warning, text="Back to Pie Chart Creator", command=back_to_pcc) |
| 162 | + back_to_pcc_button.grid(row=3, column=1, sticky="WE", padx=1, pady=1) |
| 163 | + view_register = tk.Button(master=clear_warning, text="View Register", command=show_register) |
| 164 | + view_register.grid(row=3, column=2, sticky="WE", padx=1, pady=1) |
| 165 | + |
| 166 | + else: |
| 167 | + added_lbl_1 = tk.Label(master=clear_warning, text="No items added yet. Maybe add some items?") |
| 168 | + added_lbl_1.grid(row=0, column=1, pady=10) |
| 169 | + exit_clear_warning = tk.Button( |
| 170 | + master=clear_warning, |
| 171 | + text="Back to Pie Chart Creator", |
| 172 | + command=back_to_pcc, |
| 173 | + ) |
| 174 | + exit_clear_warning.grid(row=1, column=1, sticky="WE", padx=1, pady=1) |
| 175 | + |
| 176 | + |
| 177 | +item_entry_lbl = tk.Label(text="Name of item:") |
| 178 | +item_entry = tk.Entry() |
| 179 | + |
| 180 | +item_entry_lbl.grid(row=1, column=1, sticky="W") |
| 181 | +item_entry.grid(row=1, column=2, columnspan=2, sticky="WE") |
| 182 | + |
| 183 | +percentage_entry_lbl = tk.Label(text="Percentage:") |
| 184 | +percentage_entry = tk.Entry() |
| 185 | + |
| 186 | +percentage_entry_lbl.grid(row=2, column=1, sticky="W") |
| 187 | +percentage_entry.grid(row=2, column=2, columnspan=2, sticky="WE") |
| 188 | + |
| 189 | +explode_entry_lbl = tk.Label(text="Enable emphasis(Y/N):") |
| 190 | +explode_entry = tk.Entry() |
| 191 | + |
| 192 | +explode_entry_lbl.grid(row=3, column=1, sticky="W") |
| 193 | +explode_entry.grid(row=3, column=2, columnspan=2, sticky="WE") |
| 194 | + |
| 195 | +append_btn = tk.Button( |
| 196 | + text="Add values to register", |
| 197 | + command=append |
| 198 | +) |
| 199 | +make_chart_btn = tk.Button( |
| 200 | + text="Make chart with registered values", |
| 201 | + command=make_chart |
| 202 | +) |
| 203 | +clear_btn = tk.Button( |
| 204 | + text="Clear Register", |
| 205 | + command=clear |
| 206 | +) |
| 207 | +exit_btn = tk.Button( |
| 208 | + text="Exit Pie Chart Creator", |
| 209 | + command=_exit_ |
| 210 | +) |
| 211 | +show_register_btn = tk.Button( |
| 212 | + text="View register", |
| 213 | + command=show_register |
| 214 | +) |
| 215 | + |
| 216 | +append_btn.grid(row=4, column=1, sticky="WE", padx=1, pady=1) |
| 217 | +show_register_btn.grid(row=4, column=2, columnspan=2, sticky="WE", padx=1, pady=1) |
| 218 | +clear_btn.grid(row=5, column=1, sticky="WE", padx=1, pady=1) |
| 219 | +exit_btn.grid(row=5, column=2, columnspan=2, sticky="WE", padx=1, pady=1) |
| 220 | +make_chart_btn.grid(row=6, column=1, columnspan=3, sticky="WE", padx=1, pady=1) |
| 221 | + |
| 222 | +window.mainloop() |
0 commit comments