Regarding your initial question: the button does appear physically. The problem is, since it is so large, it is hard to distinguish from the rest of the window.
Now, you said that your ultimate goal is to change the size of a button. If so, then you are on the right track: you use the height and width options for this.
However, I would recommend that you make a few changes to your code:
- Don't make the button so huge. Even on a very big monitor, having a button be that size is way overkill.
- Don't make the window so huge. Nobody wants an application that takes up the entire screen.
- Use
.gridinstead of.place. Doing so will make it easier for you to place widgets where you want them. - Set the
heightandwidthoptions when you make the button, not after it. - There is no need to import
syshere. Only import what you need. - Don't import like this:
from tkinter import *. Doing so dumps a whole bunch of names in the global namespace that can easily be overwritten.
Here is my version of your script:
import tkinter as tk
def mmWindow():
mmWindow = tk.Tk()
mmWindow.geometry('600x600')
mWindow = tk.Tk()
# You can set any size you want
mWindow.geometry('500x500+0+0')
mWindow.title('DMX512 Controller')
wtitle = tk.Label(mWindow, text="Pi DMX", fg='blue')
wtitle.grid(row=0, column=1)
# You can set any height and width you want
mmbutton = tk.Button(mWindow, height=5, width=20, text="Main Menu", command=mmWindow)
mmbutton.grid(row=1, column=1)
mWindow.mainloop()
Answer from user2555451 on Stack OverflowRegarding your initial question: the button does appear physically. The problem is, since it is so large, it is hard to distinguish from the rest of the window.
Now, you said that your ultimate goal is to change the size of a button. If so, then you are on the right track: you use the height and width options for this.
However, I would recommend that you make a few changes to your code:
- Don't make the button so huge. Even on a very big monitor, having a button be that size is way overkill.
- Don't make the window so huge. Nobody wants an application that takes up the entire screen.
- Use
.gridinstead of.place. Doing so will make it easier for you to place widgets where you want them. - Set the
heightandwidthoptions when you make the button, not after it. - There is no need to import
syshere. Only import what you need. - Don't import like this:
from tkinter import *. Doing so dumps a whole bunch of names in the global namespace that can easily be overwritten.
Here is my version of your script:
import tkinter as tk
def mmWindow():
mmWindow = tk.Tk()
mmWindow.geometry('600x600')
mWindow = tk.Tk()
# You can set any size you want
mWindow.geometry('500x500+0+0')
mWindow.title('DMX512 Controller')
wtitle = tk.Label(mWindow, text="Pi DMX", fg='blue')
wtitle.grid(row=0, column=1)
# You can set any height and width you want
mmbutton = tk.Button(mWindow, height=5, width=20, text="Main Menu", command=mmWindow)
mmbutton.grid(row=1, column=1)
mWindow.mainloop()
import sys
from tkinter import *
def update_window_size():
mmWindow.geometry('600x600')
mmWindow = Tk()
mmWindow .geometry('1920x1080+0+0')
mmWindow .title('DMX512 Controller')
wtitle = Label(mmWindow, text="Pi DMX", fg='blue')
wtitle.place(relx=0.33, rely=0.0925925)
mmbutton = Button(mmWindow, text="Main Menu", command=update_window_size)
mmbutton.place(relw=0.104167, relh=0.185185, relx=0.104167, rely=0.185185)
mmWindow.mainloop()
I know this is late, but just want to add my method of how to solve the issue of how to make the button size change. I believe using place with relw and relh will be a better way to go. relw and relh & relx and rely will be fraction of the height and width of the parent widget. Therefore you do not need to worry about adjusting the size of both the wtitle and mmbutton.
If you want to change it's width and height from place then just put the code below on button command.
def update_button_size():
mmbutton.place(width=20, height=20)
mmbutton = Button(mmWindow, text="Main Menu", command=update_button_size)
mmbutton.place(width=400, height=400, relx=0.104167, rely=0.185185)
If you want to change it's width and height from config then use code below.
def update_button_size():
mmbutton.config(width=20, height=20)
mmbutton = Button(mmWindow, text="Main Menu", command=update_button_size)
mmbutton.place(relx=0.104167, rely=0.185185)
mmbutton.config(width=400, height=400)
From my understanding config width and height is different from place width and height.
python - How to adjust Tkinter button height? - Stack Overflow
python - Resize a button less than 1 pixel like (0.5) - Stack Overflow
python - Is it possible to reduce a button size in tkinter? - Stack Overflow
How do I make the buttons be square in Tkinter?
Videos
Here are my codes:
from tkinter import *
window = Tk ()
window.title(" ")
window.geometry('600x600')
btn = Button(window, text ="X", bg = "white")
btn.config(height = 15, width = 15)
btn.grid(column = 0, row = 0)
window.mainloop()
I set the size of the button to be btn.config(height = 15, width = 15), which is supposed to be a square.
The window is a square but the button appears like this! The button is a rectangle! Besides, there is obviously something wrong when looking at the size ratio of the button to the window (which is window.geometry('600x600')) - the button should be very small (15x15 compared to 600x600)!
What's the problem? The window is a square but the button is a rectangle.
Anyone can run the codes and tell me what they see? Maybe it happens only to my PC?
Thanks to Bryan Oakley and finally I noticed that it is impossible to change the button height with Mac, and he suggested using label as alternative which is probably the best choice the solve the problem.
Thank you again for help.
You need to specify internal padding in the pack command:
self.button.pack(ipadx=10, ipady=10)
The width and height attributes are in units of characters (eg: 1 means the size of one average character). You can specify the width and height in units of pixels if the button has an image.
A button can have both an image and text, so a simple trick is to add a one-pixel transparent pixel as an image, which will then allow you to specify the size in pixels.
The padx and pady options also contributes to the size of the button. If you are trying to get a precise size, you need to set those to zero.
Here is a simple example:
import tkinter as tk
root = tk.Tk()
root.geometry("200x100")
pixel = tk.PhotoImage(width=1, height=1)
toolbar = tk.Frame(root)
toolbar.pack(side="top")
for size in (12, 16, 24, 32):
button = tk.Button(toolbar, text=str(size), width=size, height=size,
image=pixel, compound="center", padx=0, pady=0)
button.pack(side="left")
root.mainloop()

I think you could try to use a ttk.Button and give it a ttk.Style and set the font size:
s = ttk.Style()
s.configure('my_button', font=('Helvetica', 12))
b = ttk.Button(..., style='my_button')
I tried to do it with padx and pady by setting them to the same value, not square.
I tried to do it with width and height parameters also setting them to the same value, it's still not square.
I am stuck, does anyone know why these methods are not working and which ones do?
Configuring a button (or any widget) in Tkinter is done by calling a configure method "config"
To change the size of a button called button1 you simple call
button1.config( height = WHATEVER, width = WHATEVER2 )
If you know what size you want at initialization these options can be added to the constructor.
button1 = Button(self, text = "Send", command = self.response1, height = 100, width = 100)
I've always used .place() for my tkinter widgets.
place syntax
You can specify the size of it just by changing the keyword arguments!
Of course, you will have to call .place() again if you want to change it.
Works in python 3.8.2, if you're wondering.
It's not a bug, that's just how ttk buttons work. If you need a highly configurable button, use a tkinter button. ttk buttons are less configurable on purpose. The goal of ttk widgets is to give you a set of buttons consistent with a particular theme.
Since you're on a linux system you can affect the height with pack, place or grid with appropriate options, though it's less convenient than the height option.
Use the Place() method instead over Grid() or Pack() or Config() . It will work fine. I never used Config() method
The buttons with numbers from 0-23 have a hight unit of 1.
The buttons I place in have a height height of will have different heights depending of what the user puts in, however, for some reason the units change.
For example a height unit of 4 end up not being the same as a height unit of 1?
Does someone know how to get around this, or why this is happening?
If you want these widgets to be really perfectly aligned with one another, it's definitely better to align them on the same grid and use the sticky argument to make the buttons stretch to fill their cell:

import tkinter as tk
root = tk.Tk()
chLabel = tk.Label(root, text="Channel group")
channelButtons = tk.Frame(root, bg='yellow')
ch1Button = tk.Button(channelButtons, text="CH1 Settings")
ch1Button.pack(fill='x')
ch2Button = tk.Button(channelButtons, text="CH2 Settings")
ch2Button.pack(fill='x')
ch3Button = tk.Button(channelButtons, text="CH3 Settings")
ch3Button.pack(fill='x')
ch4Button = tk.Button(channelButtons, text="CH4 Settings")
ch4Button.pack(fill='x')
trigLabel = tk.Label(root, text="Trigger group")
trigButton = tk.Button(root, text="Trigger Settings")
horizLabel = tk.Label(root, text="Horizontal group")
horizButton = tk.Button(root, text="Horizontal settings")
# Align the labels and buttons in a 2-by-3 grid
chLabel.grid(row=0, column=0, pady=10)
trigLabel.grid(row=0, column=1, pady=10)
horizLabel.grid(row=0, column=2, pady=10)
channelButtons.grid(row=1, column=0, sticky='news')
trigButton.grid(row=1, column=1, sticky='news')
horizButton.grid(row=1, column=2, sticky='news')
root.mainloop()
The root of the problem is that you aren't taking advantage of the options available to pack and grid. For example, when you do .pack(side='left'), you are leaving it up to tkinter to decide how to vertically align the widget in the space allotted.
By default tkinter will center the items vertically. Since the native height of the various sections (channel group, trigger group, horizontal group) are slightly different, it prevents them from having the tops and/or bottoms perfectly aligned.
A simple fix is to use the "fill" option to have the widgets fill the space allocated to them. If you don't want them to fill the space allotted you can use the "anchor" option to have the widgets anchored to the top.