Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove space between buttons in tkinter

Tags:

python

tkinter

I'm making a calculator in tkinter and I'm trying to get the buttons to be right next to each other like this:

calculator

This is my code for the buttons so far:

Entry(root).grid(row=0, column=0, columnspan=5)

Button(root, text='C').grid(row=1, column=0, sticky='w')
Button(root, text='CE').grid(row=1, column=1, sticky='w')

Button(root, text='0').grid(row=5, column=0)

columncount = 0

for x in range(1, 4):
    Button(root, text=str()).grid(row=4, column=columncount)
    columncount += 1

columncount = 0

for x in range(4, 7):
    Button(root, text=str(x)).grid(row=3, column=columncount)
    columncount += 1

columncount = 0

for x in range(7, 10):
    Button(root, text=str(x)).grid(row=2, column=columncount)
    columncount += 1

How do I remove the space?

like image 740
Fibo36 Avatar asked Oct 15 '25 15:10

Fibo36


1 Answers

You need to request that the widgets expand to fill the space given to them. With the grid geometry manager you can control that with the sticky attribute. You give it a string containing one or more of "n", "s", "e", and "w", representing the points of a compass (north, south, east, west)

For example:

Button(root, text=str()).grid(row=4, column=columncount, sticky="nsew")

You will need to do that for every button.

If you literally want no space between the buttons, you may also need to turn of the highlight ring which is used to show which button has keyboard focus. This is controlled with the highlightthickness attribute, which defaults to 1 pixel.

Button(..., highlightthickness=0)

It's recommended not to do that, since the ring is important for being able to use the app without a mouse.

like image 110
Bryan Oakley Avatar answered Oct 17 '25 05:10

Bryan Oakley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!