diff --git a/README.md b/README.md index bc167cf..dd1c106 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,12 @@ # DearPyGui examples A collection of example scripts which demonstrate various features/functionality in DearPyGui. +![Simple paint](assets/gifs/simple_paint.gif) +![Progress bar](assets/gifs/progress_bar.gif) + ## Examples +- [Buttons](#buttons) - [Data binding](#data-binding) - [Drawing](#drawing) - [Simple paint](#simple-paint) @@ -16,6 +20,9 @@ A collection of example scripts which demonstrate various features/functionality - [Progress bar](#progress-bar) - [Window](#window) +## Buttons + +Examples of how to implement various types of buttons. This includes how to implement a button which changes colour when clicked and how to implement nested radio buttons. ## Data binding @@ -29,8 +36,6 @@ Examples of how to use the drawing API. A very simple implementation of a paint app. It demonstrates how you can click and drag the mouse on a `dpg.add_drawlist` and draw basic free-form lines using `dpg.draw_line`. -![Simple paint](assets/gifs/simple_paint.gif) - ## Listbox Examples of custom listbox widgets which extend the functionality of the default listbox. Includes how to implement a listbox which is unselected by default and how to respond to key presses. @@ -63,8 +68,6 @@ Examples of how to use threading in DearPyGui. Includes a start/stop button whic A basic progress bar with a start button. Once running, the start button changes to a pause button. The task can then be paused, upon which the pause button changes to a resume button and a reset button appears. -![Progress bar](assets/gifs/progress_bar.gif) - ## Window Examples of how to manage windows. This includes how to create a window which is always on top and how to drag the viewport when `decorated=False`. Also includes restricting the position of a window and how to implement a clicked handler for child windows. diff --git a/buttons/change_button_colour_when_clicked.py b/buttons/change_button_colour_when_clicked.py new file mode 100644 index 0000000..c211809 --- /dev/null +++ b/buttons/change_button_colour_when_clicked.py @@ -0,0 +1,25 @@ +import dearpygui.dearpygui as dpg +dpg.create_context() + +with dpg.theme() as button_clicked_theme: + with dpg.theme_component(): + dpg.add_theme_color(dpg.mvThemeCol_Button, (255, 0, 0)) + dpg.add_theme_color(dpg.mvThemeCol_ButtonHovered, (225, 0, 0)) + dpg.add_theme_color(dpg.mvThemeCol_ButtonActive, (205, 0, 0)) + + +def toggle_button_colour(sender): + if dpg.get_item_theme(sender) == button_clicked_theme: + dpg.bind_item_theme(sender, None) + else: + dpg.bind_item_theme(sender, button_clicked_theme) + +with dpg.window(): + dpg.add_button(label="Click me!", callback=toggle_button_colour) + + +dpg.create_viewport(width=800, height=600, title="Change button colour when clicked") +dpg.setup_dearpygui() +dpg.show_viewport() +dpg.start_dearpygui() +dpg.destroy_context() diff --git a/buttons/nested_radio_buttons.py b/buttons/nested_radio_buttons.py new file mode 100644 index 0000000..89745a4 --- /dev/null +++ b/buttons/nested_radio_buttons.py @@ -0,0 +1,52 @@ +import dearpygui.dearpygui as dpg +dpg.create_context() + +buttons = { + "1": {"checked": True, "nested": {"1a":False, "1b":True, "1c":False, "1d":False}}, + "2": {"checked": False, "nested": {}}, + "3": {"checked": False, "nested": {"3a":True}}, +} + +def checkbox_callback(sender): + for button in buttons.keys(): + if not button == sender: + buttons[button]["checked"] = False + dpg.set_value(button, False) + else: + buttons[button]["checked"] = True + if not dpg.get_value(sender): + dpg.set_value(sender, True) + +def radio_button_callback(sender): + for nested_button in buttons[sender.split("_")[0]]["nested"].keys(): + buttons[sender.split("_")[0]]["nested"][nested_button] = False + buttons[sender.split("_")[0]]["nested"][dpg.get_value(sender)] = True + +with dpg.window(): + for button, values in buttons.items(): + dpg.add_checkbox( + tag=button, + label=button, + default_value=values["checked"], + callback=checkbox_callback, + ) + if values["nested"]: + dpg.add_radio_button( + tag=button + "_nested", + items=list(values["nested"].keys()), + indent=24, + callback=radio_button_callback, + ) + for nested_button, nested_values in values["nested"].items(): + if nested_values: + dpg.set_value(button + "_nested", nested_button) + + dpg.add_spacer(height=20) + dpg.add_button(label="Print buttons state", callback=lambda: print(buttons)) + + +dpg.create_viewport(width=800, height=600, title="Nested Radio Buttons Demo") +dpg.setup_dearpygui() +dpg.show_viewport() +dpg.start_dearpygui() +dpg.destroy_context()