Como definir a fonte do widget de texto Tkinter
-
Definir fonte para Tkinter
Text
Widget -
Define a fonte para Tkinter
Text
Widget comtkFont
- Famílias de Fontes 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()
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()
fontExample = tkFont.Font(family="Arial", size=16, weight="bold", slant="italic")
O construtor de fontes tem opções como,
family
- família de fontes, comoArial
,Courier
.size
- tamanho da fonte (em pontos)weight
- espessura,normal
oubold
.slant
- inclinação da fonte:roman
ouitalic
.underline
- sublinhado da fonte,False
ouTrue
.overstrike
-strikeout
da fonte,False
ouTrue
.
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",
]
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 FacebookArtigo relacionado - Tkinter Text
- Como fazer com que o widget Tkinter Text seja apenas de leitura
- Como obter a entrada da caixa de texto Tkinter
- Como excluir o conteúdo da caixa de texto Tkinter
- Como definir o texto do Widget de texto Tkinter com um botão