1- # -*- coding: utf-8 -*-
1+ # -*- coding: utf-8 -*-
22from tkinter import Tk , END , Entry , N , E , S , W , Button
33from tkinter import font
4+ from tkinter import Label
45from functools import partial
56
67
7-
88def get_input (entry , argu ):
99 entry .insert (END , argu )
1010
@@ -20,30 +20,49 @@ def clear(entry):
2020
2121def calc (entry ):
2222 input_info = entry .get ()
23- output = str (eval (input_info .strip ()))
23+ try :
24+ output = str (eval (input_info .strip ()))
25+ except ZeroDivisionError :
26+ popupmsg ()
27+ output = ""
2428 clear (entry )
2529 entry .insert (END , output )
2630
2731
32+ def popupmsg ():
33+ popup = Tk ()
34+ popup .resizable (0 , 0 )
35+ popup .geometry ("120x100" )
36+ popup .title ("Alert" )
37+ label = Label (popup , text = "Cannot divide by 0 ! \n Enter valid values" )
38+ label .pack (side = "top" , fill = "x" , pady = 10 )
39+ B1 = Button (popup , text = "Okay" , bg = "#DDDDDD" , command = popup .destroy )
40+ B1 .pack ()
41+
42+
2843def cal ():
2944 root = Tk ()
3045 root .title ("Calc" )
3146 root .resizable (0 , 0 )
3247
3348 entry_font = font .Font (size = 15 )
3449 entry = Entry (root , justify = "right" , font = entry_font )
35- entry .grid (row = 0 , column = 0 , columnspan = 4 , sticky = N + W + S + E , padx = 5 , pady = 5 )
50+ entry .grid (row = 0 , column = 0 , columnspan = 4 ,
51+ sticky = N + W + S + E , padx = 5 , pady = 5 )
3652
3753 cal_button_bg = '#FF6600'
3854 num_button_bg = '#4B4B4B'
3955 other_button_bg = '#DDDDDD'
4056 text_fg = '#FFFFFF'
4157 button_active_bg = '#C0C0C0'
4258
43- num_button = partial (Button , root , fg = text_fg , bg = num_button_bg , padx = 10 , pady = 3 , activebackground = button_active_bg )
44- cal_button = partial (Button , root , fg = text_fg , bg = cal_button_bg , padx = 10 , pady = 3 , activebackground = button_active_bg )
59+ num_button = partial (Button , root , fg = text_fg , bg = num_button_bg ,
60+ padx = 10 , pady = 3 , activebackground = button_active_bg )
61+ cal_button = partial (Button , root , fg = text_fg , bg = cal_button_bg ,
62+ padx = 10 , pady = 3 , activebackground = button_active_bg )
4563
46- button7 = num_button (text = '7' , bg = num_button_bg , command = lambda : get_input (entry , '7' ))
64+ button7 = num_button (text = '7' , bg = num_button_bg ,
65+ command = lambda : get_input (entry , '7' ))
4766 button7 .grid (row = 2 , column = 0 , pady = 5 )
4867
4968 button8 = num_button (text = '8' , command = lambda : get_input (entry , '8' ))
@@ -80,18 +99,20 @@ def cal():
8099 button12 .grid (row = 2 , column = 3 , pady = 5 )
81100
82101 button0 = num_button (text = '0' , command = lambda : get_input (entry , '0' ))
83- button0 .grid (row = 5 , column = 0 , columnspan = 2 , padx = 3 , pady = 5 , sticky = N + S + E + W )
102+ #button0.grid(row=5, column=0, columnspan=2, padx=3, pady=5, sticky=N + S + E + W)
103+ button0 .grid (row = 5 , column = 0 , pady = 5 )
84104
85105 button13 = num_button (text = '.' , command = lambda : get_input (entry , '.' ))
86- button13 .grid (row = 5 , column = 2 , pady = 5 )
106+ button13 .grid (row = 5 , column = 1 , pady = 5 )
87107
88108 button14 = Button (root , text = '/' , fg = text_fg , bg = cal_button_bg , padx = 10 , pady = 3 ,
89109 command = lambda : get_input (entry , '/' ))
90110 button14 .grid (row = 1 , column = 3 , pady = 5 )
91111
92112 button15 = Button (root , text = '<-' , bg = other_button_bg , padx = 10 , pady = 3 ,
93113 command = lambda : backspace (entry ), activebackground = button_active_bg )
94- button15 .grid (row = 1 , column = 0 , columnspan = 2 , padx = 3 , pady = 5 , sticky = N + S + E + W )
114+ button15 .grid (row = 1 , column = 0 , columnspan = 2 ,
115+ padx = 3 , pady = 5 , sticky = N + S + E + W )
95116
96117 button16 = Button (root , text = 'C' , bg = other_button_bg , padx = 10 , pady = 3 ,
97118 command = lambda : clear (entry ), activebackground = button_active_bg )
@@ -101,6 +122,10 @@ def cal():
101122 command = lambda : calc (entry ), activebackground = button_active_bg )
102123 button17 .grid (row = 5 , column = 3 , pady = 5 )
103124
125+ button18 = Button (root , text = '^' , fg = text_fg , bg = cal_button_bg , padx = 10 , pady = 3 ,
126+ command = lambda : get_input (entry , '**' ))
127+ button18 .grid (row = 5 , column = 2 , pady = 5 )
128+
104129 root .mainloop ()
105130
106131
0 commit comments