Question

I am trying to use the

tkFileDialog.askopenfilename

to load an image and then blit it on screen, but it isn't working out. I created a Rect button so that when I click it it would open the dialogue box to load an image. After loading it I want it to blit but it is not working.(error). Here is my code:

if loadrect.collidepoint(mx,my) and mb[0]==1:
            filename = tkFileDialog.askopenfilename(filetypes=Formats,title="Choose an image to open")
            if filename!= None:
                screen.blit(filename,(203,44))

How would I correct this?

Thanks.

Was it helpful?

Solution

Using pygame, you have to load the image first, just passing its filename is not enough (http://www.pygame.org/docs/ref/image.html#pygame.image.load):

if loadrect.collidepoint(mx,my) and mb[0]==1:
    filename = tkFileDialog.askopenfilename(filetypes=Formats,title="Choose an image to open")
    if filename!= None:
        surface = pygame.image.load(filename)
        screen.blit(surface,(203,44))

OTHER TIPS

You should first convert your image to a pygame object. For example using:

sprite = pygame.image.load(filename)
screen.blit(sprite, (203,44))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top