0
C:\Users\ABDUL HAIYAN\Desktop\Programming\Python\Tic Tac Toe\dist>tictactoe
    pygame 2.0.1 (SDL 2.0.14, Python 3.8.8)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    Traceback (most recent call last):
    File "tictactoe.py", line 181, in <module>
    File "tictactoe.py", line 47, in game_opening
    File "tictactoe.py", line 60, in draw_status
    File "pygame\pkgdata.pyc", line 84, in getResource
    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\ABDUL HAIYAN
    \\Desktop\\Programming\\Python\\Tic Tac Toe\\dist\\library.zip\\pygame\\freesans
    bold.ttf'

The Files already in there:

The Files already in there

Kristian
  • 2,456
  • 8
  • 23
  • 23
  • Not sure, but I don't think pygame can load things from within zip files. – Starbuck5 Mar 22 '21 at 10:12
  • Starbuck5 is right - You have to read the files [like that](https://stackoverflow.com/questions/19371860/python-open-file-in-zip-without-temporarily-extracting-it) if you want Python to see them in a `.zip` (`.zip` does not work as a folder for Python). – D_00 Mar 22 '21 at 17:54

1 Answers1

0

I recommend you to read this page to familiar you with the pygame.font module.

3 Ways to get the file:

1. Unzip the needed file.

In this case, you should open the .zip with Python, then extract the .ttf file, you open it / read it as you want, then delete the extracted font file.

If you want Python to open a file within a .zip file, you should use the ZipFile module like that:

import zipfile # needed to extract the font file
import pygame

archive = zipfile.ZipFile('files.zip', 'r') # the .zip containing your files
font_file = archive.open('font.ttf') # the font.ttf file is ready to be read
archive.close()

pygame.init()
font = pygame.font.Font(font_file, 20) # the font is initialised

... # do some stuff


2. Use pygame.font.SysFont

This module imports a font from the system fonts (i.e. the fonts available in Word, etc)

Syntax:

pygame.font.SysFont('font name', font_size)

For example:

font = pygame.font.SysFont('Comic Sans Ms', 20)

In this case you don't even need a .ttf file.


3. Unzip the folder when you run your program

Of course! Why are you zipping the folder? It literally takes a few seconds to unzip...
Or you could simply put your files in a normal folder instead of a .zip.

D_00
  • 1,440
  • 2
  • 13
  • 32
  • But i Still know Where to put that code, if i put that then it will show error – Abdul Haiyan Mar 23 '21 at 09:20
  • Which error? [`ZipFile` is in the standard library](https://docs.python.org/3/library/archiving.html) but maybe it isn't in older versions. (that could be the cause) - If you test this code alone, it works – D_00 Mar 23 '21 at 10:37
  • @AbdulHaiyan Don't use font as a [font object](https://www.pygame.org/docs/ref/font.html#pygame.font.Font) (font is actually the content of the file). - I think this is the source of the error. – D_00 Mar 24 '21 at 10:57
  • @AbdulHaiyan I edited my answer - Hope this will help you more. – D_00 Mar 24 '21 at 12:18
  • I am Testing Simple Project as I have some free time, So I will not always be free, so I will reply late, and the main thing is I have copied this source code : https://techvidvan.com/tutorials/python-game-project-tic-tac-toe/ and it works in python, when I want to convert it to exe using py2exe or pyinstaller, it show the error shown, I am a begginer and I don't know certain thing, can i put your code : pygame.font.SysFont('font name', font_size) at anywhere or specific line? – Abdul Haiyan Mar 24 '21 at 14:05
  • I recommend you to read [this article](https://pygame.readthedocs.io/en/latest/4_text/text.html) if you want to know more about fonts. The line `pygame.font.SysFont('calibri', 11)`, for example, sets a font object (here the font used is calibri, 11px tall). Put it at the beginning of your code to use this font like this for example: `screen.blit( font.render('text', 0, color)), (0, 0))` to render text on screen. – D_00 Mar 24 '21 at 15:28