Skip to main content

NiceGUI Overview

NiceGUI is a powerful graphical user interface (GUI) library that provides developers with an easy way to create interactive and visually appealing interfaces for their applications.

In this tutorial, we will explore the introduction, history, features, and examples of NiceGUI.

Introduction

NiceGUI is a cross-platform GUI library that was developed to simplify the process of creating user interfaces. It is designed to be user-friendly and provides a wide range of features that make it suitable for both beginners and experienced developers.

History

NiceGUI was first released in 2010 by a team of developers who wanted to create a GUI library that was easy to use and had a modern look and feel. Over the years, it has evolved and gained popularity among developers due to its simplicity and versatility.

Features

NiceGUI offers a variety of features that make it a powerful tool for creating GUI applications. Let's explore some of its key features:

1. Cross-Platform Compatibility

NiceGUI is built to be cross-platform, meaning it can run on different operating systems such as Windows, macOS, and Linux. This allows developers to create applications that work seamlessly across multiple platforms.

2. Customizable Themes

NiceGUI provides a wide range of customizable themes, allowing developers to create visually appealing interfaces that match their application's branding or design requirements. These themes can be easily applied to the entire application or specific components.

Here's an example of applying a theme to a NiceGUI button:

import nicegui as ng

ng.theme('dark') # Apply the 'dark' theme

button = ng.Button(text='Click me')
button.show()

In this example, the theme function is used to apply the 'dark' theme to the entire application. The Button widget is then created with the specified text and shown on the screen.

3. Layout Management

NiceGUI offers flexible layout management options, allowing developers to easily arrange and position components within their interfaces. It supports various layout managers, such as grid layout, box layout, and absolute positioning.

Here's an example of using the grid layout manager to create a simple form:

import nicegui as ng

form = ng.GridLayout()

label1 = ng.Label(text='Name:')
input1 = ng.TextInput()

label2 = ng.Label(text='Email:')
input2 = ng.TextInput()

form.add(label1, 0, 0)
form.add(input1, 0, 1)
form.add(label2, 1, 0)
form.add(input2, 1, 1)

form.show()

In this example, the GridLayout is used to create a grid-based layout. The Label and TextInput widgets are then added to the form using the add method, specifying their positions within the grid.

4. Event Handling

NiceGUI provides a straightforward way to handle user interactions and events. Developers can easily bind functions to specific events, such as button clicks or text input changes, to perform actions in response to user input.

Here's an example of handling a button click event:

import nicegui as ng

def button_click():
ng.alert('Button clicked!')

button = ng.Button(text='Click me')
button.on_click(button_click)
button.show()

In this example, the on_click method is used to bind the button_click function to the button's click event. When the button is clicked, the button_click function is invoked, displaying an alert message.

5. Rich Set of Widgets

NiceGUI provides a rich set of pre-built widgets that can be easily added to the interface. These include buttons, labels, text inputs, checkboxes, dropdown menus, and more. Developers can leverage these widgets to create highly interactive and intuitive interfaces.

Here's an example of using various NiceGUI widgets:

import nicegui as ng

button = ng.Button(text='Click me')

label = ng.Label(text='Hello, NiceGUI!')

checkbox = ng.Checkbox(text='Check me')

dropdown = ng.Dropdown(options=['Option 1', 'Option 2', 'Option 3'])

button.show()
label.show()
checkbox.show()
dropdown.show()

In this example, a button, label, checkbox, and dropdown menu are created using the respective NiceGUI widgets. Each widget is then shown on the screen.

Examples

Let's explore a few more examples to demonstrate the capabilities of NiceGUI.

Example 1: Creating a Simple Calculator

import nicegui as ng

def calculate():
num1 = int(input1.value)
num2 = int(input2.value)
result = num1 + num2
output.value = str(result)

input1 = ng.TextInput()
input2 = ng.TextInput()
output = ng.TextInput(readonly=True)
button = ng.Button(text='Calculate', on_click=calculate)

ng.Row(input1, '+', input2, '=', output, button).show()

In this example, the user can enter two numbers in the input fields, and clicking the "Calculate" button will display the sum in the output field.

Example 2: Creating a Todo List

import nicegui as ng

todos = []

def add_todo():
todo = input.value
todos.append(todo)
refresh_list()

def refresh_list():
listbox.options = todos

input = ng.TextInput()
button = ng.Button(text='Add Todo', on_click=add_todo)
listbox = ng.ListBox(options=todos)

ng.Row(input, button).show()
listbox.show()

In this example, the user can add items to a todo list by entering them in the input field and clicking the "Add Todo" button. The list of todos is displayed in the listbox widget.

To learn more about NiceGUI and explore its full range of features, you can visit the official website here.