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

Commit 721d482

Browse files
authored
Merge pull request #4 from ghanteyyy/master
Re-designed from scratch
2 parents 9a84aff + 9adc32d commit 721d482

File tree

10 files changed

+497
-267
lines changed

10 files changed

+497
-267
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# Pie-Chart-Creator
22

3-
<img src="included_files/main.jpg">
3+
<img src="included_files/main.gif">
44

55
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!
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.
8-
9-
**Important Note**
10-
11-
This program will not work unless `pie_chart_creator.py` and the `included_files` folder are in the same folder on your device.
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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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?")

include.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
The global function used by both main window and view window
3+
'''
4+
5+
6+
import os
7+
import sys
8+
9+
10+
def resource_path(relative_path):
11+
'''Get absolute path to resource from temporary directory
12+
13+
In development:
14+
Gets path of files that are used in this script like icons, images or file of any extension from current directory
15+
16+
After compiling to .exe with pyinstaller and using --add-data flag:
17+
Gets path of files that are used in this script like icons, images or file of any extension from temporary directory'''
18+
19+
try:
20+
base_path = sys._MEIPASS # PyInstaller creates a temporary directory and stores path of that directory in _MEIPASS
21+
22+
except AttributeError:
23+
base_path = os.path.abspath(".")
24+
25+
return os.path.join(base_path, relative_path)
26+
27+
28+
def initial_position(window, title):
29+
'''Set window to the center of the screen at the startup'''
30+
31+
window.withdraw()
32+
window.update()
33+
34+
width, height = window.winfo_width(), window.winfo_height()
35+
screen_width, screen_height = window.winfo_screenwidth() // 2, window.winfo_screenheight() // 2
36+
37+
window.geometry(f'{width}x{height}+{screen_width - width // 2}+{screen_height - height // 2}')
38+
39+
window.resizable(0, 0)
40+
window.iconbitmap(resource_path('included_files\\icon.ico'))
41+
window.title(title)
42+
43+
window.deiconify()

included_files/main.gif

763 KB
Loading

link.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Open the github repository to view the codes of this program
3+
'''
4+
5+
import requests
6+
import webbrowser
7+
from tkinter import messagebox
8+
9+
10+
def is_internet():
11+
'''Check if you are connected to internet'''
12+
13+
try:
14+
requests.get('http://google.com')
15+
return True
16+
17+
except requests.ConnectionError:
18+
return False
19+
20+
21+
def open_link(master, event=None):
22+
'''Open the github page of the author(NMrocks) in the default browser'''
23+
24+
if is_internet():
25+
master.after(0, lambda: webbrowser.open('http://github.com/NMrocks/Pie-Chart-Creator'))
26+
27+
else:
28+
messagebox.showerror('ERROR', 'Unable to load page because you are not connected to internet')

0 commit comments

Comments
 (0)