pythonwindowpysdl2

Updating Window position in PySDL2 (Help!)


I'm currently trying to change the position of a Window I created in PySDL2, after it has been already rendered.

I've tried updating the Window.position attribute of the Window. But despite doing this, and having the surface refresh itself, no change is visible. (The window stays where it was originally drawn).

I know I can change the window position though, because if I change the position in the window's creation line, it will change when it's initially drawn on the screen. (You just don't seem to be able to change it after)

Code:

import sdl2
import sdl2.ext
import sys

White = sdl2.ext.Color(255,255,255)
Red = sdl2.ext.Color(153,0,0)

class Background(sdl2.ext.SoftwareSpriteRenderSystem):
    def __init__(self,window):
        super(Background,self).__init__(window)
        sdl2.ext.fill(self.surface,sdl2.ext.Color(0,33,66))



def main():
    sdl2.ext.init() # Initialze 
    world = sdl2.ext.World() # Create World
    W = sdl2.ext.Window("Default",size=(400,300), position = None,flags = sdl2.SDL_WINDOW_BORDERLESS) # Create Window


    BG = Background(W)
    world.add_system(BG)
    W.show()
    running = True
    while running:
        events = sdl2.ext.get_events()
        for event in events:
            if event.type == sdl2.SDL_QUIT:
                running = False
                break
            if event.type == sdl2.SDL_MOUSEBUTTONDOWN:
                X,Y = (300,100)                       # NEW COORDINATES
                print("Updating: . . ")
                W.position = X,Y                      # Updating the coordinates
                print(W.position)    
                W.hide()                       # Tried hiding and showing the window
                W.show()                       # Didn't help unfortunately
        W.refresh()   # Refresh the window. 
    return 0

if __name__ == "__main__":
    sys.exit(main())

My attempt is simply to update the .position attribute of the window. But like I stated earlier, nothing seems to happen.

EDIT: According to this blog post. It's pretty much impossible.


Solution

  • PySDL2's Window class does not feature a position attribute up to version 0.9.2. This is the reason, why your code does not work. If you are using SDL2's SDL_SetWindowPosition() function directly, the window can be positioned, if your window manager/operating system supports it (especially important for X11 and e.g. tiling window managers).

    Change your code

    print("Updating: . . ")
    W.position = X,Y                      # Updating the coordinates
    print(W.position)
    

    to

    print("Updating: . . ")
    sdl2.SDL_SetWindowPosition(W.window, X, Y)
    

    and it should work, given positioning the window is supported.