Reputation: 375
I want to change the fontsize in a Tkinter button. But the button size changes along with it. I even tried to limit the height and width of the button but nothing seems to work, This is the code:
from tkinter import *
root = Tk()
root.geometry("500x300")
root.resizable(False, False)
button = Button(root, text="Ihsan", bg="Black", fg="white",
activeforeground="white", activebackground="grey", width=15, height=3,
font=("ariel", "43"))
button.place(x=350, y=20)
root.mainloop()
I get a window with a huge button. Please help
Upvotes: 0
Views: 722
Reputation: 1360
The width and height is what is giving you the large button. Height in this case means 3 fontsize high. Default is that it only enclose the text.
Margins you provide with pad, either in the widget itself, or when you pack it. Using place is a bit uncommon and has its specific usecases, it does not look like you need it. The pack geometry manager can be used instead.
from tkinter import *
root = Tk()
root.geometry("500x300")
root.resizable(False, False)
button = Button(root, text="Ihsan", bg="Black", fg="white",
activeforeground="white", activebackground="grey",# width=15, height=3,
font=("ariel", "43"))
#button.place(x=350, y=20)
button.pack(side=RIGHT)
root.mainloop()
Upvotes: 0
Reputation: 13061
Width and height of the button in letters (if displaying text) or pixels (if displaying an image).
Here's one way to go, by using size of image to limit size of button when font size changed. Not sure if it is best one.
import tkinter as tk
from PIL import Image, ImageTk
def new_font():
global setting
setting = 1 - setting
font = f"ariel {32 if setting else 16}"
button.configure(font=font)
setting = 0
root = tk.Tk()
im = Image.new("RGB", (200, 200))
photo = ImageTk.PhotoImage(im)
root.geometry("300x300")
root.resizable(False, False)
button = tk.Button(
root,
text="Ihsan",
bg="Black",
fg="white",
activeforeground="white",
activebackground="grey",
width=200,
height=200,
font=("ariel", "16"),
image=photo,
compound='center')
button.place(x=30, y=20)
size = tk.Button(root, text="Size", command=new_font)
size.place(x=30, y=250)
root.mainloop()
Upvotes: 1