0

我已经开始使用该imgui系统来可视化“任何东西”。我在我最初的几个小时里,并且遇到了似乎很常见的障碍。

然而,尽管我可以看到对 ImGui 的 C++ 版本的一些很好的支持(我将最终过渡到),但 python imgui 的内容大多是模糊的。

我正在寻找的是以下问题的解决方案:

while not glfw.window_should_close(window):
  ...
  imgui.new_frame()
  imgui.begin("foo-window", closable=True)
  imgui.end()

一切正常。但是,窗口并没有关闭。我知道窗口不会关闭,因为它总是在每个循环中创建。

我正在寻找的是:

如何检测和识别特定窗口已关闭,并阻止它重新生成?

4

2 回答 2

2

我对 Python 的 imGui 一点也不熟悉,但如果它完全遵循与 imGui for c++ 类似的模式,那么您需要遵循以下模式:

static bool show_welcome_popup = true;

if(show_welcome_popup)
{
    showWelcomePopup(&show_welcome_popup);
}

void showWelcomePopup(bool* p_open)
{
    //The window gets created here. Passing the bool to ImGui::Begin causes the "x" button to show in the top right of the window. Pressing the "x" button toggles the bool passed to it as "true" or "false"
    //If the window cannot get created, it will call ImGui::End
    if(!ImGui::Begin("Welcome", p_open))
    {
        ImGui::End();
    } 
    else
    {
        ImGui::Text("Welcome");   
        ImGui::End();
    }
}
于 2021-04-13T13:04:13.110 回答
1

JerryWebOS 的回答基本上是正确的,但要补充的是 python 版本。请注意,pyimgui 的文档是查找此类问题答案的好来源。

https://pyimgui.readthedocs.io/en/latest/reference/imgui.core.html?highlight=begin#imgui.core.begin

imgui.begin() 返回两个布尔值的元组:(扩展,打开)。您可以使用它来检测用户何时关闭窗口,并相应地跳过在下一帧中渲染窗口:

window_is_open = True

while not glfw.window_should_close(window):
    ...
    imgui.new_frame()
    if window_is_open:
        _, window_is_open = imgui.begin("foo-window", closable=True)
        ...
        imgui.end()
于 2022-01-06T17:10:28.380 回答