1

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.

bahaaz
  • 45
  • 1
  • 3
  • 9

2 Answers2

1

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

sprite = pygame.image.load(filename)
screen.blit(sprite, (203,44))
joaquin
  • 82,968
  • 29
  • 138
  • 152
1

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))
Constantinius
  • 34,183
  • 8
  • 77
  • 85