Python - Font()

The Font() function in Python is actually part of the Tkinter graphical user interface library and is used to create font objects. Tkinter supports creating font objects using font names and sizes, but the exact fonts available depend on your operating system and installed fonts:

1. "Helvetica" - also known as "Arial", is a common sans serif font.
2. "Times" - Similar to "Times New Roman", it is a common serif font.
3. "Courier" - Similar to "Courier New", it is a fixed-width font.
4. "Verdana" - A sans serif font commonly used in web design.
5. "Tahoma" - A sans serif font, similar to "Verdana".
6. "Georgia" - A serif font commonly used in print and web design.

import tkinter as tk

root = tk.Tk()
my_font = tk.Font(family="Helvetica", size=12, weight="bold")

# 使用字体对象
label = tk.Label(root, text="Hello, World!", font=my_font)
label.pack()

root.mainloop()

Font availability may vary depending on the operating system and installed fonts.

Pygame will provide some common fonts:

1. "arial" - Arial font.
2. "couriernew" - Courier New font is a fixed-width font.
3. "timesnewroman" - Times New Roman font, is a common serif font.
4. "calibri" - Calibri font, commonly used in Microsoft Office documents.
5. "comicssansms" - Comic Sans MS font, a cartoon-style font.
6. "impact" - Impact font, a bold font often used in title and logo design.

import pygame

pygame.init()

# 创建一个使用Arial字体的字体对象
font = pygame.font.Font(None, 36)  # 使用默认字体文件,大小36

# 创建文本
text = font.render("Hello, Pygame!", True, (255, 255, 255))

# 创建显示窗口
screen = pygame.display.set_mode((400, 300))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 渲染文本到屏幕
    screen.fill((0, 0, 0))
    screen.blit(text, (100, 100))
    pygame.display.flip()

pygame.quit()

Font availability varies depending on the operating system and installed font libraries.

Pygame supports Chinese by default, but you need to make sure to use a font file that contains the Chinese character set when rendering Chinese text. The path to a TrueType font file containing Chinese characters can be specified in the font parameter to render Chinese text in Pygame.

You can download some TrueType font files containing Chinese characters and then use them as parameters of the font object:

import pygame
import os

pygame.init()

# 指定包含中文字符的TrueType字体文件的路径
font_path = os.path.join("path_to_your_font_folder", "chinese_font.ttf")

# 创建一个使用中文字体的字体对象,设置字体大小
font = pygame.font.Font(font_path, 36)

# 创建中文文本
text = font.render("你好,Pygame!", True, (255, 255, 255))

# 创建显示窗口
screen = pygame.display.set_mode((400, 300))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 渲染文本到屏幕
    screen.fill((0, 0, 0))
    screen.blit(text, (100, 100))
    pygame.display.flip()

pygame.quit()

Guess you like

Origin blog.csdn.net/Alexandra_Zero/article/details/132830175