Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from ghanteyyy/master
Browse files Browse the repository at this point in the history
Forbid duplicate entry
  • Loading branch information
nsh07 authored Sep 6, 2020
2 parents e33a776 + 35085fa commit 59ed663
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 85 deletions.
9 changes: 7 additions & 2 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'''


import collections
from tkinter import messagebox
import matplotlib.pyplot as plt

Expand Down Expand Up @@ -81,11 +82,15 @@ def make_chart(_vars):
'''Make pie-chart as per the user's data'''

items, values, explode = _vars
count = len(set(collections.Counter(items).values())) # Getting count of items and calculating its length

if items:
if count > 1:
messagebox.showerror('ERROR', 'Some items have same names.')

elif items:
figure, pie_chart = plt.subplots()

if messagebox.askyesno('Shadow', 'Do you want to show in the pie-char?'):
if messagebox.askyesno('Shadow', 'Do you want to show in your pie-chart?'):
pie_chart.pie(values, explode=explode, labels=items, autopct='%1.2f%%', shadow=True, startangle=90)

else:
Expand Down
82 changes: 37 additions & 45 deletions ui_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,64 +78,56 @@ def __init__(self):
self.tips.set_tips(self.add_value_button, 'Add values you have entered in the input fields to the register.')
self.tips.set_tips(self.make_pie_chart_button, 'Generate a pie-chart according to the data provided by you.')

self.item_entry.bind('<FocusIn>', self.bindings)
self.value_entry.bind('<FocusIn>', self.bindings)
self.value_entry.bind('<FocusOut>', lambda event, focus_out=True: self.bindings(event, focus_out))

self.master.after(0, lambda: include.initial_position(self.master, 'Pie Chart Creator'))
self.master.protocol('WM_DELETE_WINDOW', self.exit)
self.master.bind('<Button-1>', self.bindings)
self.master.bind('<Button-1>', self.master_bindings)
self.item_entry.bind('<FocusIn>', self.entry_bindings)
self.value_entry.bind('<FocusIn>', self.entry_bindings)
self.explode_radio_button_enable.bind('<FocusIn>', self.entry_bindings)

self.container_frame.pack()
self.master.mainloop()

def config(self, widget=None, mode=None, text=None, color=None, styles=None):
'''Behaviour for the entry boxes when user gets in and out of them '''

if widget:
widget.delete(0, tix.END)

if mode == 'insert':
widget.insert(tix.END, text)

if widget == self.value_entry and self.item_entry.get() == 'Items':
self.items_style.configure('I.TEntry', foreground='grey')

styles[0].configure(styles[1], foreground=color)
def master_bindings(self, event=None):
'''When user clicks anywhere outside of entry boxes and buttons'''

def bindings(self, event, focus_out=False):
'''When user clicks in and out of the entries widgets'''
widget = event.widget
widgets = [self.item_entry, self.value_entry]
entries_widgets = {self.item_entry: 'Items', self.value_entry: 'Values'}
entries_styles = {self.item_entry: (self.items_style, 'I.TEntry'), self.value_entry: (self.value_style, 'V.TEntry')}

widgets = {self.item_entry: 'Items', self.value_entry: 'Values'}
for wid in entries_widgets:
if not wid.get().strip():
wid.delete(0, tix.END)
wid.insert(tix.END, entries_widgets[wid])
style, style_name = entries_styles[wid]
style.configure(style_name, foreground='grey')

if event.widget == self.item_entry:
styles = (self.items_style, 'I.TEntry')

else:
styles = (self.value_style, 'V.TEntry')

for widget, text in widgets.items():
if widget == event.widget:
if widget.get() == text:
self.config(widget, 'delete', text, 'black', styles)

else:
if not widget.get():
self.config(widget, 'insert', text, 'grey', styles)
if widget not in widgets:
self.master.focus()

if focus_out:
if not widget.get():
self.config(widget, 'insert', text, 'grey', styles)
def entry_bindings(self, event=None):
'''When user clicks in or out of the entries widget'''

if event.widget not in widgets:
_styles = [(self.items_style, 'I.TEntry'), (self.value_style, 'V.TEntry')]
widget = event.widget
entries_widgets = {self.item_entry: 'Items', self.value_entry: 'Values'}
entries_styles = {self.item_entry: (self.items_style, 'I.TEntry'), self.value_entry: (self.value_style, 'V.TEntry')}

for index, widget in enumerate(widgets.items()):
wid, text = widget
if widget in entries_widgets:
if widget.get().strip() == entries_widgets[widget]:
widget.delete(0, tix.END)
style, style_name = entries_styles[widget]
style.configure(style_name, foreground='black')

if wid.get().strip() == text:
self.config(color='grey', styles=_styles[index])
entries_widgets.pop(widget)
entries_styles.pop(widget)

self.master.focus()
for wid in entries_widgets:
if not wid.get().strip():
wid.delete(0, tix.END)
wid.insert(tix.END, entries_widgets[wid])
style, style_name = entries_styles[wid]
style.configure(style_name, foreground='grey')

def exit(self):
'''When user clicks to X button in the title bar'''
Expand Down
49 changes: 11 additions & 38 deletions ui_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,22 @@ def __init__(self, master, _vars):

self.explode_frame = tk.Frame(self.container_frame)
self.explode_label = tk.Label(self.explode_frame, text='EXPLODE')
self.explode_text_widget = tk.Text(self.explode_frame, width=12, height=15, state='disabled', cursor='arrow')
self.explode_text_widget_frame = tk.Frame(self.explode_frame)
self.explode_text_widget = tk.Text(self.explode_text_widget_frame, width=12, height=15, state='disabled', cursor='arrow')
self.explode_label.pack()
self.explode_text_widget.pack()
self.explode_text_widget.pack(side=tk.LEFT)
self.explode_text_widget_frame.pack()
self.explode_frame.pack(side=tk.LEFT)

self.pie_text_widgets = [self.items_text_widget, self.value_text_widget, self.explode_text_widget]

self.scrollbar = tk.Scrollbar(self.container_frame, orient="vertical", command=self.multiple_yview)
self.container_frame.pack(ipadx=9)
self.scrollbar = tk.Scrollbar(self.explode_text_widget_frame, orient="vertical", command=self.multiple_yview)
self.scrollbar.pack(side=tk.LEFT, fill='y')

for widgets in self.pie_text_widgets:
widgets.config(yscrollcommand=self.scrollbar.set)

self.container_frame.pack(padx=1)

self.var = tk.IntVar()
self.enable_editing_button = ttk.Checkbutton(self.toplevel, text='Enable Editing Mode', variable=self.var, cursor='hand2', command=self.enable_editing_command)
Expand All @@ -56,7 +63,6 @@ def __init__(self, master, _vars):
self.tips.set_tips(self.value_text_widget, 'List of the values of each items')
self.tips.set_tips(self.explode_text_widget, 'List of enabled or disabled explode data of each items')

self.toplevel.after(0, self.show_scrollbar)
self.toplevel.protocol('WM_DELETE_WINDOW', self.top_level_exit)
self.toplevel.after(0, lambda: include.initial_position(self.toplevel, 'Register'))
self.toplevel.after(0, lambda: commands.insert_to_text_widget(self.pie_text_widgets, _vars))
Expand Down Expand Up @@ -90,39 +96,6 @@ def top_level_exit(self):
self.enable_editing_command()
messagebox.showinfo('Edit Mode', 'Editing Mode is Enabled')

def show_scrollbar(self):
'''Show self.scrollbar when the character in the text is more than the height of the text widget'''

try:
if self.pie_text_widgets[0].cget('height') < int(self.pie_text_widgets[0].index('end-1c').split('.')[0]):
self.scrollbar.pack(side=tk.LEFT, fill='y')

for widgets in self.pie_text_widgets:
widgets.config(yscrollcommand=self.scrollbar.set)

self.master.after(100, self.hide_scrollbar)

else:
self.master.after(100, self.show_scrollbar)

except tk.TclError:
pass

def hide_scrollbar(self):
'''Hide self.scrollbar when the character in the text is less than the height of the text widget'''

try:
if self.pie_text_widgets[0].cget('height') >= int(self.pie_text_widgets[0].index('end-1c').split('.')[0]):
self.scrollbar.pack_forget()

for widgets in self.pie_text_widgets:
widgets.config(yscrollcommand=None)

self.master.after(100, self.show_scrollbar)

except tk.TclError:
pass

def enable_editing_command(self):
'''When user clicks the enable editing checkbutton'''

Expand Down

0 comments on commit 59ed663

Please sign in to comment.