Skip to content

Commit

Permalink
main : added some button examples and updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
my1e5 committed Mar 13, 2023
1 parent 81cf6ea commit 07e1bc2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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.
25 changes: 25 additions & 0 deletions buttons/change_button_colour_when_clicked.py
Original file line number Diff line number Diff line change
@@ -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()
52 changes: 52 additions & 0 deletions buttons/nested_radio_buttons.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 07e1bc2

Please sign in to comment.