Changing ttk Button Height in Python

后端 未结 4 735
-上瘾入骨i
-上瘾入骨i 2020-12-15 23:15

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\',

相关标签:
4条回答
  • 2020-12-15 23:22

    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)
    
    0 讨论(0)
  • 2020-12-15 23:28
    self.btn = ttk.Button(window, text="LOGIN", width=20, command=self.validate)
    self.btn.place(x=435, y=300,width=230,height=50)
    
    0 讨论(0)
  • 2020-12-15 23:30

    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

    0 讨论(0)
  • 2020-12-15 23:31

    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.

    0 讨论(0)
提交回复
热议问题