Closing an Imgui window: this seems like it should be easy. How does one do it?

12.4k Views Asked by At

I have started using the imgui system for visualizing "whatever". I am in my first few hours, and am running up against what seem to be common snags.

However, although I can see some pretty good support for the C++ versions of ImGui (which I'll transition to eventually), the python imgui content is mostly obscured.

What I am looking for is the solution to the following problem:

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

Everything works fine. However, the window doesn't close. I understand that the window doesn't close because it is always created every loop.

What I am looking for is:

How do I detect and identify that the particular window has been closed, and block it from being re-generated?

2

There are 2 best solutions below

1
On BEST ANSWER

JerryWebOS's answer is basically correct, but to add to that here's the python version. Note that the documentation for pyimgui is a good source to find answers to questions like this one.

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

imgui.begin() returns a tuple of two bools: (expanded, opened). You can use this to detect when the user closes the window, and skip rendering the window in the next frames accordingly:

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

I'm not at all familiar with the imGui for Python, but if it at all follows the similar pattern as in imGui for c++, then you need to follow this pattern:

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