首页 > 解决方案 > 如何在执行其他脚本/功能时调整窗口大小/移动窗口?现代gl窗口和pyglet

问题描述

我的目标:

编写一个类,在创建窗口后绘制一些东西,并且vao.render(mode=moderngl.PATCHES)仅在update()调用方法时使用,以便更新动态 VBO。imshowmatplotlib 太慢了。

问题:

基于这些 Github 问题#114#216这个例子,我创建了一个没有run()方法的自定义窗口设置。相反,顶点数组对象(VAO)是类的实例属性,在类初始化时会渲染一次。然后使用一个update(inputs)方法来改变一个动态顶点缓冲对象(VBO),也是一个实例属性,通过使用self.vbo.write(inputs)和重新渲染VAO,然后swap_buffers()调用。

但是当其他 python 代码正在运行时,我无法调整窗口大小/移动/关闭窗口。

问题:

有没有一种简单的方法来“解决”这个问题?


到目前为止我所做的

以 matplotlib.pyplot.show() 为灵感,虽然最终掺杂了这个想法,但我创建了一个字典作为类属性。

对于从我的类创建的每个新对象,int都会添加一个 distinct 作为该字典的键,相应的值是对象本身的别名。

然后我创建了一个静态方法,render()它无限期地打开这个类的所有对象,直到最后一个被关闭。

问题是它有点反应迟钝......

下面是我编写的想法的示例:

# to illustrate the main packages I actually use, they are written here
import numpy as np
import moderngl
import moderngl_window
from moderngl_window.conf import settings


class myViewerClass:

    # A dict as a mutable class attribute to keep track
    # of all instances of this class
    _manager_dict = {}

    def __init__(self):  # there can be more inputs here

        # more code here

        # Edit: I initialize the windows here using settings
        # close, resize, key, mouse click events, among others
        # are all implemented here
        self.wnd = moderngl_window.create_window_from_settings()

        # if no instances of this class exists yet
        if not myViewerClass._manager_dict:
            # assign a key of 0 to the first instance of this class
            self._num = 0
        else:
            # else, assign a distinct integer greater than the maximum
            # one found in the dict's keys
            self._num = max(myViewerClass._manager_dict.keys())+1
        # save an alias of the generated object in the dict
        myViewerClass._manager_dict[self._num] = self

    @staticmethod
    def show_all():
        # run while there are still open windows
        # this works because the dictionary evaluates to True
        # while it is not empty
        while myViewerClass._manager_dict:
            for window in myViewerClass._manager_dict.values():
                window.wnd.clear()  # this is from moderngl_window
                window.render()  # this is an instance method that is
                                 # implemented in this class using
                                 # OpenGL (ModernGL)
                window.wnd.swap_buffers()  # also from moderngl_window
                if window.wnd.is_closing():
                    # delete this object's entry in the class dict
                    del myViewerClass._manager_dict[window._num]
                    # close the window
                    window.wnd.destroy()
                    break  # this is necessary because the dict's
                           # length has changed, which issues an
                           # error in the `for` loop


编辑1:上面的代码确实有效。对于单个或多个对象。但该myViewerClass.show_all()方法似乎有点滞后。操作一个窗口(例如调整其大小)后,其他窗口开始闪烁。单击它们后闪烁停止。

并且点击某个窗口的关闭按钮后,如果有多个窗口打开,它会冻结而不是关闭,而其他窗口继续运行。假设我最初有 3 个窗口打开和关闭其中两个,它们冻结,但第三个继续工作。只有在关闭第三个窗口后,所有窗口才会同时关闭。

标签: pythonuser-interfacewindowpython-moderngl

解决方案


推荐阅读