修改 Python Tkinter 中的默认字体
为了改变 tkinter 窗口小组件的默认行为,我们通常会重写 option_add() 方法。传递给 option_add() 方法的属性和值将反映应用程序中所有小组件的更改。因此,更改默认字体将影响应用程序中定义的所有小组件的字体。
示例
此处,我们将两个参数传递到 option_add() 方法,即 option_add("*font", "font-family font-size font-style font-orientation")。
#Import the required libraries
from tkinter import *
#Create an instance of tkinter frame
win= Tk()
#Set the geometry of frame
win.geometry("600x400")
#Change the default Font that will affect in all the widgets
win.option_add( "*font", "lucida 20 bold italic" )
win.resizable(False, False)
#Create a Label
Label(win, text="This is a New Line").pack()
Button(win, text="Button-1", width=10).pack()
win.mainloop()输出
运行以上代码将所有使用文本信息的窗口小组件的默认字体设置为“lucida 20 bold italic”。

现在,返回程序,删除以下行,然后再次运行。
win.option_add( "*font", "lucida 20 bold italic" )
文本现在将显示为默认字体 −

广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP