Mia
Mia

Reputation: 2676

Tkinter button resize

From this I found how to resize a button, so I tried to execute this code.

from tkinter import * 
selection_window = Tk()
selection_window.wm_title("")
selection_window.geometry('{}x{}'.format(200, 150))
frame_1 = Frame(selection_window, width=200, height=30)
Button(frame_1, text="Single",height = 100).pack(side=LEFT,anchor=S)
Button(frame_1,text="Range",command=Toplevel,height = 20).pack(side=RIGHT,anchor=S)
frame_1.pack()
selection_window.mainloop()

But the size of button is not changed, rather, the buttons went to the center of the window. Can someone please tell me why is the problem?

screenshot

Upvotes: 3

Views: 9877

Answers (1)

Sun Bear
Sun Bear

Reputation: 8234

Button Height: If you notice, the height of frame_1 is 30 and the height of the buttons are 100 and 20. One button height is significantly taller than frame_1. So if you maximise your tk window, you will see the height difference of the buttons. Alternatively, try setting one button height to 10 and the other to 2, and rerun your script, to see the height difference. Conclusion, the button heights can be changed.

Button Lateral Placement: The lateral placement of the buttons can be controlled by using the padx=[x_left, x_right] option of the pack system. x_left and x_right denotes the horizontal external padding to be left on each side of the button in relations to it's parent. Your can read Tk documentation for a clearer explanation on the Packer's algorithm.

from tkinter import * 
selection_window = Tk()
selection_window.wm_title("")
selection_window.geometry('{}x{}'.format(200, 150))
frame_1 = Frame(selection_window, width=200, height=30)
frame_1.pack()
Button(frame_1, text="Single",height = 10).pack(side=LEFT, anchor=S, padx=[0,40])
Button(frame_1,text="Range",command=Toplevel,height = 2).pack(side=RIGHT, anchor=S, padx=[20,0])
selection_window.mainloop()

Height: Height Placement:Placement

Part 2: Per comments below, please run below script to see if changing a ttk.Button height is even possible for OSX using 'non-default' style themes and post your finding in the comment section. It worked on my Ubuntu.

from tkinter import *
import tkinter.ttk as ttk
s=ttk.Style()
print('Style themes on my system are ', s.theme_names())
s.theme_use('clam')
s.configure('bb.TButton', background='white', padding=50)
b1=ttk.Button(text='Default')
b1.pack(side=LEFT, anchor=S, padx=[0,40])
b2=ttk.Button(text='Custom', style='bb.TButton')
b2.pack(side=RIGHT, anchor=S, padx=[20,0])

padding=1padding=1 padding=40padding=40

Upvotes: 3

Related Questions