如何在 Tkinter 中动态调整按钮文本的大小?
先决条件: Python GUI – tkinter,使用 Tkinter 调整窗口大小时动态调整按钮大小
在本文中,我们将看到如何使按钮文本大小动态化。动态意味着每当按钮大小发生变化时,按钮文本大小也会发生变化。在 Tkinter 中没有内置函数,它会动态更改按钮文本大小。
方法:
- 创建按钮并将粘性设置为所有方向
- 设置绑定,绑定会做什么,每当按钮大小改变时,它会调用我们稍后创建的调整大小函数。
- 在 resize函数内部,我们将有不同的条件,取决于主窗口的几何形状/大小。
- 设置行列配置
让我们通过一步一步的实现来理解:
第 1 步:创建一个普通的 Tkinter 窗口。
Python3
# Import module
from tkinter import *
# Create object
root = Tk()
# Adjust size
root.geometry("400x400")
# Execute tkinter
root.mainloop()
Python3
# Import module
from tkinter import *
# Create object
root = Tk()
# Adjust size
root.geometry("400x400")
# Create Buttons
button_1 = Button(root , text = "Button 1")
# Set grid
button_1.grid(row = 0,column = 0)
# Execute tkinter
root.mainloop()
Python3
# resize button text size
def resize(e):
# get window width
size = e.width/10
# define text size on diffrent condition
# if window height is greater
# than 300 and less than 400 (set font size 40)
if e.height <= 400 and e.height > 300:
button_1.config(font = ("Helvetica", 40))
# if window height is greater than
# 200 and less than 300 (set font size 30)
elif e.height < 300 and e.height > 200:
button_1.config(font = ("Helvetica", 30))
# if window height is less than 200 (set font size 40)
elif e.height < 200:
button_1.config(font = ("Helvetica", 40))
Python3
# Import module
from tkinter import *
# Create object
root = Tk()
# Adjust size
root.geometry("400x400")
# Specify Grid
Grid.columnconfigure(root, index = 0,
weight = 1)
Grid.rowconfigure(root, 0,
weight = 1)
# Create Buttons
button_1 = Button(root, text = "Button 1")
# Set grid
button_1.grid(row = 0,
column = 0, sticky = "NSEW")
# resize button text size
def resize(e):
# get window width
size = e.width/10
# define text size on diffrent condition
# if window height is greater
# than 300 and less than 400 (set font size 40)
if e.height <= 400 and e.height > 300:
button_1.config(font = ("Helvetica", 40))
# if window height is greater than
# 200 and less than 300 (set font size 30)
elif e.height < 300 and e.height > 200:
button_1.config(font = ("Helvetica", 30))
# if window height is less
# than 200 (set font size 40)
elif e.height < 200:
button_1.config(font = ("Helvetica", 40))
# it will call resize function
# when window size will change
root.bind('', resize)
# Execute tkinter
root.mainloop()
输出:
第 2 步:在主窗口内创建一个按钮。
蟒蛇3
# Import module
from tkinter import *
# Create object
root = Tk()
# Adjust size
root.geometry("400x400")
# Create Buttons
button_1 = Button(root , text = "Button 1")
# Set grid
button_1.grid(row = 0,column = 0)
# Execute tkinter
root.mainloop()
输出:
第 3 步:调整按钮文本大小
在 resize函数中,“e”值会告诉主窗口的宽度和高度。
蟒蛇3
# resize button text size
def resize(e):
# get window width
size = e.width/10
# define text size on diffrent condition
# if window height is greater
# than 300 and less than 400 (set font size 40)
if e.height <= 400 and e.height > 300:
button_1.config(font = ("Helvetica", 40))
# if window height is greater than
# 200 and less than 300 (set font size 30)
elif e.height < 300 and e.height > 200:
button_1.config(font = ("Helvetica", 30))
# if window height is less than 200 (set font size 40)
elif e.height < 200:
button_1.config(font = ("Helvetica", 40))
下面是完整的实现:
蟒蛇3
# Import module
from tkinter import *
# Create object
root = Tk()
# Adjust size
root.geometry("400x400")
# Specify Grid
Grid.columnconfigure(root, index = 0,
weight = 1)
Grid.rowconfigure(root, 0,
weight = 1)
# Create Buttons
button_1 = Button(root, text = "Button 1")
# Set grid
button_1.grid(row = 0,
column = 0, sticky = "NSEW")
# resize button text size
def resize(e):
# get window width
size = e.width/10
# define text size on diffrent condition
# if window height is greater
# than 300 and less than 400 (set font size 40)
if e.height <= 400 and e.height > 300:
button_1.config(font = ("Helvetica", 40))
# if window height is greater than
# 200 and less than 300 (set font size 30)
elif e.height < 300 and e.height > 200:
button_1.config(font = ("Helvetica", 30))
# if window height is less
# than 200 (set font size 40)
elif e.height < 200:
button_1.config(font = ("Helvetica", 40))
# it will call resize function
# when window size will change
root.bind('', resize)
# Execute tkinter
root.mainloop()
输出: