Use ipady and ipadx to add pixels inside the button (unlike pady and padx, which add pixels outside):
my_button = ttk.Button(self, text="Hello World !")
my_button.grid(row=1, column=1, ipady=10, ipadx=10)
Answer from Benyassine Adnan on Stack OverflowUse ipady and ipadx to add pixels inside the button (unlike pady and padx, which add pixels outside):
my_button = ttk.Button(self, text="Hello World !")
my_button.grid(row=1, column=1, ipady=10, ipadx=10)
Here's an implementation of Bryan Oakley's suggestion to pack the button into a frame:
import Tkinter as tk
import ttk
class MyButton(ttk.Frame):
def __init__(self, parent, height=None, width=None, text="", command=None, style=None):
ttk.Frame.__init__(self, parent, height=height, width=width, style="MyButton.TFrame")
self.pack_propagate(0)
self._btn = ttk.Button(self, text=text, command=command, style=style)
self._btn.pack(fill=tk.BOTH, expand=1)
How do I make the buttons be square in Tkinter?
python 3.x - How can I change the width and height of a tkinter ttk Button (showing only text) in pixels? - Stack Overflow
python - tkinter button height and width - Stack Overflow
python - Is it possible to reduce a button size in tkinter? - Stack Overflow
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?
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()
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.
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')
Configuration of height is not supported in ttk.Button per Changing ttk Button Height in Python
However width is supported by ttk.Button.
I don't see where you are trying to add width in your example code, so I've taken two of the buttons (remove_button, and shuffle_button) and placed a width of 100 on remove_button.
<-SNIP->
remove_button = ttk.Button(window, text="Remove", width=100, command=remove_from_playlist)
shuffle_button = ttk.Button(window, text="Shuffle", command=shuffle_playlist)
<-SNIP->
In the future, you can see what config keys are supported for a tk widget with:
print(remove_button.config().keys())
# Supported config keys:
# dict_keys(['command', 'default', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'image', 'compound', 'padding', 'state', 'cursor', 'style',
'class'])
Additionally, for ttk widgets you can check what ttk.Style keys are supported for a specific ttk widget with:
print(ttk.Style().configure(remove_button.winfo_class()).keys())
# Supported Style config keys:
# dict_keys(['padding', 'anchor', 'width'])
where remove_button is the name of the widget you are querying. In this case, I am querying the remove_button key.
Image showing example code result
there are 2 options that you could do. ttk only has options to adjust the width for a button. if you change from ttk.Button to just Button you can set the height and width of your buttons on creation. however if you want to use ttk down in your grid instead of using padx=5, pady=10 you can use ipadx=10, ipady=30. that will set the internal padding of your buttons and can stretch and grow them as you need. but it will adjust the layout of the rest of your window and affect other items in the same columns/rows if they need to be diffent sizes
Placement:
padding=40