Welcome to CodeYourCraft's Python Tutorial on DearPyGui! In this lesson, we'll dive into an exciting graphical user interface (GUI) library for Python. We'll walk you through DearPyGui from the ground up, making it accessible for both beginners and intermediates. Let's get started!
DearPyGui is a modern, efficient, and easy-to-use GUI library for Python. It allows you to create graphical applications with a clean, customizable, and cross-platform interface. DearPyGui is an excellent choice for building real-world applications, from simple calculators to complex data visualization tools.
Before we dive into the examples, let's get DearPyGui installed. Open your terminal or command prompt and run the following command:
pip install DearPyGuiNow that we have DearPyGui installed, let's create our first application.
import dearpygui.dearpygui as dpg
import dearpygui.dearpyguiqt as dpgqt
dpg.create_context()
with dpg.window(label="Hello, World!", width=300, height=200):
dpg.text(label="Welcome to DearPyGui!")
dpg.bind_item_pos_callback("Hello, World!")
def pos_callback(pos_x, pos_y):
print(f"Window moved to ({pos_x}, {pos_y})")
dpg.set_primary_window("Hello, World!", True)
dpg.show_context()
dpgqt.start_dearpygui()
dpg.destroy_context()Save this code in a file named hello_world.py and run it. You should see a window pop up that says "Hello, World!" with a title of the same name. Try moving the window around, and you'll see the position callback function in action.
DearPyGui offers a variety of widgets, such as buttons, input fields, and checkboxes, to create interactive applications.
DearPyGui allows you to create flexible and customizable layouts to organize your widgets.
Callbacks are functions that are called when an event, such as a button click or window move, occurs in your application.
Now, let's build a simple calculator using DearPyGui.
import dearpygui.dearpygui as dpg
import dearpygui.dearpyguiqt as dpgqt
dpg.create_context()
def add(x, y):
return x + y
with dpg.window(label="Calculator", width=250, height=300):
with dpg.column(label="Number Pad", width=200):
for i in range(10):
dpg.button(label=str(i), callback=lambda i=i: calc_callback(i))
dpg.button(label=".", callback=lambda: calc_callback("."))
dpg.button(label="0", callback=lambda: calc_callback("0"))
dpg.button(label="C", callback=lambda: calc_callback("C"))
with dpg.column(label="Operations"):
dpg.button(label="+", callback=lambda: calc_callback("+"))
dpg.button(label="-", callback=lambda: calc_callback("-"))
dpg.button(label="*", callback=lambda: calc_callback("*"))
dpg.button(label="/", callback=lambda: calc_callback("/"))
with dpg.column(label="Display"):
dpg.text(label="0", tag="result")
dpg.bind_item_pos_callback("Calculator")
def pos_callback(pos_x, pos_y):
print(f"Window moved to ({pos_x}, {pos_y})")
dpg.bind_item_value_callback("result", calc_callback)
dpg.set_primary_window("Calculator", True)
dpg.show_context()
def calc_callback(value):
global current_equation
current_equation += value
dpg.text(tag="result", value=current_equation)
current_equation = ""
dpgqt.start_dearpygui()
dpg.destroy_context()Save this code in a file named calculator.py and run it. You should see a simple calculator with number buttons, operation buttons, and a display. The calculator can perform basic arithmetic operations like addition, subtraction, multiplication, and division.
What is the primary advantage of using DearPyGui for building graphical applications?
That's it for this lesson on DearPyGui! In the next lesson, we'll dive deeper into DearPyGui and explore more advanced concepts. Happy coding! 🚀