when scaling the size of the font in python the size of the button changes

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.

enter image description here

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()

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top