I wrote a calculator app by using Tkinter. The problem is when I click numbers or operators etc, the whole GUI resized automatically. Actually, I think the width of the label must fix in some way. I tried to use a frame as the parent for the label but it didn’t work. Should I use a different layout manager?
Here is my code:
JavaScript
x
210
210
1
from tkinter import *
2
3
root = Tk()
4
root.title('A Simple Calculator')
5
#root.geometry('500x600')
6
root.resizable(width=False, height=False)
7
8
9
#Screen of the calculator
10
isPushed = StringVar()
11
#print(isPushed.get() == '')
12
var = StringVar()
13
#screenFrame = Frame(root)
14
screenLabel = Label(root, text='', padx=150, pady=13, bg='black',
15
fg='white', font=('Times', 15), textvariable=var)
16
17
#define commands
18
#numbers
19
def one():
20
if isPushed.get() == '':
21
s = var.get()
22
var.set(s + '1')
23
else:
24
var.set('')
25
var.set('1')
26
isPushed.set('')
27
def two():
28
if isPushed.get() == '':
29
s = var.get()
30
var.set(s + '2')
31
else:
32
var.set('')
33
var.set('2')
34
isPushed.set('')
35
def three():
36
if isPushed.get() == '':
37
s = var.get()
38
var.set(s + '3')
39
else:
40
var.set('')
41
var.set('3')
42
isPushed.set('')
43
def four():
44
if isPushed.get() == '':
45
s = var.get()
46
var.set(s + '4')
47
else:
48
var.set('')
49
var.set('4')
50
isPushed.set('')
51
def five():
52
if isPushed.get() == '':
53
s = var.get()
54
var.set(s + '5')
55
else:
56
var.set('')
57
var.set('5')
58
isPushed.set('')
59
def six():
60
if isPushed.get() == '':
61
s = var.get()
62
var.set(s + '6')
63
else:
64
var.set('')
65
var.set('6')
66
isPushed.set('')
67
def seven():
68
if isPushed.get() == '':
69
s = var.get()
70
var.set(s + '7')
71
else:
72
var.set('')
73
var.set('7')
74
isPushed.set('')
75
def eight():
76
if isPushed.get() == '':
77
s = var.get()
78
var.set(s + '8')
79
else:
80
var.set('')
81
var.set('8')
82
isPushed.set('')
83
def nine():
84
if isPushed.get() == '':
85
s = var.get()
86
var.set(s + '9')
87
else:
88
var.set('')
89
var.set('9')
90
isPushed.set('')
91
def zero():
92
if isPushed.get() == '':
93
s = var.get()
94
var.set(s + '0')
95
else:
96
var.set('')
97
var.set('0')
98
isPushed.set('')
99
100
#operations
101
def add():
102
s = var.get()
103
var.set(' {} + '.format(s))
104
isPushed.set('')
105
def subtract():
106
s = var.get()
107
var.set(' {} - '.format(s))
108
isPushed.set('')
109
def multiply():
110
s = var.get()
111
var.set(' {} {} '.format(s, chr(215)))
112
isPushed.set('')
113
def divide():
114
s = var.get()
115
var.set(' {} {} '.format(s, chr(247)))
116
isPushed.set('')
117
def calculate():
118
isPushed.set('y')
119
s = var.get()
120
s = s.split()
121
#exclude operands
122
if '.' in s[0] or '.' in s[2]:
123
x = float(s[0])
124
y = float(s[2])
125
else:
126
x = int(s[0])
127
y = int(s[2])
128
#make the operation
129
if s[1] == '+':
130
var.set(x + y)
131
elif s[1] == '-':
132
var.set(x - y)
133
elif s[1] == chr(215):
134
var.set(x * y)
135
else:
136
var.set(x / y if x % y != 0 else x // y)
137
138
139
140
#functional Buttons
141
def clean():
142
var.set('')
143
def decimal_point():
144
s = var.get()
145
var.set('{}.'.format(s))
146
147
148
149
150
#define Buttons
151
#numbers
152
num_1 = Button(root, text='1', font=('Times', 20), command=one)
153
num_2 = Button(root, text='2', font=('Times', 20), command=two)
154
num_3 = Button(root, text='3', font=('Times', 20), command=three)
155
num_4 = Button(root, text='4', font=('Times', 20), command=four)
156
num_5 = Button(root, text='5', font=('Times', 20), command=five)
157
num_6 = Button(root, text='6', font=('Times', 20), command=six)
158
num_7 = Button(root, text='7', font=('Times', 20), command=seven)
159
num_8 = Button(root, text='8', font=('Times', 20), command=eight)
160
num_9 = Button(root, text='9', font=('Times', 20), command=nine)
161
num_0 = Button(root, text='0', font=('Times', 20), command=zero)
162
163
#operations
164
addition = Button(root, text='+', font=('Times', 20), command=add)
165
subtraction = Button(root, text='-', font=('Times', 20), command=subtract)
166
multiplication = Button(root, text=chr(215), font=('Times', 20), command=multiply)
167
division = Button(root, text=chr(247), font=('Times', 20), command=divide)
168
equal = Button(root, text='=', font=('Times', 20), command=calculate)
169
170
#functional Buttons
171
clear = Button(root, text='C', font=('Times', 20), command=clean)
172
period = Button(root, text='.', font=('Times', 20), command=decimal_point)
173
174
#Packing Buttons into the form
175
#Screen of the calculator
176
#screenFrame.grid(row=0, column=0, columnspan=4, sticky=W+E)
177
#screenLabel.pack()
178
screenLabel.grid(row=0, column=0, columnspan=4, sticky=W+E)
179
180
#numbers
181
num_1.grid(row=1, column=0, sticky=W+E)
182
num_2.grid(row=1, column=1, sticky=W+E)
183
num_3.grid(row=1, column=2, sticky=W+E)
184
185
num_4.grid(row=2, column=0, sticky=W+E)
186
num_5.grid(row=2, column=1, sticky=W+E)
187
num_6.grid(row=2, column=2, sticky=W+E)
188
189
num_7.grid(row=3, column=0, sticky=W+E)
190
num_8.grid(row=3, column=1, sticky=W+E)
191
num_9.grid(row=3, column=2, sticky=W+E)
192
193
num_0.grid(row=4, column=0, columnspan=2, sticky=W+E)
194
195
#operations
196
addition.grid(row=1, column=3, sticky=W+E)
197
subtraction.grid(row=2, column=3, sticky=W+E)
198
multiplication.grid(row=3, column=3, sticky=W+E)
199
division.grid(row=4, column=3, sticky=W+E)
200
equal.grid(row=5, column=3, sticky=W+E)
201
202
#functional Buttons
203
clear.grid(row=5, column=0, columnspan=3, sticky=W+E)
204
period.grid(row=4, column=2, sticky=W+E)
205
206
207
208
209
root.mainloop()
210
Advertisement
Answer
The widget is resizing because you don’t give the widget an explicit size, so it grows to fit its contents. That is its defined behavior. Instead, you should specify the size that you want it to be. When you do that, it won’t grow when you add more characters to it than will fit.
JavaScript
1
3
1
screenLabel = Label(root, text='', pady=13, bg='black', width=20,
2
fg='white', font=('Times', 15), textvariable=var)
3