首页 > 解决方案 > 关闭 Imgui 窗口:这看起来应该很容易。如何做到这一点?

问题描述

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

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

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

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

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

我正在寻找的是:

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

标签: python-3.ximguipyimgui

解决方案


我对 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();
    }
}

推荐阅读