[Tutor] Opening a window to fit an image
James Newton
jnewton at fuelindustries.com
Thu Jan 10 17:23:17 CET 2008
Hi Python Pros,
I want to create a window to fit an image that I read in on the fly.
I'm using pygame to load images and to create the window. I find I have
to use pygame.display.set_mode() twice: once to allow me to use
pygame.image.loadfile(), and once to reset the window size after I have
been able to determine the size of the loaded image.
Does this have any undesired side-effects? Is there a better way of
doing this?
Here's the relevant extract of my script.
import os
import pygame
def main():
pygame.init()
# You must use display.set_mode() before you can use image.load
# If you don't, you get an "error: No video mode has been set"
screen = pygame.display.set_mode((1, 1))
# Now we can load an image file and find out its size
vFile = 'image.bmp' # in same folder as script
background = pygame.image.load(vFile).convert()
vRect = background.get_rect()
# Reset the display mode to fit the image
screen = pygame.display.set_mode((vRect[2], vRect[3]))
# Show the image in the window
screen.blit(background, (0, 0))
pygame.display.update()
# <SNIP: code to make application exit gracefully>
main()
Thanks in advance for your help,
James
More information about the Tutor
mailing list