1+ # Divyanshu Lohani: https://github.com/DivyanshuLohani
2+
3+ from tkinter import *
4+
5+ import tkinter .ttk
6+ root = Tk ()
7+ root .title ("Calculator - Divyanshu" )
8+ root .resizable (False , False )
9+
10+ button_texts = "C-%÷789×456-123+0.d="
11+
12+ first_col = 'C_%÷'
13+ second_col = '789×'
14+ third_col = '456-'
15+ fourth_col = '123+'
16+ fifth_col = '0.d='
17+
18+ first_bg_color_set = ["#2c3338" , "#2c3338" , "#2c3338" , "#725fcc" ]
19+ second_bg_color_set = ["#2c3338" , "#2c3338" , "#2c3338" , "#725fcc" ]
20+ third_bg_color_set = ["#2c3338" , "#2c3338" , "#2c3338" , "#725fcc" ]
21+ fourth_bg_color_set = ["#2c3338" , "#2c3338" , "#2c3338" , "#725fcc" ]
22+ fifth_bg_color_set = ["#2c3338" , "#2c3338" , "#2c3338" , "#fac40a" ]
23+
24+ main_ans = ""
25+ display_label_value = ""
26+ history_label_value = ""
27+
28+ history_label = Label (root , text = history_label_value , height = 2 , bg = "white" , font = "lucida 20" , anchor = 'e' )
29+ history_label .pack (fill = X )
30+ display_label = Label (root , text = history_label_value , height = 2 , width = 2 , pady = 5 ,bg = "white" , font = "lucida 30 bold" , anchor = 'e' )
31+ display_label .pack (fill = X )
32+ Frame (root , height = 1 , bg = 'white' ).pack (fill = X )
33+ ends_with_op = True
34+
35+ def click (event ):
36+ global display_label_value , history_label_value , main_ans , ends_with_op
37+ btn_text = event .widget .cget ('text' )
38+ # print(btn_text)
39+
40+ if btn_text == "C" :
41+ display_label_value = ""
42+ history_label_value = ""
43+ main_ans = ""
44+ elif btn_text == "-" :
45+ if ends_with_op :
46+ return
47+ main_ans += display_label_value + "-"
48+ history_label_value = main_ans
49+ display_label_value = ""
50+ ends_with_op = True
51+ elif btn_text == "+" :
52+ if ends_with_op :
53+ return
54+ main_ans += display_label_value + "+"
55+ history_label_value = main_ans
56+ display_label_value = ""
57+ ends_with_op = True
58+ elif btn_text == "÷" :
59+ if ends_with_op :
60+ return
61+ main_ans += display_label_value + "/"
62+ history_label_value = main_ans .replace ("/" , "÷" )
63+ display_label_value = ""
64+ ends_with_op = True
65+ elif btn_text == "×" :
66+ if ends_with_op :
67+ return
68+ main_ans += display_label_value + "*"
69+ history_label_value = main_ans .replace ("*" , "×" )
70+ display_label_value = ""
71+ ends_with_op = True
72+ elif btn_text == "CE" :
73+ display_label_value = display_label_value [:- 1 ]
74+ elif btn_text == "%" :
75+ try :
76+ percent = ''
77+ if not percent == display_label_value :
78+ percent += display_label_value
79+ percent = percent + "/100"
80+
81+ percent = str (eval (percent ))
82+ display_label_value = percent
83+ except :
84+
85+ pass
86+ finally :
87+ percent = ''
88+ elif btn_text == "=" :
89+ main_ans += display_label_value
90+ if display_label_value == main_ans : return
91+ history_label_value += display_label_value
92+ display_label_value = ""
93+ try :
94+ main_ans = str (eval (main_ans ))
95+ display_label_value = main_ans
96+ main_ans = ''
97+ except ZeroDivisionError as e :
98+ display_label_value = "Cannot Devide By Zero"
99+ main_ans = ''
100+ except SyntaxError as e :
101+ pass
102+ elif btn_text == "." :
103+
104+
105+ if display_label_value .find ('.' ) == - 1 :
106+ if display_label_value == "" :
107+ display_label_value = "0" + display_label_value + btn_text
108+ else :
109+ display_label_value += btn_text
110+ elif btn_text == "+/-" :
111+
112+ if display_label_value .startswith ("-" ):
113+
114+ display_label_value = display_label_value .replace ("-" , "" )
115+ else :
116+ display_label_value = "-" + display_label_value
117+ else :
118+ display_label_value += btn_text
119+ ends_with_op = False
120+ display_label ['text' ] = display_label_value
121+ history_label ['text' ] = history_label_value
122+ # print(main_ans)
123+
124+
125+
126+ for row in range (5 ):
127+ f1 = Frame (root , height = 55 )
128+ # Setup the buttons
129+ for column in range (4 ):
130+ text = ""
131+ if row == 0 :
132+ text = first_col
133+ bg_color = first_bg_color_set
134+ if row == 1 :
135+ text = second_col
136+ bg_color = second_bg_color_set
137+ if row == 2 :
138+ text = third_col
139+ bg_color = third_bg_color_set
140+ if row == 3 :
141+ text = fourth_col
142+ bg_color = fourth_bg_color_set
143+ if row == 4 :
144+ text = fifth_col
145+ bg_color = fifth_bg_color_set
146+ symbol = text [column ]
147+ # Make the respective symbols
148+ if symbol == "_" : symbol = "+/-"
149+ elif symbol == "d" : symbol = "CE"
150+ fg_color = "#dddddd"
151+ if row == 4 and column == 3 :
152+ fg_color = "black"
153+ b1 = Button (f1 , text = symbol , font = "comic 20 bold" , bg = bg_color [column ], fg = fg_color , padx = 30 , width = 2 , height = 2 , )
154+ b1 .pack (side = LEFT )
155+ b1 .bind ("<Button 1>" , click )
156+
157+
158+ f1 .pack (fill = X )
159+
160+ root .mainloop ()
0 commit comments