Changing ttk Button Height in Python

◇◆丶佛笑我妖孽 提交于 2019-11-29 14:06:23

问题


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.


回答1:


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.




回答2:


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)



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/9927386/changing-ttk-button-height-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!