Closed BlindChickens closed 1 month ago
Hello,
As is common with GUI frameworks, the GUI needs to be in the main thread. I would advise you to use two separate threads, with asyncio in a separate thread.
Example:
import asyncio
import threading
from imgui_bundle import imgui, immapp, hello_imgui
#
# GUI code
#
def gui() -> None:
imgui.text("Hello world")
if imgui.button("Click me"):
print("Button clicked")
def run_gui() -> None:
runner_params = hello_imgui.RunnerParams()
runner_params.callbacks.show_gui = gui
immapp.run(runner_params)
#
# Asyncio code
#
async def other_async_task():
while True:
print("Async task running...")
await asyncio.sleep(1)
def run_asyncio_loop():
asyncio.run(other_async_task())
if __name__ == "__main__":
# Start the asyncio loop in a separate thread
asyncio_thread = threading.Thread(target=run_asyncio_loop)
asyncio_thread.daemon = True # Make sure it exits when the main program exits
asyncio_thread.start()
# Run the GUI on the main thread
run_gui()
i had this same requirement a while back and made a utility module async_thread.py
that can be easily imported and reused to run multiple things in async thread, see below for source and usage example:
https://gist.github.com/Willy-JL/183cb7134e940db1cfab72480e95a357
sidenote, just found imgui_bundle
and it looks very nice! i will probably look at switching from pyimgui, served me well but sadly the maintainer doesnt have enough time to keep up with it and cant blame them, its a huge undertaking
I am currently converting a Python GTK app using asyncio to ImGui. It's been a pretty smooth process.
I ended up using the SDL2Renderer
from imgui_bundle.python_backends.sdl_backend
. I think glfw
is somewhat more popular but I couldn't get it to report keyboard and mouse events the way I wanted (my app does a lot of custom bindings).
In my app, I didn't create a separate thread for the UI, the main thread runs the asyncio event loop and the UI.
If you want to have a look here's the branch I am working on: bgribble/mfp ... the imgui code is in mfp/gui/imgui
As in the title, I want to make use of imgui_bundle in python with an asyncio app. There is one way to do it using two seperate threads, but I don't fully comprehend what the consequences of that will be going further with the app.
I would like to try and run it together with asyncio in one thread if possible. If it is not, or it is a bad idea, then I will try the threaded approach.
Thanks in advance for the help.