Im trying to use a different font in pygame but every time I run it I get this syntax error message
[Traceback (most recent call last):
File "C:\Python31\", line 22, in <module>
font1 = pygame.font.Font('C:\ActionIsShaded.ttf', 12)
IOError: unable to read font filename]


I tried getting rid of the qoutes around the file name but syntax just gave me an error and didn't even run the program

Please help and thanks in advance to anyone that gives me any suggestions.

Dani AI

Generated

On Windows, your path string is the likely culprit. In Python, backslashes introduce escape sequences, so \t in C:\ActionIsShaded.ttf becomes a tab character and the file cannot be found. Use a raw string, double backslashes, or forward slashes. Also make sure you are pointing at the actual file location and that it is a TTF/OTF.

Example with safe path handling and basic checks:

import os, pygame

pygame.font.init()  # or pygame.init()

# Build a reliable path to the font next to your script
base_dir = os.path.dirname(__file__)
font_path = os.path.join(base_dir, 'ActionIsShaded.ttf')  # or r'C:\full\path\ActionIsShaded.ttf'

if not os.path.exists(font_path):
    raise FileNotFoundError(f'Font not found: {font_path}')

font1 = pygame.font.Font(font_path, 12)
label = font1.render('Hello', True, (255, 255, 255))  # antialias=True, white text

If you see "NoneType has no attribute render", it means font1 is not a Font object. Common causes: assigning the result of pygame.font.init() (which returns None) to your font variable, or a failed/overwritten assignment. Initialize Pygame first, load the font as above, then render.

When using SysFont, Pygame falls back to the default if the name is not found, which is why it "looks default." You can inspect available names with pygame.font.get_fonts() and pass one of those. For details on Python string escaping, see Python string and bytes literals.

Recommended Answers

All 7 Replies

Try this.

font1 = pygame.font.SysFont('ActionIsShaded', 12)

Try this.

font1 = pygame.font.SysFont('ActionIsShaded', 12)

IT works but it uses the default font and im trying to use a different font

Perhaps try lower case? I don't have Python on this computer so I can't test it right now

maybe I have to load it first but I dont know how to do that at all

Here is document in internet:
http://www.pygame.org/docs/ref/font.html#pygame.font.Font

SysFont looks like for system fonts, not for file:
http://www.pygame.org/docs/ref/font.html#pygame.font.SysFont

Also SysFont returns font, does not set it, if I understand correctly.
In this game it is used with render method of the font returned:
http://www.gamedev.net/community/forums/topic.asp?topic_id=444490

def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.SysFont("None", 30)
        self.text = ""
        self.center = (320, 240)
                
   def update(self):
        self.image = self.font.render(self.text, 1, (0, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.center = self.center

what does it mean if I get this back
[Traceback (most recent call last):
File "C:\Python31\", line 26, in <module>
text2 = font1.render ('Work',False,WHITE,BLUE)
AttributeError: 'NoneType' object has no attribute 'render']

Here is a good PyGame font example ...

""" customFont.py
use of a customized font with PyGame:

download custom font from:
http://www.cs.iupui.edu/~aharris/pygame/ch05/GringoNights.ttf
"""

import pygame
pygame.init()

def main():
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("display some text")
    
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    # rgb tuple (0, 0, 0) is black
    background.fill((0, 0, 0))
    
    # makes sure the font file is in the working directory
    # or give the full path name
    myFont = pygame.font.Font("GringoNights.ttf", 40)
    # rgb tuple (255, 0, 0) is red
    # rgb tuple (255, 255, 0) is yellow
    label = myFont.render("Python in the Wild West", 1, (255, 0, 0))
    
    # use clock if you want to animate something
    #clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        #clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
        
        screen.blit(background, (0, 0))
        screen.blit(label, (100, 100))
        
        pygame.display.flip()

if __name__ == "__main__":
    main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.