|
| 1 | +''' |
| 2 | +The main GUI window. |
| 3 | +''' |
| 4 | + |
| 5 | +import tkinter as tk |
| 6 | +import tkinter.ttk as ttk |
| 7 | +import tkinter.tix as tix |
| 8 | +from tkinter import messagebox |
| 9 | +import tips |
| 10 | +import link |
| 11 | +import ui_view |
| 12 | +import include |
| 13 | +import commands |
| 14 | + |
| 15 | + |
| 16 | +class UI: |
| 17 | + def __init__(self): |
| 18 | + self.explode, self.pie_items, self.pie_items_percentage = [], [], [] |
| 19 | + |
| 20 | + self.master = tix.Tk() |
| 21 | + self.container_frame = tix.Frame(self.master, bg='white') |
| 22 | + self.tips = tips.Tips(self.master) |
| 23 | + |
| 24 | + self.image_frame = tix.Frame(self.container_frame) |
| 25 | + self.image_obj = tix.PhotoImage(file=include.resource_path('included_files\\PCC_Logo.png')) |
| 26 | + self.image_label = tix.Label(self.image_frame, image=self.image_obj, cursor='hand2') |
| 27 | + self.image_label.bind('<Button-1>', lambda e: link.open_link(self.master)) |
| 28 | + self.image_label.pack() |
| 29 | + self.image_frame.pack() |
| 30 | + |
| 31 | + self.entries_frame = tix.Frame(self.container_frame, bg='white') |
| 32 | + self.items_style, self.value_style = ttk.Style(), ttk.Style() |
| 33 | + self.items_style.configure('I.TEntry', foreground='grey') |
| 34 | + self.value_style.configure('V.TEntry', foreground='grey') |
| 35 | + self.item_entry = ttk.Entry(self.entries_frame, justify=tix.CENTER, width=35, style='I.TEntry') |
| 36 | + self.value_entry = ttk.Entry(self.entries_frame, justify=tix.CENTER, width=35, style='V.TEntry') |
| 37 | + |
| 38 | + self.radio_button_style = ttk.Style() |
| 39 | + self.radio_button_style.configure('R.TRadiobutton', background='white') |
| 40 | + self.explode_frame = tk.LabelFrame(self.container_frame, text='Explode', bg='white') |
| 41 | + self.radio_button_var = tix.IntVar() |
| 42 | + self.explode_radio_button_enable = ttk.Radiobutton(self.explode_frame, text='Enable', variable=self.radio_button_var, value=1, cursor='hand2', style='R.TRadiobutton') |
| 43 | + self.explode_radio_button_disable = ttk.Radiobutton(self.explode_frame, text='Disable', variable=self.radio_button_var, value=2, cursor='hand2', style='R.TRadiobutton') |
| 44 | + self.explode_radio_button_enable.pack(side=tix.LEFT, ipadx=15, ipady=5) |
| 45 | + self.explode_radio_button_disable.pack(side=tix.LEFT) |
| 46 | + |
| 47 | + self.item_entry.pack(ipady=2, pady=5) |
| 48 | + self.value_entry.pack(ipady=2, pady=5) |
| 49 | + self.entries_frame.pack(pady=5) |
| 50 | + self.explode_frame.pack(ipady=2, pady=5) |
| 51 | + |
| 52 | + for widget, text in {self.item_entry: 'Items', self.value_entry: 'Values'}.items(): |
| 53 | + widget.insert(tix.END, text) |
| 54 | + |
| 55 | + self.styles_list = {self.items_style: 'I.TEntry', self.value_style: 'V.TEntry'} |
| 56 | + self.pie_vars = (self.pie_items, self.pie_items_percentage, self.explode) |
| 57 | + self.pie_entries = (self.item_entry, self.value_entry, self.radio_button_var) |
| 58 | + |
| 59 | + self.buttons_frame = tix.Frame(self.container_frame, bg='white') |
| 60 | + self.add_value_button = tix.Button(self.buttons_frame, text='Add Values', bg='white', activebackground='white', cursor='hand2', relief=tix.GROOVE, command=lambda: commands.add_command(self.pie_entries, self.pie_vars, self.styles_list)) |
| 61 | + self.view_button = tix.Button(self.buttons_frame, text='View Register', bg='white', activebackground='white', cursor='hand2', relief=tix.GROOVE, command=lambda: ui_view.UI(self.master, self.pie_vars)) |
| 62 | + self.clear_button = tix.Button(self.buttons_frame, text='Clear Register', bg='white', activebackground='white', cursor='hand2', relief=tix.GROOVE, command=lambda: commands.clear(self.pie_vars, self.pie_entries, self.styles_list)) |
| 63 | + self.make_pie_chart_button = tix.Button(self.buttons_frame, text='Make Pie-Chart', bg='white', activebackground='white', cursor='hand2', relief=tix.GROOVE, command=lambda: commands.make_chart(self.pie_vars)) |
| 64 | + |
| 65 | + self.add_value_button.pack(ipadx=71, ipady=2, pady=3) |
| 66 | + self.view_button.pack(ipadx=65, ipady=2, pady=3) |
| 67 | + self.clear_button.pack(ipadx=64, ipady=2, pady=3) |
| 68 | + self.make_pie_chart_button.pack(ipadx=59, ipady=2, pady=3) |
| 69 | + |
| 70 | + self.buttons_frame.pack() |
| 71 | + |
| 72 | + self.tips.set_tips(self.item_entry, 'Input desire name for your item.') |
| 73 | + self.tips.set_tips(self.clear_button, 'Clear ALL VALUES from the register.') |
| 74 | + self.tips.set_tips(self.view_button, 'View and Edit items stored in register.') |
| 75 | + self.tips.set_tips(self.image_label, 'http://github.com/NMrocks/Pie-Chart-Creator') |
| 76 | + self.tips.set_tips(self.value_entry, 'Input values for the name of your item.') |
| 77 | + self.tips.set_tips(self.explode_frame, 'Explode is spliting some part of pie-chart if Enabled') |
| 78 | + self.tips.set_tips(self.add_value_button, 'Add values you have entered in the input fields to the register.') |
| 79 | + self.tips.set_tips(self.make_pie_chart_button, 'Generate a pie-chart according to the data provided by you.') |
| 80 | + |
| 81 | + self.item_entry.bind('<FocusIn>', self.bindings) |
| 82 | + self.value_entry.bind('<FocusIn>', self.bindings) |
| 83 | + self.value_entry.bind('<FocusOut>', lambda event, focus_out=True: self.bindings(event, focus_out)) |
| 84 | + |
| 85 | + self.master.after(0, lambda: include.initial_position(self.master, 'Pie Chart Creator')) |
| 86 | + self.master.protocol('WM_DELETE_WINDOW', self.exit) |
| 87 | + self.master.bind('<Button-1>', self.bindings) |
| 88 | + self.container_frame.pack() |
| 89 | + self.master.mainloop() |
| 90 | + |
| 91 | + def config(self, widget=None, mode=None, text=None, color=None, styles=None): |
| 92 | + '''Behaviour for the entry boxes when user gets in and out of them ''' |
| 93 | + |
| 94 | + if widget: |
| 95 | + widget.delete(0, tix.END) |
| 96 | + |
| 97 | + if mode == 'insert': |
| 98 | + widget.insert(tix.END, text) |
| 99 | + |
| 100 | + if widget == self.value_entry and self.item_entry.get() == 'Items': |
| 101 | + self.items_style.configure('I.TEntry', foreground='grey') |
| 102 | + |
| 103 | + styles[0].configure(styles[1], foreground=color) |
| 104 | + |
| 105 | + def bindings(self, event, focus_out=False): |
| 106 | + '''When user clicks in and out of the entries widgets''' |
| 107 | + |
| 108 | + widgets = {self.item_entry: 'Items', self.value_entry: 'Values'} |
| 109 | + |
| 110 | + if event.widget == self.item_entry: |
| 111 | + styles = (self.items_style, 'I.TEntry') |
| 112 | + |
| 113 | + else: |
| 114 | + styles = (self.value_style, 'V.TEntry') |
| 115 | + |
| 116 | + for widget, text in widgets.items(): |
| 117 | + if widget == event.widget: |
| 118 | + if widget.get() == text: |
| 119 | + self.config(widget, 'delete', text, 'black', styles) |
| 120 | + |
| 121 | + else: |
| 122 | + if not widget.get(): |
| 123 | + self.config(widget, 'insert', text, 'grey', styles) |
| 124 | + |
| 125 | + if focus_out: |
| 126 | + if not widget.get(): |
| 127 | + self.config(widget, 'insert', text, 'grey', styles) |
| 128 | + |
| 129 | + if event.widget not in widgets: |
| 130 | + _styles = [(self.items_style, 'I.TEntry'), (self.value_style, 'V.TEntry')] |
| 131 | + |
| 132 | + for index, widget in enumerate(widgets.items()): |
| 133 | + wid, text = widget |
| 134 | + |
| 135 | + if wid.get().strip() == text: |
| 136 | + self.config(color='grey', styles=_styles[index]) |
| 137 | + |
| 138 | + self.master.focus() |
| 139 | + |
| 140 | + def exit(self): |
| 141 | + '''When user clicks to X button in the title bar''' |
| 142 | + |
| 143 | + if messagebox.askyesno('Exit', 'Do you really want to exit?', parent=self.master): |
| 144 | + self.master.destroy() |
0 commit comments