I am trying to learn how to make a GUI so I decided to practice making a calculator. I have all of the number buttons and the ‘+’, ‘-‘, ‘*’, ‘/’ buttons working. I got stuck at the decimal button. When ever I press the button in the GUI it goes into my terminal instead of the text box.
Here is the code:
JavaScript
x
156
156
1
from tkinter import *
2
3
root = Tk()
4
root.title("Calculator")
5
6
e = Entry(root, width=50, bg="light gray", fg="black", borderwidth=3)
7
e.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
8
9
10
def button_equal():
11
second_number = e.get()
12
e.delete(0, END)
13
14
if math == "addition":
15
e.insert(0, f_num + int(second_number))
16
17
if math == "subtraction":
18
e.insert(0, f_num - int(second_number))
19
20
if math == "multiplication":
21
e.insert(0, f_num * int(second_number))
22
23
if math == "division":
24
e.insert(0, f_num / int(second_number))
25
26
27
def button_click(number):
28
current = e.get()
29
e.delete(0, END)
30
e.insert(0, str(current) + str(number))
31
32
33
def button_clear():
34
e.delete(0, END)
35
36
37
def button_add():
38
first_number = e.get()
39
global f_num
40
global math
41
math = "addition"
42
f_num = int(first_number)
43
e.delete(0, END)
44
45
46
deci = "."
47
48
49
def button_equal():
50
second_number = e.get()
51
e.delete(0, END)
52
53
if math == "addition":
54
e.insert(0, f_num + int(second_number))
55
56
if math == "subtraction":
57
e.insert(0, f_num - int(second_number))
58
59
if math == "multiplication":
60
e.insert(0, f_num * int(second_number))
61
62
if math == "division":
63
e.insert(0, f_num / int(second_number))
64
65
if math == ".":
66
e.insert(0, f_num, deci)
67
68
69
def button_subtraction():
70
first_number = e.get()
71
global f_num
72
global math
73
math = "subtraction"
74
f_num = int(first_number)
75
e.delete(0, END)
76
77
78
def button_multiplication():
79
first_number = e.get()
80
global math
81
math = "multiplication"
82
f_num = int(first_number)
83
e.delete(0, END)
84
85
86
def button_division():
87
first_number = e.get()
88
global f_num
89
global math
90
math = "division"
91
f_num = int(first_number)
92
e.delete(0, END)
93
94
95
def button_decimal():
96
first_number = e.get()
97
global f_num
98
global math
99
math = " "
100
f_num = int(first_number)
101
e.delete(0, END)
102
103
104
button_1 = Button(root, text="1", padx=40, pady=20, bg="light gray", command=lambda: button_click(1))
105
button_2 = Button(root, text="2", padx=40, pady=20, bg="light gray", command=lambda: button_click(2))
106
button_3 = Button(root, text="3", padx=43, pady=20, bg="light gray", command=lambda: button_click(3))
107
button_4 = Button(root, text="4", padx=40, pady=20, bg="light gray", command=lambda: button_click(4))
108
button_5 = Button(root, text="5", padx=40, pady=20, bg="light gray", command=lambda: button_click(5))
109
button_6 = Button(root, text="6", padx=43, pady=20, bg="light gray", command=lambda: button_click(6))
110
button_7 = Button(root, text="7", padx=40, pady=20, bg="light gray", command=lambda: button_click(7))
111
button_8 = Button(root, text="8", padx=40, pady=20, bg="light gray", command=lambda: button_click(8))
112
button_9 = Button(root, text="9", padx=43, pady=20, bg="light gray", command=lambda: button_click(9))
113
button_0 = Button(root, text=" 0 ", padx=80, pady=20, bg="light gray", command=lambda:
114
button_click(0))
115
116
button_subtraction = Button(root, text="-", padx=41, pady=20, bg="light gray",
117
command=button_subtraction)
118
button_multiplication = Button(root, text="×", padx=40, pady=20, bg="light gray",
119
command=button_multiplication)
120
button_division = Button(root, text="÷", padx=40, pady=20, bg="light gray", command=button_division)
121
button_add = Button(root, text="+", padx=40, pady=20, bg="light gray", command=button_add)
122
123
button_equal = Button(root, text="=", padx=40, pady=20, bg="light gray", command=button_equal)
124
button_clear = Button(root, text="C", padx=40, pady=20, bg="light gray", command=button_clear)
125
126
button_decimal = Button(root, text=".", padx=44, pady=20, bg="light gray", command=lambda: print("."))
127
button_percent = Button(root, text="%", padx=41, pady=20, bg="light gray")
128
button_negative = Button(root, text="-", padx=40, pady=20, bg="light gray")
129
130
button_1.grid(row=4, column=0)
131
button_2.grid(row=4, column=1)
132
button_3.grid(row=4, column=2)
133
134
button_4.grid(row=3, column=0)
135
button_5.grid(row=3, column=1)
136
button_6.grid(row=3, column=2)
137
138
button_7.grid(row=2, column=0)
139
button_8.grid(row=2, column=1)
140
button_9.grid(row=2, column=2)
141
142
button_0.grid(row=5, column=0, columnspan=2)
143
button_clear.grid(row=1, column=0)
144
button_equal.grid(row=5, column=3)
145
146
button_subtraction.grid(row=3, column=3)
147
button_multiplication.grid(row=2, column=3)
148
button_division.grid(row=1, column=3)
149
button_add.grid(row=4, column=3)
150
151
button_decimal.grid(row=5, column=2)
152
button_percent.grid(row=1, column=2)
153
button_negative.grid(row=1, column=1)
154
155
root.mainloop()
156
Advertisement
Answer
That is beacuse you have coded it in such a way
JavaScript
1
2
1
button_decimal = Button(root, text=".", padx=44, pady=20, bg="light gray", command=lambda: print("."))
2
See, button_decimal takes a lambda that will print to the console