1

我目前正在尝试更改我在 PySDL2 中创建的窗口的位置,在它已经被渲染之后。

我试过更新Window.position窗口的属性。但是尽管这样做了,并且让表面自行刷新,但没有可见的变化。(窗口停留在最初绘制的位置)。

我知道我可以更改窗口位置,因为如果我更改窗口创建行中的位置,它会在最初绘制在屏幕上时发生更改。(你只是似乎无法改变它之后)

代码:

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())

我的尝试只是更新窗口的 .position 属性。但就像我之前所说的,似乎什么都没有发生。

编辑:根据这篇博文。这几乎是不可能的。

4

1 回答 1

2

PySDL2 的 Window 类在 0.9.2 版之前没有位置属性。这就是为什么您的代码不起作用的原因。如果您直接使用 SDL2 的SDL_SetWindowPosition()函数,则可以定位窗口,前提是您的窗口管理器/操作系统支持它(对于 X11 尤其重要,例如平铺窗口管理器)

更改您的代码

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

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

并且它应该可以工作,因为它支持定位窗口。

于 2014-07-07T10:34:48.780 回答