Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

python - Icons are not displayed properly with pygame

The icon is not displayed, it's only displayed after closing for half a second:

screen = pygame.display.set_mode((1600, 900)) 
pygame.display.set_caption(‘Elizabeth2’) 
icon = pygame.image.load('reindeer.png') 
pygame.display.set_icon(icon) 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See pygame.display.set_icon():

[...] Some systems do not allow the window icon to change after it has been shown. This function can be called before pygame.display.set_mode() to create the icon before the display mode is set.

Set the icon before pygame.display.set_mode((1600, 900)):

icon = pygame.image.load('reindeer.png') 
pygame.display.set_icon(icon)

screen = pygame.display.set_mode((1600, 900))
pygame.display.set_caption(‘Elizabeth2’) 

In addition, the size of the icon is limited:

[...] You can pass any surface, but most systems want a smaller image around 32x32.

If the icon is not displayed, try a smaller icon.


Make sure that the resource (image) path and the working directory is correct.

The image file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python file.

The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd() and can be changed by os.chdir(path).

import os

sourceFileDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(sourceFileDir)

An alternative solution is to find the absolute path. If the image is relative to the folder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()) the image filename. e.g.:

import pygame
import os

# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))

iconPath = os.path.join(sourceFileDir, 'reindeer.png')
icon = pygame.image.load(iconPath) 
pygame.display.set_icon(icon)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...