How do I change button size in Python Tkinter?
Tkinter Button widgets are used to create buttons that are necessary for an application. We can also add an event Object in the Button constructor and trigger it to perform some operation.
In order to customize the Button size, we can use the width and height property of the button widget.
Example
In this example, we will create some buttons with different sizes,
Output
Running the above code will display a window containing buttons of different sizes.
Как изменить размер кнопки Tkinter
Опции height и width виджета Tkinter Button задают размер создаваемой кнопки при инициализации. После инициализации все же можно было использовать метод configure для настройки опций height и width для программного изменения размера виджета Tkinter Button .
Укажите опции height и width для установки размера кнопки
height и width устанавливаются в единице текстовых единиц равными 20 . Горизонтальный текстовый блок равен ширине символа 0 , а вертикальный текстовый блок равен высоте 0 , оба в системном шрифте по умолчанию.
Полные рабочие коды
Как вы могли видеть, высота и ширина кнопки не совпадают в пикселях, хотя ее width и height установлены как 10 .
Установите width и height в пикселах кнопки Tkinter
Если нам нужно задать ширину и/или высоту виджета Tkinter Button в единице пикселей , то к Button можно добавить виртуальное невидимое изображение 1×1 пикселей. Тогда width и height будут измеряться в единицах пикселей .
Нам также нужно установить опцию compound в c или в равной степени tk.CENTER , если невидимое изображение и текст должны быть отцентрированы в кнопке. Если compound не настроен, то текст не будет отображаться в кнопке.
Измените размер кнопки после инициализации
После создания виджета Button метод configure может установить опции width и/или height для изменения размера Button .
How do I resize buttons in pixels? (Tkinter)
I am making a Tic Tac Toe game in Python Tkinter but the buttons are rectangles, and I want them to all be 100×100 pixels in size. I tried using:
(ignore the empty string and a1 ) and yet it does not resize it into a square. I have written most of the code and don’t want to use frames to resize them. What should I do?
![]()
5 Answers 5
One simple method is to give the button an invisible 1×1 pixel image. When you do that, the width and height attributes are interpreted as pixels (or more accurately, screen units, which could also mean points, inches, or centimeters).
If you do this, you can set compound to the value «c» to mean that the button should show both text and the image, with both being centered on the window.
![]()
If you are adding the button with the place method, you can use the expression:
With this, the button is created with a size of 200×30 px within its parent object at the position x = 0, y = 0
![]()
The way I would want to make sure a button is a specific size in pixels is by placing that button inside of a frame and making sure the frame is set to a specific size and the button is set to fill that frame.
This can easily be converted to work in your program and it makes sure the buttons are set to exactly 100 x 100.
This is the only way I am aware of to be sure of the size of a button in pixels. There may be other options.
First we will need 2 list. One to hold the frames and one to hold the buttons. This will allow us to store the frames and buttons in an easy way that we can work with later.
I am not sure off the top of my head on how to condense your check_win() function so I will only make a minor change to it so it will work with the lists. We need to replace each instance of a1 , a2 , a3 , and so on with its counterpart in the buttons list with the index value that is going to be created with the for loop later.
Then we need to change the process_turn() function to include the index value of each button in the buttons list so add an argument to it like this.
Finally we need to create all the buttons with the correct index in the command and we can do this with a for loop. Well 2 for loops.
The first loop will start the row count and the 2nd for loop will work the column count. This creates our 3 by 3 grid. The ndex variable is used to keep track of the index that each button needs to configure on the buttons list.
All put together you have this code that has the exact 100×100 pixel sizing you wanted.
Tkinter Button width
Tkinter Button width option sets width of the button in letters (for textual buttons) or pixels (for images). You can give only integer values for width option, be it number of lines or pixels.
In this tutorial, we will learn how to use width option to change the width of Tkinter Button.
Example 1 – Tkinter Button set Width
In the following program, we will change the width of Tkinter Button.
example.py – Python Program
Output
Conclusion
In this Python Tutorial, we learned about Tkinter Button width option and use it to change the width of a button, with the help of example Python programs.