A Beginner’s Guide to Creating a Simple Tkinter (ttkbootstrap) Window using Python
In this tutorial, we will walk you through the process of creating a basic window using the Tkinter library along with the ttkbootstrap extension in Python. Tkinter is the standard GUI (Graphical User Interface) library in Python, and ttkbootstrap enhances its functionality by adding a modern, sleek look to your apps
What is ttkbootstrap Library
Tkinter is Python’s standard library for creating desktop applications with graphical user interfaces (GUIs). It is built on top of the Tk GUI toolkit, which is known for being easy to use and lightweight. Tkinter allows you to create windows, buttons, labels, and many other UI elements that make your application interactive.
While Tkinter itself is functional, it has a rather old-fashioned look. ttkbootstrap is a library that modernizes the appearance of your Tkinter applications. ttkbootstrap is at theme extension for tkinter that enables modern flat style themes inspired by Bootstrap.
Installing ttkbootstrap Library
Before we dive into coding, you need to ensure that both Tkinter and ttkbootstrap are installed. Tkinter comes bundled with Python, so no additional installation is required for it. However, to use ttkbootstrap, you’ll need to install it manually.
Open your terminal or command prompt and run
pip install ttkbootstrap
You can also use the built in package manager present in the Thonny IDE to install the ttkbootstrap library.
Creating Your First Window with Tkinter and ttkbootstrap
Please type the following code into the IDE and Press Run.
import ttkbootstrap
# Create a window using the ttkbootstrap library with a 'yeti' theme
root = ttkbootstrap.Window(themename = 'yeti')
# Set the title of the window
root.title('Hello World')
# Set the size of the window (300 pixels wide and 300 pixels high)
root.geometry('300x300') # width x height
# Start the Tkinter event loop to keep the window open and responsive
root.mainloop()
On Running the code you get this
First line imports the ttkbootstrap
library, which enhances the look and feel of Tkinter applications.
root = ttkbootstrap.Window(themename = 'yeti')
:
- This line creates a window using
ttkbootstrap.Window
. TheWindow
function is a replacement for Tkinter'sTk()
, but it adds the ability to easily apply a modern theme. themename = 'yeti'
specifies the theme for the window.ttkbootstrap
provides several themes.full list can be found in the ttkbootstrap website
root.title('Hello World')
: This sets the title of the window. When you run the program, the window will display “Hello World” as its title .
root.geometry(‘300x300’) defines the size of the window. The window will be 300 pixels wide and 300 pixels tall.
root.mainloop() starts the Tkinter event loop. The event loop is necessary to keep the window open and responsive. Without it, the window would appear briefly and then close immediately. The event loop waits for user interactions like clicks or key presses and keeps the program running
This is a very basic example of using the ttkbootstrap library to create a simple window with minimal code, showcasing the power of the library in providing modern UI elements.