Como definir a fonte do widget de texto Tkinter

Jinku Hu 30 janeiro 2023
  1. Definir fonte para Tkinter Text Widget
  2. Define a fonte para Tkinter Text Widget com tkFont
  3. Famílias de Fontes Tkinter
Como definir a fonte do widget de texto Tkinter

O método configure do widget Tkinter Text especifica as propriedades do Text, como fonte de texto. A font pode ser do tipo tuple ou um objeto Tkinter Font.

Definir fonte para Tkinter Text Widget

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

Conjunto de texto Tkinter Fonte

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

Define a fonte para ser Courier, em itálico, com o tamanho de 16.

Define a fonte para Tkinter Text Widget com tkFont

Também poderíamos definir a fonte com um objeto font do módulo tkFont.

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 Conjunto de texto Font com objeto Font

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

O construtor de fontes tem opções como,

  1. family - família de fontes, como Arial, Courier.
  2. size - tamanho da fonte (em pontos)
  3. weight - espessura, normal ou bold.
  4. slant - inclinação da fonte: roman ou italic.
  5. underline - sublinhado da fonte, False ou True.
  6. overstrike - strikeout da fonte, False ou True.

A vantagem de utilizar o objeto Font em vez do tipo de fonte tuple é que o mesmo objeto Font poderia ser atribuído a diferentes widgets e poderia ser atualizado programmaticamente com o método Font.configure. Todos os widgets têm o mesmo objeto Font será atualizado para o novo estilo font.

fontExample.configure(weight="normal")

Ele atualiza o peso da fontExample para ser normal.

Famílias de Fontes Tkinter

Para sua conveniência, listamos todas as famílias de fontes disponíveis no Tkinter (Tkinter 3, Windows OS). Você também pode listar as famílias de fontes em seu ambiente de trabalho com os seguintes códigos,

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",
]
Autor: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Artigo relacionado - Tkinter Text

Artigo relacionado - Tkinter Font