Introduction: How to Create an Automatically Updating Tkinter(ttkbootstrap) GUI Using Python

Here , We will learn to create a tkinter(ttkbootstrap) GUI that will automatically update the values of certain widgets like Labels, textboxes etc. in Periodic intervals without user intervention in Python.


The original article along with source code can be found below.


Supplies

  1. Requires Python 3.x.x to be installed on your system
  2. Requires ttkbootstrap to be installed on your System.
  3. Text Editor to run your program.

Step 1: Install Python,tkinter and Ttkbootstrap

Install Python/tkinter

Visit the official Python website at and navigate to the "Downloads" section. Choose the latest version of Python for Windows.

Once the installer is downloaded, double-click on it to start the installation process.

Make sure that you add Python to Path Variable(You can check the above video). This will allow you to run Python from the command line more easily.


Install ttkbootstrap theme extension on Windows 10

ttkbootstrap is a Python package that extends the functionality of Tkinter, the standard GUI toolkit for Python. It provides a set of pre-styled themes and widgets, allowing developers to easily create modern and visually appealing graphical user interfaces (GUIs) with minimal effort. This package combines the theming capabilities of the ttk module in Tkinter with the design elements of the popular Bootstrap framework, offering a wide range of styling options for Tkinter applications

To install ttkbootstrap, you can use the following steps

Use the following command to install ttkbootstrap using pip, Python's package installer. Pip is installed by default if you use the standard python installer as shown in above video.


pip install ttkbootstrap


You can check the above video for the full installation process.

If you are new to tkinter/ttkbootstrap ,Do check out our tutorial on Python GUI design using ttkbootstrap and tkinter


Step 2: Using After() Method of the Tkinter Window

We have a Tkinter GUI (as shown in the image above ) featuring two buttons and a text box.

The objective is to continuously display data from a sensor onto the text box within the Tkinter GUI, updating every second.

This ensures that users can consistently view the latest data.

To achieve this, the Tkinter GUI must periodically query the sensor function in the background at specific intervals, updating the data on the GUI automatically.

To ensure the GUI remains responsive to user interactions, such as pressing buttons, while continuously updating the text box,

we can utilize the .after() method provided by the Tkinter window.

This method allows us to execute a specific function in the background at regular intervals.

We can encapsulate the function responsible for querying the sensor inside the .after() function


Syntax of the .after() method for automatically running periodic updates to tkinter GUI

root = ttkb.Window()
root.after(delay_in_milli_seconds,function_to_run_periodically) 

#here "delay_in_milli_seconds" defines the period in milliseconds at which the function runs the "function_to_run_periodically" function

#here "function_to_run_periodically" is the function that will be run


Step 3: .after() Method Code for Updating Tkinter GUI Periodically

Here is a very basic code for running background tasks using .after() method to update tkinter/ttkbootstrap GUI automatically in real-time.

import ttkbootstrap as ttkb

def run_periodic_background_func():
print('Read from Sensor/Update textbox') #Put update function here
root.after(1000,run_periodic_background_func)

root = ttkb.Window()
root.geometry('400x400')
run_periodic_background_func() # call the update function once
root.mainloop()

I will just explain the basic tkinter GUI code first.

First line imports the ttkbootstrap library and aliases it as ttkb. ttkbootstrap is a Python package that extends Tkinter with Bootstrap-styled themes and widgets.

root = ttkb.Window()
root.geometry('400x400')

This code creates a Tkinter window using ttkb.Window() from ttkbootstrap and sets its size to 400x400 pixels using the geometry() method.

root.mainloop()

This line starts the Tkinter event loop, allowing the window to display and respond to user interactions.

All the action takes place in the function

def run_periodic_background_func():
print('Read from Sensor/Update textbox') #Put update function here
root.after(1000,run_periodic_background_func)

we have to call the function once to initialize it .

This function is defined to run periodically in the background. Inside the function, you can see a placeholder comment indicating where the update function should be placed.

The print() statement is used here as a placeholder to demonstrate the periodic execution of the function.

The function then schedules itself to run again after 1000 milliseconds (1 second) using the .after() method of the root Tkinter window.

You can put your "own update function", instead of the print() statement, every one second that "your own update function" will be called and run.





Step 4: Limitations of .after() Method

One issue with the .after() method is that ,if the function you are running inside the .after() method takes too long to return. The response of your GUI will be slow and sluggish.

One way to improve the responsiveness of functions that takes too long to run is to use python threading

The GUI will create a thread and run that function inside that thread concurrently, thereby the performance of the GUI is not effected. 

Please note that ,In threading we do not use the .after() method.

If you are interested in learning about threading in Python. Do check out our Video on Python threading here.