Python Textual: Build Beautiful UIs in the Terminal

Python Textual: Build Beautiful UIs in the Terminal

Have you ever wanted to create an app with an appealing interface that works in the command line? Welcome to Textual, a Python toolkit and framework for creating beautiful, functional text-based user interface (TUI) applications. The Textual library provides a powerful and flexible framework for building TUIs. It offers a variety of features that allow you to create interactive and engaging console applications.

In this tutorial, you’ll learn how to create, style, and enhance Textual apps with layouts, events, and actions.

By the end of this tutorial, you’ll understand that:

  • Python Textual is a framework for building terminal-based applications with interactive and visually appealing text interfaces.
  • Textual works by providing a set of widgets, layouts, and styling options, enabling you to create responsive and interactive console apps.
  • Textual is useful for building efficient, platform-independent text-based user interfaces that work over remote connections and in low-resource environments.

Textual is built on Rich, a Python library for creating rich text and beautifully formatted terminal output. It enhances text-based applications by applying colors, styles, and formatting to your text output in Python.

Take the Quiz: Test your knowledge with our interactive “Python Textual: Build Beautiful UIs in the Terminal” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Python Textual: Build Beautiful UIs in the Terminal

In this quiz, you'll test your understanding of the Python Textual library. This library is used to create rich terminal applications and widgets. By working through this quiz, you'll reinforce your knowledge of Textual's key concepts and features.

Installing Textual

Textual requires Python 3.8 or later to work. As with any new Python project, it’s a good idea to create a virtual environment before you start. This helps keep your system’s Python environment clean and prevents unnecessary dependencies that may cause trouble later on.

Once you have the Python virtual environment, you can install Textual from PyPI using pip, which is the package installer for Python.

Open a command-line console and create a directory to host your Textual code. Then, run the commands below to create a virtual environment, activate it, and install Textual:

Windows PowerShell
PS> python -m venv venv
PS> venv\Scripts\activate
(venv) PS> python -m pip install textual textual-dev
Shell
$ python -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install textual textual-dev

Here, you first create a virtual environment using the venv module from the standard library. Then, you activate it, and finally, you install Textual using pip.

You’ve installed two packages:

  1. textual is the library and application framework that’ll provide the machinery for your TUI applications.
  2. textual-dev contains a command-line tool, also named textual, that facilitates debugging and interactive development through the Textual console.

In what follows, you’ll mostly be using the textual library, but you’ll also see an example of how to use the textual tool from textual-dev.

Checking the Textual Installation

The textual package is now installed in your Python virtual environment. You can check your installation by running the following command:

Shell
(venv) $ python -m textual

You should see the Textual demo app, a nicely formatted TUI application that displays useful information about Textual’s capabilities. You can interact with it using the mouse or keys:

Notice the colored text, emojis, structured layout, and the keyboard shortcuts listed in the window footer. There’s a lot to explore here, but for now, you’re going to dive right in and create your first app using Textual. You can close the demo by pressing Ctrl+C.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

About Charles de Villiers

Charles teaches Physics and Math. When he isn't teaching or coding, he spends way too much time playing online chess.

» More about Charles

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: intermediate front-end tools