在主循环期间,如何更改Tkinter应用程序的默认字体?

2024-05-14 20:11:33 发布

您现在位置:Python中文网/ 问答频道 /正文

在主循环期间,如何更改Tkinter应用程序的默认字体

我想在我的应用程序中有一个按钮,用户可以增加或减少所有小部件的字体。目前,我刚开始更改root的默认字体大小,但在应用程序运行后,我无法修改字体大小

我知道Tkinter允许您使用root.option\u 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()

Tags: textinselfmasteraddtkinter字体root
1条回答
网友
1楼 · 发布于 2024-05-14 20:11:33

最简单的方法是获取对默认字体对象的引用。当您重新配置它时,每个使用该字体的小部件都会自动调整

下面是一个人为设计的示例,包括两个按钮、一个标签和一个文本小部件。按钮和标签自动使用默认字体,文本小部件默认为其他字体,因此我们将显式将其设置为默认字体

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

默认情况下,窗口如下所示:

screenshot of initial window

这是单击“放大”按钮几次后的外观:

screenshot after zooming in

相关问题 更多 >

    热门问题