Skip to content

Example Dynamic List

Zacharie edited this page Jul 19, 2023 · 1 revision

Tutorial: Creating a Dynamic List using PynamicUI

In this tutorial, we will learn how to create a dynamic list of items using PynamicUI, a lightweight Python library that provides a dynamic user interface (UI) framework. We will build a virtual DOM with a container and use a loop to create multiple items in the list.

Prerequisites

Before getting started, make sure you have Python and PynamicUI installed on your system. If you don't have PynamicUI installed, you can install it using pip:

pip install pynamicui

Step 1: Import the Required Modules

First, import the necessary modules to create the virtual DOM and UI elements:

from pynamicui import createDom, createElement

Step 2: Create the Dynamic List

Now, let's create a dynamic list of items using PynamicUI. We will use a loop to generate multiple items within a parent container.

# Create the virtual DOM
dom = createDom()

# Create the parent container
container = createElement(dom, "Frame", place={"relx": 0, "rely": 0, "relwidth": 1, "relheight": 1})

# Define the number of items in the list
num_items = 5

# Create dynamic list items using a loop
for i in range(num_items):
    # Create a button element for each item with a unique text
    item = createElement(container, "Button", props={"text": f"Item {i+1}"})
    # Place the items in the parent container vertically with equal spacing
    item.place({"relx": 0, "rely": (i / num_items), "relwidth": 1, "relheight": (1 / num_items)})

# Render the virtual DOM
dom.render()

Step 3: Run the Dynamic List App

Save the code in a Python file (e.g., dynamic_list_app.py) and run it using the following command:

python dynamic_list_app.py

You should now see a window displaying a dynamic list of buttons labeled "Item 1" to "Item 5". Each button will be placed vertically in the parent container with equal spacing.

Conclusion

In this tutorial, we learned how to create a dynamic list of items using PynamicUI. We used a loop to generate multiple elements within a parent container and placed them vertically with equal spacing. PynamicUI's declarative syntax and dynamic UI framework make it easy to build interactive and responsive applications. Feel free to experiment with the code and explore more features of PynamicUI to enhance your UI elements. Happy coding!

Clone this wiki locally