hoffstadt / DearPyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies
https://dearpygui.readthedocs.io/en/latest/
MIT License
12.63k stars 669 forks source link

How Multiple Processes Interact #2221

Closed monkeycc closed 7 months ago

monkeycc commented 8 months ago
import dearpygui.dearpygui as dpg
from multiprocessing import Process

def example_process():

    print("Hello DearPyGui!")

    dpg.set_value("hello_text", "Hello DearPyGui!")

def start_process(sender, data):
    test_process = Process(target=example_process)
    test_process.start()

dpg.create_context()
dpg.create_viewport(title='Custom Title', width=600, height=300)

with dpg.window(label="Example Window"):
    dpg.add_text("Hello, world",tag="hello_text",)
    dpg.add_button(label="button1", callback=start_process,width=100,height=100)

if __name__ == "__main__":

    dpg.setup_dearpygui()
    dpg.show_viewport()
    dpg.start_dearpygui()
    dpg.destroy_context()

Subprocess How to Control GUI Framework

v-ein commented 8 months ago

Since the main program and the subprocess are separate processes, you can only send messages from the subprocess to the main process and then do all the UI work in the main process. Use whatever inter-process communication (IPC) mechanism you find convenient. The doc on the multiprocessing module has a couple of sections on IPC - start with this one:

https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes