Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

im using dpg and i want to add a user integer input to my program how do i do that?

  1. User

    im using dpg and i want to add a user integer input to my program how do i do that?

  2. Assistant

    To add a user integer input to your program using DPG (DearPyGui), you can use the add_input_int() function. Here's an example of how you can achieve this:

    from dearpygui.core import *
    from dearpygui.simple import *
    
    def input_callback(sender, data):
        user_input = get_value(data)
        # perform necessary operations with user_input
        print(f"User input: {user_input}")
    
    with window("My Window"):
        add_text("Enter an Integer:")
        add_input_int("##input_int", callback=input_callback)
    
    start_dearpygui()
    

    In this example, window() is used to create a window called "My Window". add_text() is used to display a text letting the user know what to do. add_input_int() is then used to add the input field for the user to enter an integer value. The callback parameter is set to input_callback function. Whenever the user interacts with the input field, input_callback function will be called.

    Inside the input_callback function, you can get the value from the input field using the get_value() function with the ID of the input field as the parameter. Then, you can perform any necessary operations with the obtained user input. In the example, it simply prints the user input to the console.

    Finally, start_dearpygui() is called to start the DearPyGui event loop and display the window.

    Remember to have DearPyGui and its dependencies properly installed before running this code.

  3. User

    import dearpygui.dearpygui as dpg

    dpg.create_context()

    def button_callback(sender, app_data): pass with dpg.window(tag="Primary Window"): dpg.add_menu_item(label='max. Anzal der Kisten')

    dpg.add_input_int()
    dpg.add_input_int()
    
    
    
    
    
    dpg.add_button(label='Generieren')
    
    
    dpg.add_table()
    

    dpg.create_viewport(title='Custom Title', width=600, height=200) dpg.setup_dearpygui() dpg.show_viewport() dpg.set_primary_window("Primary Window", True) dpg.start_dearpygui() dpg.destroy_context()


    der zweite input integer soll auf 10er schritte begrenzt sein

  4. Assistant

    To limit the second input integer to increments of 10, you can make use of the step parameter in the add_input_int() function. Here's an updated version of your code with the necessary modifications:

    import dearpygui.dearpygui as dpg
    
    def button_callback(sender, app_data):
        pass
    
    with dpg.window(tag="Primary Window"):
        dpg.add_menu_item(label='max. Anzahl der Kisten')
    
        dpg.add_input_int(label='Input 1')
        dpg.add_input_int(label='Input 2', step=10)  # Setting step parameter to 10
    
    
        dpg.add_button(label='Generieren')
    
    
        dpg.add_table()
    
    dpg.create_context()
    dpg.create_viewport(title='Custom Title', width=600, height=200)
    dpg.setup_dearpygui()
    dpg.show_viewport()
    dpg.set_primary_window("Primary Window", True)
    dpg.start_dearpygui()
    dpg.destroy_context()
    

    In this updated code, the second add_input_int() call includes the step=10 parameter. This sets the step size to 10, meaning the input value will be restricted to increments of 10.

    Please note that you should call create_context() before any other DPG function calls and destroy_context() at the end of your code to properly manage the DPG context.

  5. User

    printe mir den user input

  6. Assistant

    Um den Benutzereingabe-Wert in der Konsole auszugeben, können Sie die vorhandenen Benutzereingabe-Funktionen mit einer print-Anweisung kombinieren. Hier ist der aktualisierte Code:

    import dearpygui.dearpygui as dpg
    
    def button_callback(sender, app_data):
        pass
    
    def print_user_input(sender, app_data):
        user_input = dpg.get_value(app_data)
        print(f"User Input: {user_input}")
    
    with dpg.window(tag="Primary Window"):
        dpg.add_menu_item(label='max. Anzahl der Kisten')
    
        dpg.add_input_int(label='Input 1', callback=print_user_input, callback_data="Input 1")
        dpg.add_input_int(label='Input 2', step=10, callback=print_user_input, callback_data="Input 2")
    
        dpg.add_button(label='Generieren')
    
    
        dpg.add_table()
    
    dpg.create_context()
    dpg.create_viewport(title='Custom Title', width=600, height=200)
    dpg.setup_dearpygui()
    dpg.show_viewport()
    dpg.set_primary_window("Primary Window", True)
    dpg.start_dearpygui()
    dpg.destroy_context()
    

    Hier haben wir eine neue Funktion print_user_input hinzugefügt. Diese Funktion wird als callback für beide add_input_int-Aufrufe verwendet. Die Funktion ruft den Wert der jeweiligen Eingabe mit dpg.get_value(app_data) ab und gibt ihn mit print aus.

    Im add_input_int-Aufruf wird das callback-Argument auf print_user_input festgelegt und das callback_data-Argument auf den entsprechenden Bezeichner (z.B. "Input 1" oder "Input 2") festgelegt. Dadurch wird der Bezeichner als app_data-Parameter an die print_user_input-Funktion übergeben, um den Unterschied zwischen den verschiedenen Eingaben zu erkennen.

    Jetzt wird der Benutzereingabe-Wert in der Konsole ausgegeben, sobald der Benutzer etwas in eines der Eingabefelder eingibt.