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

Commit 35085fa

Browse files
authored
Fixed naming duplicated error
1 parent 9adc32d commit 35085fa

File tree

4 files changed

+57
-87
lines changed

4 files changed

+57
-87
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
<img src="included_files/main.gif">
44

5-
A Python program which creates a pie chart using [MatPlotLib](https://github.com/matplotlib/matplotlib) and has a GUI created using [Tkinter](https://en.wikipedia.org/wiki/Tkinter). Feel free to copy and use my code anywhere, just don't forget to credit me!
5+
A Python program which creates a pie chart using [MatPlotLib](https://github.com/matplotlib/matplotlib) and has a GUI created using [Tkinter](https://en.wikipedia.org/wiki/Tkinter)
66

7-
I would like to thank [Ghanteyyy](http://github.com/ghanteyyy) for contributing to this project. This program wouldn't have been what it is today without his efforts.
7+
I would like to thank [Ghanteyyy](http://github.com/ghanteyyy) for contributing to this project. This program wouldn't have been what it is today without his efforts.

commands.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'''
44

55

6+
import collections
67
from tkinter import messagebox
78
import matplotlib.pyplot as plt
89

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

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

85-
if items:
87+
if count > 1:
88+
messagebox.showerror('ERROR', 'Some items have same names.')
89+
90+
elif items:
8691
figure, pie_chart = plt.subplots()
8792

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

9196
else:

ui_main.py

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -78,64 +78,56 @@ def __init__(self):
7878
self.tips.set_tips(self.add_value_button, 'Add values you have entered in the input fields to the register.')
7979
self.tips.set_tips(self.make_pie_chart_button, 'Generate a pie-chart according to the data provided by you.')
8080

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-
8581
self.master.after(0, lambda: include.initial_position(self.master, 'Pie Chart Creator'))
8682
self.master.protocol('WM_DELETE_WINDOW', self.exit)
87-
self.master.bind('<Button-1>', self.bindings)
83+
self.master.bind('<Button-1>', self.master_bindings)
84+
self.item_entry.bind('<FocusIn>', self.entry_bindings)
85+
self.value_entry.bind('<FocusIn>', self.entry_bindings)
86+
self.explode_radio_button_enable.bind('<FocusIn>', self.entry_bindings)
87+
8888
self.container_frame.pack()
8989
self.master.mainloop()
9090

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)
91+
def master_bindings(self, event=None):
92+
'''When user clicks anywhere outside of entry boxes and buttons'''
10493

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

108-
widgets = {self.item_entry: 'Items', self.value_entry: 'Values'}
99+
for wid in entries_widgets:
100+
if not wid.get().strip():
101+
wid.delete(0, tix.END)
102+
wid.insert(tix.END, entries_widgets[wid])
103+
style, style_name = entries_styles[wid]
104+
style.configure(style_name, foreground='grey')
109105

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)
106+
if widget not in widgets:
107+
self.master.focus()
124108

125-
if focus_out:
126-
if not widget.get():
127-
self.config(widget, 'insert', text, 'grey', styles)
109+
def entry_bindings(self, event=None):
110+
'''When user clicks in or out of the entries widget'''
128111

129-
if event.widget not in widgets:
130-
_styles = [(self.items_style, 'I.TEntry'), (self.value_style, 'V.TEntry')]
112+
widget = event.widget
113+
entries_widgets = {self.item_entry: 'Items', self.value_entry: 'Values'}
114+
entries_styles = {self.item_entry: (self.items_style, 'I.TEntry'), self.value_entry: (self.value_style, 'V.TEntry')}
131115

132-
for index, widget in enumerate(widgets.items()):
133-
wid, text = widget
116+
if widget in entries_widgets:
117+
if widget.get().strip() == entries_widgets[widget]:
118+
widget.delete(0, tix.END)
119+
style, style_name = entries_styles[widget]
120+
style.configure(style_name, foreground='black')
134121

135-
if wid.get().strip() == text:
136-
self.config(color='grey', styles=_styles[index])
122+
entries_widgets.pop(widget)
123+
entries_styles.pop(widget)
137124

138-
self.master.focus()
125+
for wid in entries_widgets:
126+
if not wid.get().strip():
127+
wid.delete(0, tix.END)
128+
wid.insert(tix.END, entries_widgets[wid])
129+
style, style_name = entries_styles[wid]
130+
style.configure(style_name, foreground='grey')
139131

140132
def exit(self):
141133
'''When user clicks to X button in the title bar'''

ui_view.py

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@ def __init__(self, master, _vars):
3737

3838
self.explode_frame = tk.Frame(self.container_frame)
3939
self.explode_label = tk.Label(self.explode_frame, text='EXPLODE')
40-
self.explode_text_widget = tk.Text(self.explode_frame, width=12, height=15, state='disabled', cursor='arrow')
40+
self.explode_text_widget_frame = tk.Frame(self.explode_frame)
41+
self.explode_text_widget = tk.Text(self.explode_text_widget_frame, width=12, height=15, state='disabled', cursor='arrow')
4142
self.explode_label.pack()
42-
self.explode_text_widget.pack()
43+
self.explode_text_widget.pack(side=tk.LEFT)
44+
self.explode_text_widget_frame.pack()
4345
self.explode_frame.pack(side=tk.LEFT)
4446

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

47-
self.scrollbar = tk.Scrollbar(self.container_frame, orient="vertical", command=self.multiple_yview)
48-
self.container_frame.pack(ipadx=9)
49+
self.scrollbar = tk.Scrollbar(self.explode_text_widget_frame, orient="vertical", command=self.multiple_yview)
50+
self.scrollbar.pack(side=tk.LEFT, fill='y')
51+
52+
for widgets in self.pie_text_widgets:
53+
widgets.config(yscrollcommand=self.scrollbar.set)
54+
55+
self.container_frame.pack(padx=1)
4956

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

59-
self.toplevel.after(0, self.show_scrollbar)
6066
self.toplevel.protocol('WM_DELETE_WINDOW', self.top_level_exit)
6167
self.toplevel.after(0, lambda: include.initial_position(self.toplevel, 'Register'))
6268
self.toplevel.after(0, lambda: commands.insert_to_text_widget(self.pie_text_widgets, _vars))
@@ -90,39 +96,6 @@ def top_level_exit(self):
9096
self.enable_editing_command()
9197
messagebox.showinfo('Edit Mode', 'Editing Mode is Enabled')
9298

93-
def show_scrollbar(self):
94-
'''Show self.scrollbar when the character in the text is more than the height of the text widget'''
95-
96-
try:
97-
if self.pie_text_widgets[0].cget('height') < int(self.pie_text_widgets[0].index('end-1c').split('.')[0]):
98-
self.scrollbar.pack(side=tk.LEFT, fill='y')
99-
100-
for widgets in self.pie_text_widgets:
101-
widgets.config(yscrollcommand=self.scrollbar.set)
102-
103-
self.master.after(100, self.hide_scrollbar)
104-
105-
else:
106-
self.master.after(100, self.show_scrollbar)
107-
108-
except tk.TclError:
109-
pass
110-
111-
def hide_scrollbar(self):
112-
'''Hide self.scrollbar when the character in the text is less than the height of the text widget'''
113-
114-
try:
115-
if self.pie_text_widgets[0].cget('height') >= int(self.pie_text_widgets[0].index('end-1c').split('.')[0]):
116-
self.scrollbar.pack_forget()
117-
118-
for widgets in self.pie_text_widgets:
119-
widgets.config(yscrollcommand=None)
120-
121-
self.master.after(100, self.show_scrollbar)
122-
123-
except tk.TclError:
124-
pass
125-
12699
def enable_editing_command(self):
127100
'''When user clicks the enable editing checkbutton'''
128101

0 commit comments

Comments
 (0)