pythonclasspython-importinitdearpygui

DearPyGUI script won't start after wrapping it in a class


I have a messy code for sensor live data recording which I rewrote in OOP style, docstrings and comments. The new version runs without errors but the GUI is invisible or won't start at all. Here I have a minimal example. First script runs and displays the GUI window. But when I wrap it into a class and run it from main, I see nothing. No errors but also no GUI. Could someone show me my logic error? I know that the naming here is non-standard, that's irrelevant. Also, this is the first time I'm building an OOP program so I might have misconceptions about the __init()__ method, too. Feel free to enlighten me. This script runs ok:

import dearpygui.dearpygui as dpg

dpg.create_context()
with dpg.window(label="Example Window"):
    dpg.add_text("Hello, world")
    dpg.add_button(label="Save")
    dpg.add_input_text(label="string", default_value="Quick brown fox")
    dpg.add_slider_float(label="float", default_value=0.273, max_value=1)

dpg.create_viewport(title='Custom Title', width=600, height=300)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

But the same script as a class from main does not show anything:

# this script is saved to same dir with main as dpg_demo.py
import dearpygui.dearpygui as dpg

class dpg_demo():

    def __init__(self):

        dpg.create_context()
        with dpg.window(label="Example Window"):
            dpg.add_text("Hello, world")
            dpg.add_button(label="Save")
            dpg.add_input_text(label="string", default_value="Quick brown fox")
            dpg.add_slider_float(label="float", default_value=0.273, max_value=1)

        dpg.create_viewport(title='Custom Title', width=600, height=300)
        dpg.setup_dearpygui()
        dpg.show_viewport()
        dpg.start_dearpygui()
        dpg.destroy_context()
import dpg_demo as demo

def main():

    gui = demo()

Solution

  • You forgot to call main

    if __name__ == "__main__":
        main()