14

This seems like a silly question, but is it possible to change the height of a ttk button manually?

Something like button = tkinter.Button(frame, text='hi', width=20, height=20...) works just fine for a tkinter button. Though I'd prefer to use a ttk button since it looks much better aesthetically.

button = ttk.Button(frame, text='hi', width=20, height=20...) does not work, height doesn't seem to be a valid option. I've tried setting it with config or looking for elements in the style to change and haven't had any luck.

Is there a simple solution to this? I'm using Python 2.7, Windows for the record. Sorry, this seems like kind of a trivial questions but I've looked around without much luck.

Chris J
  • 30,688
  • 6
  • 69
  • 111
user1301039
  • 143
  • 1
  • 1
  • 5

5 Answers5

20

This worked for me:

my_button = ttk.Button(self, text="Hello World !")
my_button.grid(row=1, column=1, ipady=10, ipadx=10)

where ipady and ipadx adds pixels inside the button unlike pady and padx which adds pixels outside of the button

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
9

Just an example, as @Bryan said, "For example, you can pack the button into a frame", I did it like this:

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)
Pedru
  • 1,430
  • 1
  • 14
  • 32
7

To directly answer your question, no, you can't do this. The whole point of themed buttons is to provide a uniform size.

That being said, there's plenty of room for out-of-the-box thinking. For example, you can pack the button into a frame, turn geometry propagation off on the frame (so the size of the frame controls the size of the button rather than visa versa), then set the size of the frame to whatever you want.

Or, try putting a transparent image on the button that is the height you desire, then use the compound option to overlay the label over the invisible image.

Or, create a custom theme that uses padding to get the size you want.

Finally, you can put the button in a grid, have it "sticky" to all sides, then set a min-height for that row.

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

PotatoBeenCrafted
  • 302
  • 1
  • 6
  • 14
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Great, those all seem like potential good work arounds. Thank you. – user1301039 Mar 30 '12 at 14:00
  • Will the disabling of geometry propagation and sticking into a frame work with grid, too? I tried it once, but couldn't get it to play nice. Might need to take a second try. – Kumba Jul 05 '13 at 04:30
  • @Kumba: grid and pack are no different in this regard -- when you turn geometry propagation off, the size of the child widget does not affect the size of the containing widget. – Bryan Oakley Jul 05 '13 at 11:35
  • I wonder why height is not a valid option anymore (in ttk) but width still is? – Pedru Aug 06 '15 at 14:48
  • @Pedru: because Apple has strict guidelines about how buttons look on that OS. – Bryan Oakley Aug 06 '15 at 15:58
  • @BryanOakley: so everybody must suffer because of the fruit – Pedru Aug 07 '15 at 06:32
  • @Pedru: I'm not sure I understand your comments. Each platform has its own requirements and limitations. If you want configurability use the standard tk buttons, and if you want buttons more consistent with the platform use ttk widgets. – Bryan Oakley Aug 07 '15 at 10:38
5
self.btn = ttk.Button(window, text="LOGIN", width=20, command=self.validate)
self.btn.place(x=435, y=300,width=230,height=50)
Arnab Das
  • 149
  • 3
  • 3
0

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.