我想在Tkinter按钮中更改fontsize。但是按钮的大小会随之变化。我什至试图限制按钮的高度和宽度,但似乎没有任何效果,这是代码:
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()
我得到一个带有大按钮的窗口。请帮忙
按钮的宽度和高度,以字母(如果显示文本)或像素(如果显示图像)为单位。
这是一种方法,当字体大小更改时,通过使用图像大小来限制按钮的大小。不确定是否最好。
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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句