mandaw2014
mandaw2014

Reputation: 21

How To Set The Window Icon PySDL2

I want to set the window icon in PySDL2. I tried doing this

self.icon = sdl2.ext.load_image("./assets/icon.png")
sdl2.SDL_SetWindowIcon(self.window, self.icon)

But since I'm using sdl2.ext.Window it doesn't work.

Any ideas on how I can go about doing this?

Upvotes: 0

Views: 223

Answers (2)

mandaw2014
mandaw2014

Reputation: 21

Instead of using sdl2.ext, I just imported sdl2 and sdl2.sdlimage. Here is the code if anyone is wondering:

self.icon = "./icon.png"
image = sdl2.sdlimage.IMG_Load(self.icon.encode())
sdl2.SDL_SetWindowIcon(self.window, image)
sdl2.SDL_FreeSurface(image)

Upvotes: 0

Yamm Elnekave
Yamm Elnekave

Reputation: 66

There might be a limit to the quality of the image you place as an Icon.

window being sdl2.ext.Window

image = sdl2.ext.image.load_img(path_to_image)

sdl2.SDL_SetWindowIcon(
    window.window,
    image,
)

This code was taken from rubato a pysdl2 wrapper Game Engine for python. Check it out here: https://rubato.app/.

Edit due to comments:

For your code it would look something like this:

self.icon = sdl2.ext.load_image("./assets/icon.png")
sdl2.SDL_SetWindowIcon(self.window.window, self.icon)

You only needed the actual object, stored at self.window.window. self.window is a pointer?

Upvotes: 1

Related Questions