Search code examples
python-3.ximgui

How to set up IMGUI tabs for OpenGL drawing with Python?



import glfw
from OpenGL.GL import *
import imgui
from imgui.integrations.glfw import GlfwRenderer

def init_window(width, height, title):
    if not glfw.init():
        raise Exception("GLFW initialization failed")
    
    window = glfw.create_window(width, height, title, None, None)
    if not window:
        glfw.terminate()
        raise Exception("GLFW window creation failed")

    glfw.make_context_current(window)
    return window

def render_gui():
    imgui.new_frame()

    # Begin an ImGui window with automatic resizing
    if imgui.begin("Tab Window", True):
        
        if imgui.begin_tab_bar("MyTabs"):
            if imgui.begin_tab_item("Tab 1"):
                imgui.text("Rendering OpenGL in Tab 1")
                imgui.end_tab_item()

            if imgui.begin_tab_item("Tab 2"):
                imgui.text("Rendering OpenGL in Tab 2")
                imgui.end_tab_item()

            if imgui.begin_tab_item("Tab 3"):
                imgui.text("Rendering OpenGL in Tab 3")
                imgui.end_tab_item()

            imgui.end_tab_bar()
    
    imgui.end()


def main():
    # Initialize the window and ImGui
    window = init_window(800, 600, "GLFW Window with Tabs")
    imgui.create_context()
    impl = GlfwRenderer(window)
    
    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()
        
        glClear(GL_COLOR_BUFFER_BIT)
        
        # Render ImGui GUI
        render_gui()
        
        # OpenGL rendering would go here
        # Call the appropriate OpenGL drawing functions for the active tab
        
        # Render the ImGui frame
        imgui.render()
        impl.render(imgui.get_draw_data())
        
        glfw.swap_buffers(window)
    
    impl.shutdown()
    glfw.terminate()

if __name__ == "__main__":
    main()


Traceback (most recent call last):
  File "c:\Users\crist\repos\......\main.py", line 70, in <module>
    main()
  File "c:\Users\crist\repos\......\main.py", line 55, in main
    render_gui()
  File "c:\Users\crist\repos\......\main.py", line 35, in render_gui
    imgui.end_tab_item()
  File "imgui\core.pyx", line 11950, in imgui.core.end_tab_item
imgui.core.ImGuiError: ImGui assertion error (window->IDStack.Size > 1) at imgui-cpp/imgui.cpp:7017

My OS is Windows 11 Home, with Python 3.12+-...

glClear(GL_COLOR_BUFFER_BIT), is showing as unresolved. "from OpenGL.GL import *", has always seemed to work for me in the past when bringing in OpenGL functions like glClear. Why this traceback? How to set this up so that the window can open and display the tabs?

So far I tried changing the window size to try to get the tabs to come up...


Solution

  • Add the .selected attribute to the tab checker in the render_gui():

    imgui.new_frame()
    
        # Begin an ImGui window with automatic resizing
        if imgui.begin("Tab Window", True):
            
            if imgui.begin_tab_bar("MyTabs"):
                if imgui.begin_tab_item("Tab 1").selected:
                    imgui.text("Rendering OpenGL in Tab 1")
                    imgui.end_tab_item()
    
                if imgui.begin_tab_item("Tab 2").selected:
                    imgui.text("Rendering OpenGL in Tab 2")
                    imgui.end_tab_item()
    
                if imgui.begin_tab_item("Tab 3").selected:
                    imgui.text("Rendering OpenGL in Tab 3")
                    imgui.end_tab_item()
    
                imgui.end_tab_bar()
        
        imgui.end()