1
+ # pip install tkinter
2
+ import tkinter as tk
3
+ import tkinter .messagebox
4
+ from tkinter .constants import SUNKEN
5
+
6
+ window = tk .Tk ()
7
+ window .title ('Calculator-Python' )
8
+ frame = tk .Frame (master = window , bg = "khaki1" , padx = 200 )
9
+ frame .pack ()
10
+ entry = tk .Entry (master = frame , relief = SUNKEN , borderwidth = 3 , width = 35 )
11
+ entry .grid (row = 0 , column = 0 , columnspan = 3 , ipady = 2 , pady = 2 )
12
+
13
+
14
+ def myclick (number ):
15
+ entry .insert (tk .END , number )
16
+
17
+
18
+ def equal ():
19
+ try :
20
+ y = str (eval (entry .get ()))
21
+ entry .delete (0 , tk .END )
22
+ entry .insert (0 , y )
23
+ except :
24
+ tkinter .messagebox .showinfo ("Error" , "Syntax Error" )
25
+
26
+
27
+ def clear ():
28
+ entry .delete (0 , tk .END )
29
+
30
+
31
+ button_1 = tk .Button (master = frame , text = '1' , padx = 15 ,
32
+ pady = 5 , width = 3 , command = lambda : myclick (1 ))
33
+ button_1 .grid (row = 1 , column = 0 , pady = 2 )
34
+ button_2 = tk .Button (master = frame , text = '2' , padx = 15 ,
35
+ pady = 5 , width = 3 , command = lambda : myclick (2 ))
36
+ button_2 .grid (row = 1 , column = 1 , pady = 2 )
37
+ button_3 = tk .Button (master = frame , text = '3' , padx = 15 ,
38
+ pady = 5 , width = 3 , command = lambda : myclick (3 ))
39
+ button_3 .grid (row = 1 , column = 2 , pady = 2 )
40
+ button_4 = tk .Button (master = frame , text = '4' , padx = 15 ,
41
+ pady = 5 , width = 3 , command = lambda : myclick (4 ))
42
+ button_4 .grid (row = 2 , column = 0 , pady = 2 )
43
+ button_5 = tk .Button (master = frame , text = '5' , padx = 15 ,
44
+ pady = 5 , width = 3 , command = lambda : myclick (5 ))
45
+ button_5 .grid (row = 2 , column = 1 , pady = 2 )
46
+ button_6 = tk .Button (master = frame , text = '6' , padx = 15 ,
47
+ pady = 5 , width = 3 , command = lambda : myclick (6 ))
48
+ button_6 .grid (row = 2 , column = 2 , pady = 2 )
49
+ button_7 = tk .Button (master = frame , text = '7' , padx = 15 ,
50
+ pady = 5 , width = 3 , command = lambda : myclick (7 ))
51
+ button_7 .grid (row = 3 , column = 0 , pady = 2 )
52
+ button_8 = tk .Button (master = frame , text = '8' , padx = 15 ,
53
+ pady = 5 , width = 3 , command = lambda : myclick (8 ))
54
+ button_8 .grid (row = 3 , column = 1 , pady = 2 )
55
+ button_9 = tk .Button (master = frame , text = '9' , padx = 15 ,
56
+ pady = 5 , width = 3 , command = lambda : myclick (9 ))
57
+ button_9 .grid (row = 3 , column = 2 , pady = 2 )
58
+ button_0 = tk .Button (master = frame , text = '0' , padx = 15 ,
59
+ pady = 5 , width = 3 , command = lambda : myclick (0 ))
60
+ button_0 .grid (row = 4 , column = 1 , pady = 2 )
61
+
62
+ button_add = tk .Button (master = frame , text = "+" , padx = 15 ,
63
+ pady = 5 , width = 3 , command = lambda : myclick ('+' ))
64
+ button_add .grid (row = 5 , column = 0 , pady = 2 )
65
+
66
+ button_subtract = tk .Button (
67
+ master = frame , text = "-" , padx = 15 , pady = 5 , width = 3 , command = lambda : myclick ('-' ))
68
+ button_subtract .grid (row = 5 , column = 1 , pady = 2 )
69
+
70
+ button_multiply = tk .Button (
71
+ master = frame , text = "*" , padx = 15 , pady = 5 , width = 3 , command = lambda : myclick ('*' ))
72
+ button_multiply .grid (row = 5 , column = 2 , pady = 2 )
73
+
74
+ button_div = tk .Button (master = frame , text = "/" , padx = 15 ,
75
+ pady = 5 , width = 3 , command = lambda : myclick ('/' ))
76
+ button_div .grid (row = 6 , column = 0 , pady = 2 )
77
+
78
+ button_clear = tk .Button (master = frame , text = "clear" ,
79
+ padx = 15 , pady = 5 , width = 12 , command = clear )
80
+ button_clear .grid (row = 6 , column = 1 , columnspan = 2 , pady = 2 )
81
+
82
+ button_equal = tk .Button (master = frame , text = "=" , padx = 15 ,
83
+ pady = 5 , width = 9 , command = equal )
84
+ button_equal .grid (row = 7 , column = 0 , columnspan = 3 , pady = 2 )
85
+
86
+ window .mainloop ()
0 commit comments