1- import matplotlib .pyplot as plt
1+ import os
2+ import sys
23
3- try :
4+ try : # Python 3
45 import tkinter as tk
56 from tkinter import messagebox
67
7- except (ModuleNotFoundError , ImportError ):
8+ except (ModuleNotFoundError , ImportError ): # Python 2
89 import Tkinter as tk
910 import tkMessageBox as messagebox
1011
12+ import matplotlib .pyplot as plt
13+
1114
1215def center_window (window , title ):
1316 '''This function places any window (either TopLevel or Tk) to the center of the screen'''
@@ -19,28 +22,56 @@ def center_window(window, title):
1922 window .grab_set ()
2023 window .title (title )
2124 window .resizable (0 , 0 )
22- window .iconbitmap (' icon.ico' )
25+ window .iconbitmap (resource_path ( 'included_files \\ icon.ico') )
2326
2427 width , height = window .winfo_width (), window .winfo_height ()
2528 window .geometry (f'{ width } x{ height } +{ screen_width - width // 2 } +{ screen_height - height // 2 } ' )
26-
2729 window .deiconify ()
2830
2931
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' ))
42+
43+ wid .tag_add ('i' , italic_index [0 ], italic_index [1 ])
44+ wid .tag_configure ('i' , font = ('Helvetica' , '11' , 'italic' ))
45+
46+ wid .config (state = tk .DISABLED )
47+
48+
3049def instructions ():
31- with open ('instructions.txt' , 'r' ) as f :
50+ '''Show instruction window'''
51+
52+ file_path = resource_path ('included_files\\ instructions.txt' )
53+
54+ with open (file_path , 'r' ) as f :
3255 contents = f .read ().strip ('\n ' )
3356
3457 instructions_window = tk .Toplevel (window )
3558
36- instruction_lbl = tk .Label (master = instructions_window , text = contents , justify = tk .LEFT )
37- instruction_lbl .grid (row = 1 , column = 1 )
59+ instructions_text_widget_frame = tk .Frame (instructions_window )
60+
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 )
64+
65+ instructions_text_widget_frame .pack ()
3866
67+ instructions_window .after (0 , lambda : change_style (instructions_text_widget ))
3968 instructions_window .after (0 , lambda : center_window (instructions_window , 'PCC Instructions' ))
4069 instructions_window .mainloop ()
4170
4271
4372def append ():
73+ '''Store data given by the user'''
74+
4475 item = item_entry .get ().title ()
4576 explode_ = explode_entry .get ().title ()
4677 percentage = percentage_entry .get ()
@@ -54,7 +85,7 @@ def append():
5485 pie_items .append (item )
5586 pie_items_percentage .append (percentage )
5687
57- if explode_ . title () in ['Y' , 'Yes' ]:
88+ if explode_ in ['Y' , 'Yes' ]:
5889 explode .append (0.1 )
5990
6091 else :
@@ -64,10 +95,12 @@ def append():
6495 widget .delete (0 , tk .END )
6596
6697 else :
67- messagebox .showerror ('Invalid Percentage' , 'Percentage must be a number' )
98+ messagebox .showerror ('Invalid Percentage' , 'Percentage must be in number' )
6899
69100
70101def show_register ():
102+ '''Show stored data'''
103+
71104 copy_pie_items = pie_items
72105 copy_pie_items_percentage = pie_items_percentage
73106 copy_explode = explode
@@ -118,20 +151,30 @@ def show_register():
118151
119152
120153def make_chart ():
121- figure , pie_chart = plt .subplots ()
122- pie_chart .pie (pie_items_percentage , explode = explode , labels = pie_items , autopct = '%1.2f%%' , shadow = True , startangle = 90 )
123- pie_chart .axis ('equal' )
124- plt .show ()
154+ '''Make pie-chart as per the user's data'''
155+
156+ if pie_items :
157+ figure , pie_chart = plt .subplots ()
158+ pie_chart .pie (pie_items_percentage , explode = explode , labels = pie_items , autopct = '%1.2f%%' , shadow = True , startangle = 90 )
159+ pie_chart .axis ('equal' )
160+ plt .show ()
161+
162+ else :
163+ messagebox .showerror ('No data' , 'No data were inputed to make pie-charts' )
125164
126165
127166def _exit_ ():
167+ '''Quit program'''
168+
128169 if messagebox .askyesno ('Exit?' , 'Do you really want to exit?\n All registered values will be lost' ):
129170 window .destroy ()
130171
131172
132173def clear (window = None ):
174+ '''Clear data from the register'''
175+
133176 if pie_items :
134- if messagebox .askyesno ('Clear Register?' , 'Do you really want to clear the register? \n This cannot be undone. ' ):
177+ if messagebox .askyesno ('Clear Register?' , 'Do you really want to clear REGISTER? ' ):
135178 for lists in [pie_items , pie_items_percentage , explode ]:
136179 del lists [0 ]
137180
@@ -149,9 +192,25 @@ def left_button_bind(event, window):
149192 window .focus ()
150193
151194
152- explode = []
153- pie_items = []
154- pie_items_percentage = []
195+ def resource_path (relative_path ):
196+ '''Get absolute path to resource from temporary directory
197+
198+ In development:
199+ Gets path of files that are used in this script like icons, images or file of any extension from current directory
200+
201+ After compiling to .exe with pyinstaller and using --add-data flag:
202+ Gets path of files that are used in this script like icons, images or file of any extension from temporary directory'''
203+
204+ try :
205+ base_path = sys ._MEIPASS # PyInstaller creates a temporary directory and stores path of that directory in _MEIPASS
206+
207+ except AttributeError :
208+ base_path = os .path .abspath ("." )
209+
210+ return os .path .join (base_path , relative_path )
211+
212+
213+ explode , pie_items , pie_items_percentage = [], [], []
155214
156215window = tk .Tk ()
157216screen_width , screen_height = window .winfo_screenwidth () // 2 , window .winfo_screenheight () // 2
@@ -160,7 +219,7 @@ def left_button_bind(event, window):
160219 window .columnconfigure (_ , weight = 1 )
161220 window .rowconfigure (_ , weight = 1 )
162221
163- pcc_logo = tk .PhotoImage (file = " PCC_Logo.png" )
222+ pcc_logo = tk .PhotoImage (file = resource_path ( 'included_files \\ PCC_Logo.png' ) )
164223pcc_logo_lbl = tk .Label (window , image = pcc_logo )
165224pcc_logo_lbl .grid (column = 1 , columnspan = 3 )
166225
0 commit comments