|
| 1 | +''' |
| 2 | +Different commands when user clicks different button |
| 3 | +''' |
| 4 | + |
| 5 | + |
| 6 | +from tkinter import messagebox |
| 7 | +import matplotlib.pyplot as plt |
| 8 | + |
| 9 | + |
| 10 | +def default_state(entry_widgets, styles): |
| 11 | + '''Set the entries box and radio-button the default state after adding the value''' |
| 12 | + |
| 13 | + for widget, text in {entry_widgets[0]: 'Items', entry_widgets[1]: 'Values'}.items(): |
| 14 | + widget.delete(0, 'end') |
| 15 | + widget.insert('end', text) |
| 16 | + |
| 17 | + for style, name in styles.items(): # Change the foreground color of entries box |
| 18 | + style.configure(name, foreground='grey') |
| 19 | + |
| 20 | + entry_widgets[2].set(0) |
| 21 | + |
| 22 | + |
| 23 | +def add_command(entry_widgets, _vars, styles): |
| 24 | + '''Store item_name, values and explode in respective variables''' |
| 25 | + |
| 26 | + entry_get = entry_widgets[0].get().strip() |
| 27 | + values_get = entry_widgets[1].get().strip() |
| 28 | + split_value_get = values_get.split('.') |
| 29 | + explode_get = entry_widgets[2].get() |
| 30 | + |
| 31 | + pie_items_var, values_var, explode_var = _vars |
| 32 | + |
| 33 | + if entry_get in ['', 'Items']: |
| 34 | + messagebox.showerror('Invalid Name', 'Provide valid item name') |
| 35 | + |
| 36 | + elif entry_get in _vars[0]: |
| 37 | + messagebox.showerror('Exists', f'{entry_get} already exists in the register') |
| 38 | + |
| 39 | + elif not split_value_get[0].isdigit() and not split_value_get[1].isdigit(): |
| 40 | + messagebox.showerror('Invalid Values', 'Values was expected in number') |
| 41 | + |
| 42 | + elif explode_get not in [1, 2]: |
| 43 | + messagebox.showerror('Invalid Explode', 'You must select Enable or Disable in Explode option') |
| 44 | + |
| 45 | + else: |
| 46 | + pie_items_var.append(entry_get) |
| 47 | + values_var.append(float(values_get)) |
| 48 | + |
| 49 | + if explode_get == 1: |
| 50 | + explode_var.append(0.1) |
| 51 | + |
| 52 | + else: |
| 53 | + explode_var.append(0) |
| 54 | + |
| 55 | + messagebox.showinfo('Added', 'Values are added') |
| 56 | + default_state(entry_widgets, styles) |
| 57 | + |
| 58 | + |
| 59 | +def insert_to_text_widget(text_widgets, _vars): |
| 60 | + '''Inserting data from self.explode, self.pie_items and self.pie_items_values in their own text_widget''' |
| 61 | + |
| 62 | + for widget in text_widgets: |
| 63 | + widget.config(state='normal') |
| 64 | + widget.delete('1.0', 'end') |
| 65 | + |
| 66 | + pie_items, pie_values, pie_explode = _vars |
| 67 | + pie_explode = ['Enabled' if explode else 'Disabled' for explode in pie_explode] |
| 68 | + |
| 69 | + for index, values in enumerate(zip(pie_items, pie_values, pie_explode)): |
| 70 | + item, values, explode = values |
| 71 | + |
| 72 | + text_widgets[0].insert('1.0', f'{item}\n') |
| 73 | + text_widgets[1].insert('1.0', f'{values}\n') |
| 74 | + text_widgets[2].insert('1.0', f'{explode}\n') |
| 75 | + |
| 76 | + for widget in text_widgets: |
| 77 | + widget.config(state='disabled', cursor='arrow') |
| 78 | + |
| 79 | + |
| 80 | +def make_chart(_vars): |
| 81 | + '''Make pie-chart as per the user's data''' |
| 82 | + |
| 83 | + items, values, explode = _vars |
| 84 | + |
| 85 | + if items: |
| 86 | + figure, pie_chart = plt.subplots() |
| 87 | + |
| 88 | + if messagebox.askyesno('Shadow', 'Do you want to show in the pie-char?'): |
| 89 | + pie_chart.pie(values, explode=explode, labels=items, autopct='%1.2f%%', shadow=True, startangle=90) |
| 90 | + |
| 91 | + else: |
| 92 | + pie_chart.pie(values, explode=explode, labels=items, autopct='%1.2f%%', shadow=False, startangle=90) |
| 93 | + |
| 94 | + pie_chart.axis('equal') |
| 95 | + plt.show() |
| 96 | + |
| 97 | + else: |
| 98 | + messagebox.showerror('No data', 'No data were inputed to make pie-charts') |
| 99 | + |
| 100 | + |
| 101 | +def clear(_vars, text_widgets=None, styles=None): |
| 102 | + '''Clear data from the register''' |
| 103 | + |
| 104 | + if _vars[0]: |
| 105 | + if messagebox.askyesno('Clear Register?', 'Do you really want to clear REGISTER?'): |
| 106 | + for _var in _vars: |
| 107 | + _var.clear() |
| 108 | + |
| 109 | + messagebox.showinfo('Cleared', 'All values are cleared from the REGISTER') |
| 110 | + default_state(text_widgets, styles) |
| 111 | + |
| 112 | + else: |
| 113 | + messagebox.showinfo('Clear Register', "No items added yet. How about adding some items?") |
0 commit comments