这是我正在做的课程中的锅炉板代码,当我运行这个程序时,出现了这个错误。
C:\Users\Tanish\Desktop\Coding\cs50 ai>"C:/Program Files/Python39/python.exe"
"c:/Users/Tanish/Desktop/Coding/cs50 ai/tictactoe/runner.py"
pygame 2.0.1 (SDL 2.0.14, Python 3.9.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last)
File "c:\Users\Tanish\Desktop\Coding\cs50 ai\tictactoe\runner.py", line 18, in <module>
mediumFont = pygame.font.Font("OpenSans-Regular.ttf", 28)
FileNotFoundError: [Errno 2] No such file or directory: 'OpenSans-Regular.ttf'虽然我在同一个目录中有字体.ttf文件。
代码:
import pygame
import sys
import time
import pygame
import tictactoe as ttt
pygame.init()
size = width, height = 600, 400
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode(size)
mediumFont = pygame.font.Font("OpenSans-Regular.ttf", 28)
largeFont = pygame.font.Font("OpenSans-Regular.ttf", 40)
moveFont = pygame.font.Font("OpenSans-Regular.ttf", 60)目录:

发布于 2021-02-07 03:32:50
这是因为您正在运行文件的位置。
目前,您正在C:\Users\Tanish\Desktop\Coding\cs50 ai上运行它。但是,您的.tff文件位于c:/Users/Tanish/Desktop/Coding/cs50 ai/tictactoe/中。
在pygame.font.Font("OpenSans-Regular.ttf", 28)中,您告诉程序从当前目录打开文件,当然该目录不存在。
一个解决方案是找到文件的绝对路径:
import os, sys
ttf_path = os.path.join(sys.path[0], "OpenSans-Regular.ttf")
pygame.font.Font(ttf_path, 28)或者,您可以在tictactoe/目录中运行代码。
https://stackoverflow.com/questions/66084187
复制相似问题