python - 更新 PySDL2 中的窗口位置(帮助!)

标签 python window pysdl2

我目前正在尝试更改在 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 属性。但正如我之前所说,似乎什么也没有发生。

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

最佳答案

PySDL2's Window class 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)

如果支持窗口定位,它应该可以工作。

关于python - 更新 PySDL2 中的窗口位置(帮助!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24576570/

相关文章:

python - PySDL2 制作多个曲面

python - 在plotly Dash中显示一个简单的matplotlib图

python - 如何从 CSV 中获取特定表格并用 Python 编写新文件?

python - 使用 netCDF4 python 模块时出错

python - 适用于 Linux 的可移植 .so 库

python - 如何在我的 Tkinter GUI 应用程序中嵌入 SDL2 窗口?

python - Django 国际化 (i18n) lint 检查器?告诉我什么还没有 _( )'ed or {% trans %}' ed

c++ - WM_CREATE 和主窗口

button - 将程序置于最前面的 VBS 脚本

c# - HtmlWindow.Error 未捕获脚本错误 .NET