#๐Ÿ”’ Error saying pygame.display isn't initialized even though it is.

89 messages ยท Page 1 of 1 (latest)

pastel thicket
#

This is the code: https://paste.pythondiscord.com/ACDA
and this is the error:

Traceback (most recent call last):
  File "/home/ddking/Python programs/NEA-Briscola/Trials.py", line 66, in <module>
    player1_first_card = Buttons.Button(20, 200, pygame.image.load(player1[0].filename).convert_alpha(), 0.25)
pygame.error: cannot convert without pygame.display initialized
neon drumBOT
#

@pastel thicket

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wet fjord
#

Are you sure it's initialized at that point?

#

you can do pygame.display.get_init() to check

#

that returns True if it's initialized

#

@pastel thicket

pastel thicket
#

in which line would i do that?

wet fjord
#

try line 65

pastel thicket
#

also i have small issue with my file trajectory... could you help me with that too

wet fjord
#

one thing at a time

pastel thicket
#

initializing it there fixed the issue

#

thanks

wet fjord
#

what?

pastel thicket
#

now its just file not found

wet fjord
#

.get_init() doesn't initialize it, just checks to see if it is initialized

pastel thicket
#

ohj

#

it doesnt?

#

i didn't get that messafe tho

#

ah nvm

#

i did

#

The error message is still displayed

wet fjord
#

Is it initialized?

pastel thicket
wet fjord
#

You have to check the return value of that method

#

ok, just do print(pygame.display.get_init())

#

That should print True or False

pastel thicket
#

True

#

It prints true until i close the file and then it prints false

wet fjord
#

close what file?

pastel thicket
#

the canvas

#

the screen

#

@wet fjord

foggy torrent
#

you should call pygame.init() at the top of your file

foggy torrent
#

oh there, between the imports

pastel thicket
#

yeh

#

the issue with the display not being intialized is fine for now

#

wait no it isnt

foggy torrent
#

oh, it's probably because of your quit

pastel thicket
#

kuje the display is there because i get a True returned

pastel thicket
foggy torrent
#

you run the .quit, but right afterwards it exits the for-event loop, and tries to create a Button again. which won't work anymore, because you uninitialized pygame

#

I recommend to put your main loop into a function, and return at that point.

def main():
    run = True
    while run:
        screen.fill((81, 51, 128))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                return  # Exit the main() function
                
        player1_first_card = Buttons.Button(20, 200, pygame.image.load(player1[0].filename).convert_alpha(), 0.25)
                  
        pygame.display.update()
  
if __name__ == '__main__':
    main()
#

(then you tecnially don't need the run variable anymore

pastel thicket
#

ohh okay ty

#

let me try it rq

foggy torrent
#

sys.exit(0) or something like that would also work, if you don't want to use a main function

pastel thicket
#

that fixed the issue of it saying it wasn't initialized thx

pastel thicket
#

can you help with something else tho?

foggy torrent
#

maybe

pastel thicket
#

even tho i specify where the image should be laoded from it doesnt load them from there

#

like the path "NEA-Brisocla etc...

#

when loading the images

#

it says the image cant be found

#

its to do with the trajectory but idk how

foggy torrent
#

oh yea, those are relative paths, and relative paths are always resolved relative to the current working directory

#

which might not be the same as the directory your script is located in

pastel thicket
foggy torrent
#

if your run the python command directly in your terimnal, then the directory mentioned there is your CWD.

pastel thicket
#

the images should be loaded from the images folder

foggy torrent
#

so it's looking for "~/Python programs/NEA-Briscola/Images/..."

pastel thicket
#

but when creating the variable i specify where to load the image from

foggy torrent
#

okay, how do you do that? what exactly are you trying to do and what doesn't work?

pastel thicket
#

two_of_clubs_image = pygame.image.load('NEA-Briscola/Images/two_of_clubs.png').convert_alpha()

three_of_clubs_image = pygame.image.load('NEA-Briscola/Images/three_of_clubs.png').convert_alpha()

four_of_clubs_image = pygame.image.load('NEA-Briscola/Images/four_of_clubs.png').convert_alpha()
foggy torrent
#

okay, and it doesn't work how exactly?

pastel thicket
#

py FileNotFoundError: No file 'three_of_clubs.png' found in working directory '/home/ddking/Python programs'.

foggy torrent
#

it's probably the Buttons.Button then?

pastel thicket
#

should i send the code for the Buttons file

#

?

foggy torrent
#

pygame.image.load(player1[0].filename)

#

that is what you're using in your other file

#

and you only get the filename

#

which is without the path

#

you probably need to prepend the folder

#

pygame.image.load("NEA-Briscola/Images/" + player1[0].filename)

#

or similar

pastel thicket
foggy torrent
#

๐Ÿ‘

pastel thicket
#

i hate these path file systems honestly

#

At least it works now,ty

#

!close

neon drumBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.