如何在不知道元件字體系列/大小的情況下更改 Tkinter 元件的字型樣式?


Tkinter 元件支援屬性,如字體系列與字型大小,可以使用屬性font(‘Font-Family’, font-size)指定。

示例

在以下示例中,我們建立了一個文字標籤,可透過將字體系列定義為“Times New Roman”和字型大小定義為“20”來進行配置。

#Import the tkinter library
from tkinter import *

#Create an instance of tkinter frame
win = Tk()

#Set the geometry
win.geometry("650x250")

#Add a text label and add the font property to it
label= Label(win, text= "This is a New Text", font=('Times New Roman bold',20))
label.pack(padx=10, pady=10)

win.mainloop()

輸出

執行上述程式碼將顯示包含文字的視窗或框架,

更新時間:2021 年 3 月 26 日

267 次瀏覽

開啟您的職業生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.