Skip to content

Dear PyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies

Install

pip install dearpygui

Simple example

import dearpygui.dearpygui as dpg

def save_callback():
    print("Save Clicked")

dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()

with dpg.window(label="Example Window"):
    dpg.add_text("Hello world")
    dpg.add_button(label="Save", callback=save_callback)
    dpg.add_input_text(label="string")
    dpg.add_slider_float(label="float")

dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

Demo example

import dearpygui.dearpygui as dpg
import dearpygui.demo as demo

dpg.create_context()
dpg.create_viewport(title='Custom Title', width=600, height=600)

demo.show_demo()

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

python-qrcode example

#### WRITING QR ###########################################
# creating a qr code image with qrcode library more info can 
# more info on the qrcode library 
# can be found here https://github.com/lincolnloop/python-qrcode
import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_M,
    box_size=5,
    border=4,
)
qr.add_data('https://github.com/hoffstadt/DearPyGui')
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
img.save("dpg-qr.png")


#### READING QR #######################################
# more info on the pyzbar library can be 
# found here https://github.com/NaturalHistoryMuseum/pyzbar
# we will read directly into dpg however you could utilize open cv 
# from the last example and grab the image from every camera 
# capture and use pyzbar to decode it

# reading the qr code .png into dpg and showing it
import dearpygui.dearpygui as dpg
from pyzbar.pyzbar import decode
import numpy
import webbrowser
dpg.create_context()

def process_qr_image():
    width, height, channels, data = dpg.load_image("dpg-qr.png")

    # after loading with dpg we will read 
    # into a numpy array to do some data minuplation
    # pyzbar wants data in a a 255 shaped format of 8 bits
    # but will also accept a numpy array and do the 
    # conversion from 32 to 8 bit for us
    nparray = numpy.frombuffer(data, 'f')
    nparrayscalar = numpy.multiply(nparray, 255.0)
    npreshaped = numpy.reshape(nparrayscalar,(width, height, channels))

    # various helpful stuff to know about the array
    print("Array is of type: ", type(npreshaped))
    print("No. of dimensions: ", npreshaped.ndim)
    print("Shape of array: ", npreshaped.shape)
    print("Size of array: ", npreshaped.size)
    print("Array stores elements of type: ", npreshaped.dtype)

    # reading the image for QR 
    qr_class = decode(npreshaped)[0]

    # various helpful stuff to know about the qr code class
    print(type(qr_class))
    print(qr_class)

    # reading for data
    qr_data = qr_class.data.decode()

    # because the data in teh qr code is a link we will open it
    webbrowser.open(qr_data)

    # we can also pull the rect or poly infor from the 
    # qr code class and draw squares, 
    # referenceing the open cv example also you could draw 
    # live rectangles overtop of the images using dpg every frame

width, height, channels, data = dpg.load_image("dpg-qr.png")
with dpg.texture_registry():
    dpg.add_static_texture(width=width, height=height, default_value=data, tag="texture_tag")

with dpg.window(label="Tutorial", tag="MainWin", width=800, height=600):
    dpg.add_image("texture_tag")
    dpg.add_button(label="Process QR code", callback=process_qr_image)
    dpg.add_text("", tag="QR Data")



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

OpenCV Image Preview example

import dearpygui.dearpygui as dpg
import cv2 as cv
import numpy as np

dpg.create_context()
dpg.create_viewport(title='Custom Title', width=600, height=800)
dpg.setup_dearpygui()

vid = cv.VideoCapture(0)
ret, frame = vid.read()

# image size or you can get this from image shape
frame_width = vid.get(cv.CAP_PROP_FRAME_WIDTH)
frame_height = vid.get(cv.CAP_PROP_FRAME_HEIGHT)
video_fps = vid.get(cv.CAP_PROP_FPS)
print(frame_width)
print(frame_height)
print(video_fps)

print("Frame Array:")
print("Array is of type: ", type(frame))
print("No. of dimensions: ", frame.ndim)
print("Shape of array: ", frame.shape)
print("Size of array: ", frame.size)
print("Array stores elements of type: ", frame.dtype)
data = np.flip(frame, 2)  # because the camera data comes in as BGR and we need RGB
data = data.ravel()  # flatten camera data to a 1 d stricture
data = np.asfarray(data, dtype='f')  # change data type to 32bit floats
texture_data = np.true_divide(data, 255.0)  # normalize image data to prepare for GPU

print("texture_data Array:")
print("Array is of type: ", type(texture_data))
print("No. of dimensions: ", texture_data.ndim)
print("Shape of array: ", texture_data.shape)
print("Size of array: ", texture_data.size)
print("Array stores elements of type: ", texture_data.dtype)

with dpg.texture_registry(show=True):
    dpg.add_raw_texture(frame.shape[1], frame.shape[0], texture_data, tag="texture_tag", format=dpg.mvFormat_Float_rgb)

with dpg.window(label="Example Window"):
    dpg.add_text("Hello, world")
    dpg.add_image("texture_tag")

dpg.show_metrics()
dpg.show_viewport()
while dpg.is_dearpygui_running():

    # updating the texture in a while loop the frame rate will be limited to the camera frame rate.
    # commenting out the "ret, frame = vid.read()" line will show the full speed that operations and updating a texture can run at

    ret, frame = vid.read()
    data = np.flip(frame, 2)
    data = data.ravel()
    data = np.asfarray(data, dtype='f')
    texture_data = np.true_divide(data, 255.0)
    dpg.set_value("texture_tag", texture_data)

    # to compare to the base example in the open cv tutorials uncomment below
    #cv.imshow('frame', frame)
    dpg.render_dearpygui_frame()

vid.release()
#cv.destroyAllWindows() # when using upen cv window "imshow" call this also
dpg.destroy_context()

See also

Favorite site