Set Chinese font in Python

Set Chinese font in Python

It is very common to process Chinese strings in Python, but by default, Chinese display may not be supported in Python's console and graphical interface. In order to solve this problem, we need to set Chinese fonts in the program.

Generally speaking, Tkinter is used in Python as a graphical interface toolkit. Tkinter supports changing the font and font size. However, by default, only English fonts and font sizes can be used. If you want to use Chinese fonts, you need to install Chinese fonts first.

Install Chinese fonts

We can install Chinese fonts in the following ways:

# 安装思源宋体
!apt-get install fonts-noto-cjk

Or manually put the font file in .ttf format /usr/share/fonts/truetypeinto the directory.

Set Chinese font

After completing the installation of Chinese fonts, we can use the following code to set Chinese fonts:

import tkinter as tk
from tkinter.font import Font

# 创建主窗口
r

Guess you like

Origin blog.csdn.net/Jack_user/article/details/130351827