亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在主循環期間更改 Tkinter 應用程序的默認字體?

如何在主循環期間更改 Tkinter 應用程序的默認字體?

拉莫斯之舞 2023-09-05 17:18:19
如何在主循環期間更改 Tkinter 應用程序的默認字體?我想在我的應用程序中有一個按鈕,用戶可以在其中增加或減少所有小部件的字體。目前,我只是在一開始就更改了 root 的默認字體大小,但在應用程序運行后我無法修改字體大小。我知道 Tkinter 允許您使用 root.option_add("*Font", font) 更改默認值,但是如何在應用程序運行時修改字體?這是我正在嘗試做的一個例子:import tkinter as tkclass 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()
查看完整描述

1 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

最簡單的方法是獲取對默認字體對象的引用。當您重新配置它時,使用該字體的每個小部件都會自動調整。


這是一個人為的示例,其中包括兩個按鈕、一個標簽和一個文本小部件。按鈕和標簽自動使用默認字體,文本小部件默認使用不同的字體,因此我們將顯式將其設置為默認字體。


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

默認情況下,該窗口如下所示:

https://img1.sycdn.imooc.com//64f6f29c0001358603950317.jpg

這是單擊“放大”按鈕幾次后的樣子:

https://img1.sycdn.imooc.com//64f6f2a70001e89703960318.jpg


查看完整回答
反對 回復 2023-09-05
  • 1 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號