user1301039
user1301039

Reputation: 153

How can I control the height of a ttk.Button?

With Tkinter, I can set the height of a button with something like
button = tkinter.Button(frame, text='hi', width=20, height=20...).

But in my program, I want to use a ttk button.
button = ttk.Button(frame, text='hi', width=20, height=20...) doesn't work - height doesn't seem to be a valid option. I couldn't find a way to set the height using config or by changing style elements, either.

How can I set the height of a ttk button explicitly?

Upvotes: 15

Views: 36659

Answers (5)

Bryan Oakley
Bryan Oakley

Reputation: 386020

The whole point of themed buttons is to provide a uniform size, so it can't be done directly. However, there's plenty of room for out-of-the-box thinking. For example:

  • Pack the button into a frame, turn off geometry propagation for the frame (so that the size of the frame controls the size of the button, rather than vice-versa); then set the size of the frame as desired.

  • Use a transparent image for the button's label which has the height needed to control the button's height; then use the compound option to overlay a text label.

  • Create a custom theme that uses padding to get the size you want.

  • Put the button in a grid, have it "sticky" to all sides, then set a minimum height for that row.

Of course, if you are on OSX all bets are off -- it really wants to make buttons a specific size.

Upvotes: 9

Pedru
Pedru

Reputation: 1450

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)

Upvotes: 10

Benyassine Adnan
Benyassine Adnan

Reputation: 251

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)

Upvotes: 21

Jacobo Córdova
Jacobo Córdova

Reputation: 136

Ok, according to the answer, there are many tricks, so it works to me on Macos

start = ttk.Button(frame_2, text = "\nStart\n", width = 30).place(x = 710, y = 600)

Put \n lines before and after the text make the button bigger.

Upvotes: -1

Arnab Das
Arnab Das

Reputation: 157

self.btn = ttk.Button(window, text="LOGIN", width=20, command=self.validate)
self.btn.place(x=435, y=300,width=230,height=50)

Upvotes: 4

Related Questions