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

Commit fd38871

Browse files
authored
Instruction is shown when hovers to that widget
Clearing values in ``explode``, ``pie_items`` and ``pie_items_percentage`` variable were broken (that was my fault when I refactored code). Glad I found it while testing. **Parent window** was added to the messagebox so that user must click any choice in messagebox and then redirect to the tix.Tk or tix.Toplevel window. Added functionality to make tix.Tk window disappear when user opens Toplevel window and reappear when user exits the tix.Toplevel window. Removed **Exit Pie Chart Creator** button because I think its useless as we can do the same thing with X button from the title bar. **Small window gets pop-up when user hover to the specific widget with some texts. This replaces "Instruction" window.** Added additional exceptions to prevent program from crashing or to make user know what went wrong. Changed button name "Make chart with registered values" to "Make Pie-Chart". Deleted some code which became useless after adding new features. Clicking image at the main window opens the GitHub repository. Names of some variables and parameters are changed. Added image of the program to README.md. Used tix window instead of Tk window. Placed buttons one after another. Deleted "Instructions.txt". Improved some comments.
1 parent fc1f99e commit fd38871

File tree

3 files changed

+89
-90
lines changed

3 files changed

+89
-90
lines changed

README.md

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

3+
<img src="included_files/main.jpg">
4+
35
A Python program which creates a pie chart using MatPlotLib and has a GUI created using Tkinter. Feel free to copy and use my code anywhere, just don't forget to credit me!
46

57
I would like to thank Ghanteyyy (http://github.com/ghanteyyy) for contributing to this project. This program wouldn't have been so nice without his efforts.

included_files/main.jpg

16.4 KB
Loading

pie_chart_creator.py

Lines changed: 87 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,62 @@
11
import os
22
import sys
3+
import webbrowser
34

45
try: # Python 3
5-
import tkinter as tk
6+
import tkinter.tix as tix
67
from tkinter import messagebox
78

89
except (ModuleNotFoundError, ImportError): # Python 2
9-
import Tkinter as tk
10+
import Tix as tix
1011
import tkMessageBox as messagebox
1112

13+
import requests
1214
import matplotlib.pyplot as plt
1315

1416

15-
def center_window(window, title):
16-
'''This function places any window (either TopLevel or Tk) to the center of the screen'''
17+
def is_internet():
18+
'''Check if you are connected to internet'''
1719

18-
window.withdraw()
19-
20-
window.update()
21-
window.focus()
22-
window.grab_set()
23-
window.title(title)
24-
window.resizable(0, 0)
25-
window.iconbitmap(resource_path('included_files\\icon.ico'))
26-
27-
width, height = window.winfo_width(), window.winfo_height()
28-
window.geometry(f'{width}x{height}+{screen_width - width // 2}+{screen_height - height // 2}')
29-
window.deiconify()
30-
31-
32-
def change_style(wid):
33-
'''Change text styles to bold or italic'''
34-
35-
italic_index = ('23.0', '23.83')
36-
bold_indexs = [('1.0', '1.19'), ('4.0', '4.14'), ('5.4', '5.31'), ('8.4', '8.22'), ('11.4', '11.23'), ('14.4', '14.31'), ('17.4', '17.26'), ('20.4', '20.42')]
37-
38-
for start, end in bold_indexs:
39-
wid.tag_add('b', start, end)
40-
41-
wid.tag_configure('b', font=('Helvetica', '11', 'bold'))
20+
try:
21+
requests.get('http://google.com')
22+
return True
4223

43-
wid.tag_add('i', italic_index[0], italic_index[1])
44-
wid.tag_configure('i', font=('Helvetica', '11', 'italic'))
24+
except requests.ConnectionError:
25+
return False
4526

46-
wid.config(state=tk.DISABLED)
4727

28+
def center_window(_window, title):
29+
'''Set position of TopLevel or Tk window to the center of the screen'''
4830

49-
def instructions():
50-
'''Show instruction window'''
31+
_window.withdraw()
5132

52-
file_path = resource_path('included_files\\instructions.txt')
33+
_window.update()
34+
_window.title(title)
35+
_window.resizable(0, 0)
36+
_window.iconbitmap(resource_path('included_files\\icon.ico'))
5337

54-
with open(file_path, 'r') as f:
55-
contents = f.read().strip('\n')
38+
width, height = _window.winfo_width(), _window.winfo_height()
39+
_window.geometry(f'{width}x{height}+{screen_width - width // 2}+{screen_height - height // 2}')
5640

57-
instructions_window = tk.Toplevel(window)
41+
_window.deiconify()
5842

59-
instructions_text_widget_frame = tk.Frame(instructions_window)
6043

61-
instructions_text_widget = tk.Text(instructions_text_widget_frame, height=26, cursor='arrow')
62-
instructions_text_widget.insert('1.0', contents)
63-
instructions_text_widget.pack(side=tk.LEFT)
44+
def open_link(event=None, _window=None):
45+
'''Open the github page of the author(NMrocks) in the default browser'''
6446

65-
instructions_text_widget_frame.pack()
47+
if is_internet():
48+
window.after(0, lambda: webbrowser.open('http://github.com/NMrocks/Pie-Chart-Creator'))
6649

67-
instructions_window.after(0, lambda: change_style(instructions_text_widget))
68-
instructions_window.after(0, lambda: center_window(instructions_window, 'PCC Instructions'))
69-
instructions_window.mainloop()
50+
else:
51+
messagebox.showerror('ERROR', 'Unable to load page because you are not connected to internet', parent=_window)
7052

7153

7254
def append():
7355
'''Store data given by the user'''
7456

7557
item = item_entry.get().title()
76-
explode_ = explode_entry.get().title()
7758
percentage = percentage_entry.get()
59+
explode_ = explode_entry.get().title()
7860

7961
if not item:
8062
messagebox.showerror('Invalid Input', 'Invalid name of item')
@@ -92,7 +74,7 @@ def append():
9274
explode.append(0)
9375

9476
for widget in [item_entry, percentage_entry, explode_entry]:
95-
widget.delete(0, tk.END)
77+
widget.delete(0, tix.END)
9678

9779
else:
9880
messagebox.showerror('Invalid Percentage', 'Percentage must be in number')
@@ -107,23 +89,24 @@ def show_register():
10789

10890
a = 0
10991

110-
register_window = tk.Toplevel()
92+
window.withdraw()
93+
register_window = tix.Toplevel()
11194

11295
if pie_items:
113-
added_lbl_1 = tk.Label(register_window, text="Name of item", justify=tk.LEFT)
96+
added_lbl_1 = tix.Label(register_window, text="Name of item", justify=tix.LEFT)
11497
added_lbl_1.grid(row=0, column=0, pady=5)
11598

116-
added_lbl_2 = tk.Label(register_window, text="Percentage", justify=tk.LEFT)
99+
added_lbl_2 = tix.Label(register_window, text="Percentage", justify=tix.LEFT)
117100
added_lbl_2.grid(row=0, column=1, pady=5)
118101

119-
added_lbl_3 = tk.Label(register_window, text="Emphasis", justify=tk.LEFT)
102+
added_lbl_3 = tix.Label(register_window, text="Emphasis", justify=tix.LEFT)
120103
added_lbl_3.grid(row=0, column=2, pady=5)
121104

122105
for pie_item in copy_pie_items:
123-
appended_lbl1 = tk.Label(register_window, text=f"{pie_item.title()}")
106+
appended_lbl1 = tix.Label(register_window, text=f"{pie_item.title()}")
124107
appended_lbl1.grid(row=a + 1, column=0)
125108

126-
appended_lbl2 = tk.Label(register_window, text=f"{copy_pie_items_percentage[a]}")
109+
appended_lbl2 = tix.Label(register_window, text=f"{copy_pie_items_percentage[a]}")
127110
appended_lbl2.grid(row=a + 1, column=1)
128111

129112
if copy_explode[a] == 0.1:
@@ -132,21 +115,22 @@ def show_register():
132115
else:
133116
_explode_ = "Disabled"
134117

135-
appended_lbl3 = tk.Label(register_window, text=f"{_explode_}")
118+
appended_lbl3 = tix.Label(register_window, text=f"{_explode_}")
136119
appended_lbl3.grid(row=a + 1, column=2)
137120

138121
a += 1
139122

140123
appended_lbl2.grid(pady=10)
141124

142125
else:
143-
added_lbl_1 = tk.Label(register_window, text="No items added yet. Maybe add some items?")
126+
added_lbl_1 = tix.Label(register_window, text="No items added yet. Maybe add some items?")
144127
added_lbl_1.grid(row=0, column=1, pady=10)
145128

146-
clear_register_btn = tk.Button(register_window, text="Clear Register", justify='center', bd=1, cursor='hand2', relief=tk.SOLID, command=lambda: clear(register_window))
129+
clear_register_btn = tix.Button(register_window, text="Clear Register", justify='center', bd=1, cursor='hand2', relief=tix.SOLID, command=lambda: _clear(register_window))
147130
clear_register_btn.grid(row=a + 1, column=1, pady=10)
148131

149132
register_window.after(0, lambda: center_window(register_window, 'PCC Register'))
133+
register_window.protocol('WM_DELETE_WINDOW', lambda: _exit(register_window))
150134
register_window.mainloop()
151135

152136

@@ -163,29 +147,34 @@ def make_chart():
163147
messagebox.showerror('No data', 'No data were inputed to make pie-charts')
164148

165149

166-
def _exit_():
167-
'''Quit program'''
150+
def _exit(_window):
151+
'''Destroy Tk window or Toplevel window'''
168152

169-
if messagebox.askyesno('Exit?', 'Do you really want to exit?\nAll registered values will be lost'):
170-
window.destroy()
153+
if _window == window:
154+
if messagebox.askyesno('Exit', 'Do you really want to exit?'):
155+
window.destroy()
171156

157+
else:
158+
_window.destroy()
159+
window.deiconify()
172160

173-
def clear(window=None):
161+
162+
def _clear(_window=None):
174163
'''Clear data from the register'''
175164

176165
if pie_items:
177-
if messagebox.askyesno('Clear Register?', 'Do you really want to clear REGISTER?'):
166+
if messagebox.askyesno('Clear Register?', 'Do you really want to clear REGISTER?', parent=_window):
178167
for lists in [pie_items, pie_items_percentage, explode]:
179-
del lists[0]
168+
lists.clear()
180169

181170
else:
182-
messagebox.showinfo('Clear Register?', "No items added yet. Maybe add some items?")
171+
messagebox.showinfo('Clear Register', "No items added yet. How about adding some items?", parent=_window)
183172

184-
if window:
185-
window.destroy()
173+
if _window:
174+
_exit(_window)
186175

187176

188-
def left_button_bind(event, window):
177+
def left_button_bind(event=None):
189178
'''Focus out from the entry widget when user clicks to any widget'''
190179

191180
if event.widget not in [item_entry, percentage_entry, explode_entry]:
@@ -212,53 +201,61 @@ def resource_path(relative_path):
212201

213202
explode, pie_items, pie_items_percentage = [], [], []
214203

215-
window = tk.Tk()
204+
window = tix.Tk()
205+
216206
screen_width, screen_height = window.winfo_screenwidth() // 2, window.winfo_screenheight() // 2
217207

218208
for _ in range(8):
219209
window.columnconfigure(_, weight=1)
220210
window.rowconfigure(_, weight=1)
221211

222-
pcc_logo = tk.PhotoImage(file=resource_path('included_files\\PCC_Logo.png'))
223-
pcc_logo_lbl = tk.Label(window, image=pcc_logo)
212+
pcc_logo = tix.PhotoImage(file=resource_path('included_files\\PCC_Logo.png'))
213+
pcc_logo_lbl = tix.Label(window, image=pcc_logo, cursor='hand2')
224214
pcc_logo_lbl.grid(column=1, columnspan=3)
215+
pcc_logo_lbl.bind('<Button-1>', open_link)
225216

226-
item_entry_lbl = tk.Label(window, text="Name of item:")
217+
item_entry_lbl = tix.Label(window, text="Name of item:")
227218
item_entry_lbl.grid(row=1, column=1, sticky="W")
228219

229-
item_entry = tk.Entry(window)
220+
item_entry = tix.Entry(window)
230221
item_entry.grid(row=1, column=2, sticky="WE")
231222

232-
percentage_entry_lbl = tk.Label(window, text="Percentage:")
223+
percentage_entry_lbl = tix.Label(window, text="Percentage:")
233224
percentage_entry_lbl.grid(row=2, column=1, sticky="W")
234225

235-
percentage_entry = tk.Entry(window)
226+
percentage_entry = tix.Entry(window)
236227
percentage_entry.grid(row=2, column=2, sticky="WE")
237228

238-
explode_entry_lbl = tk.Label(window, text="Enable emphasis(Y/N):")
229+
explode_entry_lbl = tix.Label(window, text="Enable emphasis(Y/N):")
239230
explode_entry_lbl.grid(row=3, column=1, sticky="W")
240231

241-
explode_entry = tk.Entry(window)
232+
explode_entry = tix.Entry(window)
242233
explode_entry.grid(row=3, column=2, sticky="WE")
243234

244-
append_btn = tk.Button(window, text="Add values to register", command=append)
245-
append_btn.grid(row=4, column=1, sticky="WE", padx=1, pady=1)
235+
append_btn = tix.Button(window, text="Add values to register", cursor='hand2', command=append)
236+
append_btn.grid(row=4, column=1, sticky="WE", columnspan=3, padx=1, pady=1)
246237

247-
make_chart_btn = tk.Button(window, text="Make chart with registered values", command=make_chart)
248-
make_chart_btn.grid(row=7, column=1, columnspan=2, sticky="WE", padx=1, pady=1)
238+
show_register_btn = tix.Button(window, text="View register", cursor='hand2', command=show_register)
239+
show_register_btn.grid(row=5, column=1, sticky="WE", columnspan=3, padx=1, pady=1)
249240

250-
clear_btn = tk.Button(window, text="Clear Register", command=clear)
251-
clear_btn.grid(row=5, column=1, sticky="WE", padx=1, pady=1)
241+
clear_btn = tix.Button(window, text="Clear Register", cursor='hand2', command=_clear)
242+
clear_btn.grid(row=6, column=1, sticky="WE", columnspan=3, padx=1, pady=1)
252243

253-
exit_btn = tk.Button(window, text="Exit Pie Chart Creator", command=_exit_)
254-
exit_btn.grid(row=5, column=2, sticky="WE", padx=1, pady=1)
244+
make_chart_btn = tix.Button(window, text="Make Pie-Chart", cursor='hand2', command=make_chart)
245+
make_chart_btn.grid(row=7, column=1, columnspan=3, sticky="WE", padx=1, pady=1)
255246

256-
show_register_btn = tk.Button(window, text="View register", command=show_register)
257-
show_register_btn.grid(row=4, column=2, sticky="WE", padx=1, pady=1)
247+
window.bind('<Button-1>', left_button_bind)
248+
window.protocol('WM_DELETE_WINDOW', lambda: _exit(window))
249+
window.after(0, lambda: center_window(window, 'Pie Chart Creator'))
258250

259-
instructions_btn = tk.Button(window, text="Read Instructions", command=instructions)
260-
instructions_btn.grid(row=6, column=1, columnspan=2, sticky="WE", padx=1, pady=1)
251+
balloon = tix.Balloon(window)
252+
balloon.bind_widget(item_entry, balloonmsg='Input desire name for your item.')
253+
balloon.bind_widget(clear_btn, balloonmsg='Clear ALL VALUES from the register.')
254+
balloon.bind_widget(pcc_logo_lbl, balloonmsg='http://github.com/NMrocks/Pie-Chart-Creator')
255+
balloon.bind_widget(show_register_btn, balloonmsg='View and Edit items stored in register.')
256+
balloon.bind_widget(percentage_entry, balloonmsg='Input percentage for the name of your item.')
257+
balloon.bind_widget(make_chart_btn, balloonmsg='Generate a pie-chart according to the data provided by you.')
258+
balloon.bind_widget(append_btn, balloonmsg='Add values you have entered in the input fields to the register.')
259+
balloon.bind_widget(explode_entry, balloonmsg="'Y' enables emphasis whereas 'N' disables it. If enabled some\nspaces will be created between the items in the pie-chart.")
261260

262-
window.bind('<Button-1>', lambda event, window=window: left_button_bind(event, window))
263-
window.after(0, lambda: center_window(window, 'Pie Chart Creator'))
264261
window.mainloop()

0 commit comments

Comments
 (0)