Skip to content

Instantly share code, notes, and snippets.

@DGriffin91
Last active April 4, 2024 06:39
Show Gist options
  • Save DGriffin91/c986d9bce5a0549d6e5c207261636e62 to your computer and use it in GitHub Desktop.
Save DGriffin91/c986d9bce5a0549d6e5c207261636e62 to your computer and use it in GitHub Desktop.
A simple pyimgui example using the GLFW backend
#pip install imgui[glfw]
import imgui
import glfw
import OpenGL.GL as gl
from imgui.integrations.glfw import GlfwRenderer
def impl_glfw_init(window_name="minimal ImGui/GLFW3 example", width=1280, height=720):
if not glfw.init():
print("Could not initialize OpenGL context")
exit(1)
# OS X supports only forward-compatible core profiles from 3.2
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(int(width), int(height), window_name, None, None)
glfw.make_context_current(window)
if not window:
glfw.terminate()
print("Could not initialize Window")
exit(1)
return window
class GUI(object):
def __init__(self):
super().__init__()
self.backgroundColor = (0, 0, 0, 1)
self.window = impl_glfw_init()
gl.glClearColor(*self.backgroundColor)
imgui.create_context()
self.impl = GlfwRenderer(self.window)
self.string = ""
self.f = 0.5
self.loop()
def loop(self):
while not glfw.window_should_close(self.window):
glfw.poll_events()
self.impl.process_inputs()
imgui.new_frame()
imgui.begin("Custom window", True)
imgui.text("Hello, world!")
if imgui.button("OK"):
print(f"String: {self.string}")
print(f"Float: {self.f}")
_, self.string = imgui.input_text("A String", self.string, 256)
_, self.f = imgui.slider_float("float", self.f, 0.25, 1.5)
imgui.show_test_window()
imgui.end()
imgui.render()
gl.glClearColor(*self.backgroundColor)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
self.impl.render(imgui.get_draw_data())
glfw.swap_buffers(self.window)
self.impl.shutdown()
glfw.terminate()
if __name__ == "__main__":
gui = GUI()
@quazi-irfan
Copy link

It was really helpful! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment