如何設定 Tkinter 文字控制元件的字型

Jinku Hu 2023年1月30日
  1. 為 Tkinter Text 文字框控制元件設定字型
  2. 使用 tkFont 為 Tkinter Text 文字框控制元件設定字型
  3. Tkinter 字型系列
如何設定 Tkinter 文字控制元件的字型

Tkinter 文字框控制元件的 configure 方法指定文字框的屬性,例如文字字型 font。字型可以是元組型別,也可以是 Tkinter 的 Font 物件。

為 Tkinter Text 文字框控制元件設定字型

import tkinter as tk

root = tk.Tk()
root.geometry("400x240")

textExample = tk.Text(root, height=10)
textExample.pack()

textExample.configure(font=("Courier", 16, "italic"))

root.mainloop()

Tkinter Text_set 字型

textExample.configure(font=("Courier", 16, "italic"))

它將字型設定為 Courier,斜體字,字號為 16

使用 tkFont 為 Tkinter Text 文字框控制元件設定字型

我們還可以使用 tkFont 模組的 font 物件設定字型。

import tkinter as tk
import tkinter.font as tkFont

root = tk.Tk()
root.geometry("400x240")

textExample = tk.Text(root, height=10)
textExample.pack()

fontExample = tkFont.Font(family="Arial", size=16, weight="bold", slant="italic")

textExample.configure(font=fontExample)

root.mainloop()

Tkinter Text_set 字型與 Font 物件

fontExample = tkFont.Font(family="Arial", size=16, weight="bold", slant="italic")

Font 構造器具有以下選項:

  1. family - 字型系列,如 ArialCourier
  2. size - 字型大小(以 points 為單位)
  3. weight - 粗細,normalbold
  4. slant - 字型傾斜:romanitalic
  5. underline - 字型的下劃線,FalseTrue
  6. overstrike - 字型刪除線,FalseTrue

使用 Font 字型物件而不是字型元組型別的優點在於,可以將相同的字型物件分配給不同的控制元件,並可以使用 Font.configure 方法以程式設計方式對其進行更新。所有具有相同字型物件的控制元件都將更新為新的字型樣式。

fontExample.configure(weight="normal")

它將 fontExample 的粗細更新為正常 normal

Tkinter 字型系列

為了方便起見,我們在 Tkinter(Tkinter 3,Windows OS)中列出了所有可用的字型系列。你還可以使用以下程式碼列出工作環境中的字型系列,

import tkinter as tk
import tkinter.font as tkFont

print(list(tkFont.families()))
[
    "System",
    "Terminal",
    "Fixedsys",
    "Modern",
    "Roman",
    "Script",
    "Courier",
    "MS Serif",
    "MS Sans Serif",
    "Small Fonts",
    "Marlett",
    "Arial",
    "Arabic Transparent",
    "Arial Baltic",
    "Arial CE",
    "Arial CYR",
    "Arial Greek",
    "Arial TUR",
    "Arial Black",
    "Bahnschrift Light",
    "Bahnschrift SemiLight",
    "Bahnschrift",
    "Bahnschrift SemiBold",
    "Bahnschrift Light SemiCondensed",
    "Bahnschrift SemiLight SemiConde",
    "Bahnschrift SemiCondensed",
    "Bahnschrift SemiBold SemiConden",
    "Bahnschrift Light Condensed",
    "Bahnschrift SemiLight Condensed",
    "Bahnschrift Condensed",
    "Bahnschrift SemiBold Condensed",
    "Calibri",
    "Calibri Light",
    "Cambria",
    "Cambria Math",
    "Candara",
    "Candara Light",
    "Comic Sans MS",
    "Consolas",
    "Constantia",
    "Corbel",
    "Corbel Light",
    "Courier New",
    "Courier New Baltic",
    "Courier New CE",
    "Courier New CYR",
    "Courier New Greek",
    "Courier New TUR",
    "Ebrima",
    "Franklin Gothic Medium",
    "Gabriola",
    "Gadugi",
    "Georgia",
    "Impact",
    "Ink Free",
    "Javanese Text",
    "Leelawadee UI",
    "Leelawadee UI Semilight",
    "Lucida Console",
    "Lucida Sans Unicode",
    "Malgun Gothic",
    "@Malgun Gothic",
    "Malgun Gothic Semilight",
    "@Malgun Gothic Semilight",
    "Microsoft Himalaya",
    "Microsoft JhengHei",
    "@Microsoft JhengHei",
    "Microsoft JhengHei UI",
    "@Microsoft JhengHei UI",
    "Microsoft JhengHei Light",
    "@Microsoft JhengHei Light",
    "Microsoft JhengHei UI Light",
    "@Microsoft JhengHei UI Light",
    "Microsoft New Tai Lue",
    "Microsoft PhagsPa",
    "Microsoft Sans Serif",
    "Microsoft Tai Le",
    "Microsoft YaHei",
    "@Microsoft YaHei",
    "Microsoft YaHei UI",
    "@Microsoft YaHei UI",
    "Microsoft YaHei Light",
    "@Microsoft YaHei Light",
    "Microsoft YaHei UI Light",
    "@Microsoft YaHei UI Light",
    "Microsoft Yi Baiti",
    "MingLiU-ExtB",
    "@MingLiU-ExtB",
    "PMingLiU-ExtB",
    "@PMingLiU-ExtB",
    "MingLiU_HKSCS-ExtB",
    "@MingLiU_HKSCS-ExtB",
    "Mongolian Baiti",
    "MS Gothic",
    "@MS Gothic",
    "MS UI Gothic",
    "@MS UI Gothic",
    "MS PGothic",
    "@MS PGothic",
    "MV Boli",
    "Myanmar Text",
    "Nirmala UI",
    "Nirmala UI Semilight",
    "Palatino Linotype",
    "Segoe MDL2 Assets",
    "Segoe Print",
    "Segoe Script",
    "Segoe UI",
    "Segoe UI Black",
    "Segoe UI Emoji",
    "Segoe UI Historic",
    "Segoe UI Light",
    "Segoe UI Semibold",
    "Segoe UI Semilight",
    "Segoe UI Symbol",
    "SimSun",
    "@SimSun",
    "NSimSun",
    "@NSimSun",
    "SimSun-ExtB",
    "@SimSun-ExtB",
    "Sitka Small",
    "Sitka Text",
    "Sitka Subheading",
    "Sitka Heading",
    "Sitka Display",
    "Sitka Banner",
    "Sylfaen",
    "Symbol",
    "Tahoma",
    "Times New Roman",
    "Times New Roman Baltic",
    "Times New Roman CE",
    "Times New Roman CYR",
    "Times New Roman Greek",
    "Times New Roman TUR",
    "Trebuchet MS",
    "Verdana",
    "Webdings",
    "Wingdings",
    "Yu Gothic",
    "@Yu Gothic",
    "Yu Gothic UI",
    "@Yu Gothic UI",
    "Yu Gothic UI Semibold",
    "@Yu Gothic UI Semibold",
    "Yu Gothic Light",
    "@Yu Gothic Light",
    "Yu Gothic UI Light",
    "@Yu Gothic UI Light",
    "Yu Gothic Medium",
    "@Yu Gothic Medium",
    "Yu Gothic UI Semilight",
    "@Yu Gothic UI Semilight",
    "HoloLens MDL2 Assets",
    "BIZ UDGothic",
    "@BIZ UDGothic",
    "BIZ UDPGothic",
    "@BIZ UDPGothic",
    "BIZ UDMincho Medium",
    "@BIZ UDMincho Medium",
    "BIZ UDPMincho Medium",
    "@BIZ UDPMincho Medium",
    "Meiryo",
    "@Meiryo",
    "Meiryo UI",
    "@Meiryo UI",
    "MS Mincho",
    "@MS Mincho",
    "MS PMincho",
    "@MS PMincho",
    "UD Digi Kyokasho N-B",
    "@UD Digi Kyokasho N-B",
    "UD Digi Kyokasho NP-B",
    "@UD Digi Kyokasho NP-B",
    "UD Digi Kyokasho NK-B",
    "@UD Digi Kyokasho NK-B",
    "UD Digi Kyokasho N-R",
    "@UD Digi Kyokasho N-R",
    "UD Digi Kyokasho NP-R",
    "@UD Digi Kyokasho NP-R",
    "UD Digi Kyokasho NK-R",
    "@UD Digi Kyokasho NK-R",
    "Yu Mincho",
    "@Yu Mincho",
    "Yu Mincho Demibold",
    "@Yu Mincho Demibold",
    "Yu Mincho Light",
    "@Yu Mincho Light",
    "DengXian",
    "@DengXian",
    "DengXian Light",
    "@DengXian Light",
    "FangSong",
    "@FangSong",
    "KaiTi",
    "@KaiTi",
    "SimHei",
    "@SimHei",
    "Ubuntu",
    "Raleway",
    "Ubuntu Condensed",
    "Ubuntu Light",
]
作者: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

DelftStack.com 創辦人。Jinku 在機器人和汽車行業工作了8多年。他在自動測試、遠端測試及從耐久性測試中創建報告時磨練了自己的程式設計技能。他擁有電氣/ 電子工程背景,但他也擴展了自己的興趣到嵌入式電子、嵌入式程式設計以及前端和後端程式設計。

LinkedIn Facebook

相關文章 - Tkinter Text

相關文章 - Tkinter Font