1
+ # Python Calculator
2
+
3
+ from tkinter import *
4
+
5
+ root = Tk ()
6
+ root .geometry ("500x500" )
7
+ root .resizable (0 , 0 )
8
+ root .title ('Python Calculator' )
9
+
10
+ expression = ""
11
+
12
+ input_text = StringVar ()
13
+
14
+ # clear
15
+
16
+
17
+ def btn_clear ():
18
+ global expression
19
+ expression = ""
20
+ input_text .set ("" )
21
+
22
+ # click
23
+
24
+
25
+ def btn_click (item ):
26
+ global expression
27
+ expression = expression + str (item )
28
+ input_text .set (expression )
29
+
30
+ # calculate
31
+
32
+
33
+ def btn_equal ():
34
+ global expression
35
+ result = str (eval (expression ))
36
+ input_text .set (result )
37
+ expression = ""
38
+
39
+
40
+ # input frame
41
+ input_frame = Frame (root , width = 312 , height = 50 , bd = 0 ,
42
+ highlightbackground = "black" , highlightcolor = "black" ,
43
+ highlightthickness = 2 )
44
+
45
+ input_frame .pack (side = TOP )
46
+
47
+ # input field inside the frame
48
+ input_field = Entry (input_frame , font = ('arial' , 18 , 'bold' ),
49
+ textvariable = input_text , width = 50 , bg = "#eee" , bd = 0 , justify = RIGHT )
50
+
51
+ input_field .grid (row = 0 , column = 0 )
52
+
53
+ input_field .pack (ipady = 10 )
54
+
55
+ # button frame
56
+ btns_frame = Frame (root , width = 312 , height = 272.5 , bg = "grey" )
57
+
58
+ btns_frame .pack ()
59
+
60
+ # first row
61
+ clear = Button (btns_frame , text = "CLEAR" , fg = "black" , width = 32 ,
62
+ height = 3 , bd = 0 , bg = "#eee" , cursor = "hand2" , command = lambda :
63
+ btn_clear ()).grid (row = 0 , column = 0 , columnspan = 3 , padx = 1 , pady = 1 )
64
+ divide = Button (btns_frame , text = "/" , fg = "black" , width = 10 ,
65
+ height = 3 , bd = 0 , bg = "#eee" , cursor = "hand2" , command = lambda :
66
+ btn_click ("/" )).grid (row = 0 , column = 3 , padx = 1 , pady = 1 )
67
+
68
+ # second row
69
+ seven = Button (btns_frame , text = "7" , fg = "black" , width = 10 ,
70
+ height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
71
+ btn_click (7 )).grid (row = 1 , column = 0 , padx = 1 , pady = 1 )
72
+ eight = Button (btns_frame , text = "8" , fg = "black" , width = 10 ,
73
+ height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
74
+ btn_click (8 )).grid (row = 1 , column = 1 , padx = 1 , pady = 1 )
75
+ nine = Button (btns_frame , text = "9" , fg = "black" , width = 10 , height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
76
+ btn_click (9 )).grid (row = 1 , column = 2 , padx = 1 , pady = 1 )
77
+ multiply = Button (btns_frame , text = "*" , fg = "black" , width = 10 ,
78
+ height = 3 , bd = 0 , bg = "#eee" , cursor = "hand2" , command = lambda :
79
+ btn_click ("*" )).grid (row = 1 , column = 3 , padx = 1 , pady = 1 )
80
+
81
+ # third row
82
+ four = Button (btns_frame , text = "4" , fg = "black" , width = 10 , height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
83
+ btn_click (4 )).grid (row = 2 , column = 0 , padx = 1 , pady = 1 )
84
+ five = Button (btns_frame , text = "5" , fg = "black" , width = 10 , height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
85
+ btn_click (5 )).grid (row = 2 , column = 1 , padx = 1 , pady = 1 )
86
+ six = Button (btns_frame , text = "6" , fg = "black" , width = 10 , height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
87
+ btn_click (6 )).grid (row = 2 , column = 2 , padx = 1 , pady = 1 )
88
+ minus = Button (btns_frame , text = "-" , fg = "black" , width = 10 ,
89
+ height = 3 , bd = 0 , bg = "#eee" , cursor = "hand2" , command = lambda :
90
+ btn_click ("-" )).grid (row = 2 , column = 3 , padx = 1 , pady = 1 )
91
+
92
+ # fourth row
93
+ one = Button (btns_frame , text = "1" , fg = "black" , width = 10 , height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
94
+ btn_click (1 )).grid (row = 3 , column = 0 , padx = 1 , pady = 1 )
95
+ two = Button (btns_frame , text = "2" , fg = "black" , width = 10 , height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
96
+ btn_click (2 )).grid (row = 3 , column = 1 , padx = 1 , pady = 1 )
97
+ three = Button (btns_frame , text = "3" , fg = "black" , width = 10 ,
98
+ height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
99
+ btn_click (3 )).grid (row = 3 , column = 2 , padx = 1 , pady = 1 )
100
+ plus = Button (btns_frame , text = "+" , fg = "black" , width = 10 , height = 3 , bd = 0 , bg = "#eee" , cursor = "hand2" , command = lambda :
101
+ btn_click ("+" )).grid (row = 3 , column = 3 , padx = 1 , pady = 1 )
102
+
103
+ # fourth row
104
+ zero = Button (btns_frame , text = "0" , fg = "black" , width = 21 , height = 3 , bd = 0 , bg = "#fff" , cursor = "hand2" , command = lambda :
105
+ btn_click (0 )).grid (row = 4 , column = 0 , columnspan = 2 , padx = 1 , pady = 1 )
106
+ point = Button (btns_frame , text = "." , fg = "black" , width = 10 ,
107
+ height = 3 , bd = 0 , bg = "#eee" , cursor = "hand2" , command = lambda :
108
+ btn_click ("." )).grid (row = 4 , column = 2 , padx = 1 , pady = 1 )
109
+ equals = Button (btns_frame , text = "=" , fg = "black" , width = 10 ,
110
+ height = 3 , bd = 0 , bg = "#eee" , cursor = "hand2" , command = lambda :
111
+ btn_equal ()).grid (row = 4 , column = 3 , padx = 1 , pady = 1 )
112
+
113
+ root .mainloop ()
0 commit comments