如何在主循环期间更改 Tkinter 应用程序的默认字体?
How can I change the default font of a Tkinter application during the mainloop?
如何在主循环期间更改 Tkinter 应用程序的默认字体?
我想在我的应用程序中有一个按钮,用户可以在其中增加或减少所有小部件的字体。目前我刚开始只是修改root的默认字体大小,但是申请到运行.
后一直无法修改字体大小
我知道 Tkinter 允许您使用 root.option_add("*Font", font) 更改默认值,但是如何在应用程序处于 运行 时修改字体?
这是我正在尝试做的一个例子:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master, bg= "#E3E5E6")
self.master = master
self.grid(sticky = "NESW")
self.menubar = tk.Menu(self)
fileMenu = tk.Menu(self.menubar)
fileMenu.add_command(label="Change Font Size", command =self.changeFontSize)
self.menubar.add_cascade(label = "Click Me", menu = fileMenu)
self.master.configure(menu=self.menubar)
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
self.messageWidgetExample = tk.Message(text = text)
self.messageWidgetExample.grid()
def changeFontSize(self):
print("Change Font Size")
self.master.option_add("*Font", "calibri 8")
self.master.update()
# Doesn't work either
# self.messageWidgetExample.update()
root = tk.Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.option_add("*Font", "calibri 14")
app = Application(master=root)
app.mainloop()
最简单的方法是获取对默认字体对象的引用。当您重新配置它时,每个使用该字体的小部件都会自动调整。
这是一个人为设计的示例,其中包括两个按钮、一个标签和一个文本小部件。按钮和标签自动使用默认字体,文本小部件默认使用不同的字体,因此我们将其明确设置为默认字体。
import tkinter as tk
from tkinter import font
def zoom_in():
size = default_font.cget("size")
default_font.configure(size=size+2)
def zoom_out():
size = default_font.cget("size")
default_font.configure(size=max(size-2, 8))
root = tk.Tk()
root.geometry("400x300")
default_font = tk.font.nametofont("TkDefaultFont")
toolbar = tk.Frame(root)
# start small, but then expand to fill the window.
# Doing this, and fixing the size of the window will
# prevent the window from growing or shrinking when
# we change the font.
text = tk.Text(root, width=1, height=1, font=default_font)
print(text.cget("font"))
toolbar.pack(side="top", fill="x", ipady=4)
text.pack(side="bottom", fill="both", expand=True)
zoom_in = tk.Button(toolbar, text="Zoom In", command=zoom_in)
zoom_out = tk.Button(toolbar, text="Zoom Out", command=zoom_out)
label = tk.Label(toolbar, text="Change font size:")
label.pack(side="left")
zoom_in.pack(side="left")
zoom_out.pack(side="left")
text.insert("end", "Hello, world")
root.mainloop()
默认情况下,window 如下所示:
这是点击“放大”按钮几次后的样子:
如何在主循环期间更改 Tkinter 应用程序的默认字体?
我想在我的应用程序中有一个按钮,用户可以在其中增加或减少所有小部件的字体。目前我刚开始只是修改root的默认字体大小,但是申请到运行.
后一直无法修改字体大小我知道 Tkinter 允许您使用 root.option_add("*Font", font) 更改默认值,但是如何在应用程序处于 运行 时修改字体?
这是我正在尝试做的一个例子:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master, bg= "#E3E5E6")
self.master = master
self.grid(sticky = "NESW")
self.menubar = tk.Menu(self)
fileMenu = tk.Menu(self.menubar)
fileMenu.add_command(label="Change Font Size", command =self.changeFontSize)
self.menubar.add_cascade(label = "Click Me", menu = fileMenu)
self.master.configure(menu=self.menubar)
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
self.messageWidgetExample = tk.Message(text = text)
self.messageWidgetExample.grid()
def changeFontSize(self):
print("Change Font Size")
self.master.option_add("*Font", "calibri 8")
self.master.update()
# Doesn't work either
# self.messageWidgetExample.update()
root = tk.Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.option_add("*Font", "calibri 14")
app = Application(master=root)
app.mainloop()
最简单的方法是获取对默认字体对象的引用。当您重新配置它时,每个使用该字体的小部件都会自动调整。
这是一个人为设计的示例,其中包括两个按钮、一个标签和一个文本小部件。按钮和标签自动使用默认字体,文本小部件默认使用不同的字体,因此我们将其明确设置为默认字体。
import tkinter as tk
from tkinter import font
def zoom_in():
size = default_font.cget("size")
default_font.configure(size=size+2)
def zoom_out():
size = default_font.cget("size")
default_font.configure(size=max(size-2, 8))
root = tk.Tk()
root.geometry("400x300")
default_font = tk.font.nametofont("TkDefaultFont")
toolbar = tk.Frame(root)
# start small, but then expand to fill the window.
# Doing this, and fixing the size of the window will
# prevent the window from growing or shrinking when
# we change the font.
text = tk.Text(root, width=1, height=1, font=default_font)
print(text.cget("font"))
toolbar.pack(side="top", fill="x", ipady=4)
text.pack(side="bottom", fill="both", expand=True)
zoom_in = tk.Button(toolbar, text="Zoom In", command=zoom_in)
zoom_out = tk.Button(toolbar, text="Zoom Out", command=zoom_out)
label = tk.Label(toolbar, text="Change font size:")
label.pack(side="left")
zoom_in.pack(side="left")
zoom_out.pack(side="left")
text.insert("end", "Hello, world")
root.mainloop()
默认情况下,window 如下所示:
这是点击“放大”按钮几次后的样子: