Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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

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)
self.btn = ttk.Button(window, text="LOGIN", width=20, command=self.validate)
self.btn.place(x=435, y=300,width=230,height=50)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top