Search code examples
pythontkinterttk

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?


Solution

  • 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.