Summary: in this tutorial, you’ll learn step-by-step how to develop the Tkinter
“Hello, World!” program.
Creating a window
The following program shows how to display a window on the screen:
import tkinter as tk
root = tk.Tk()
root.mainloop()
Code language: Python (python)
If you execute the program, you’ll see the following window:
How it works.
First, import the tkinter
module as tk
to the program:
import tkinter as tk
Code language: Python (python)
Second, create an instance of the tk.Tk
class that will create the application window:
root = tk.Tk()
Code language: Python (python)
By convention, the main window in Tkinter is called root
. But you can use any other name like main
.
Third, call the mainloop()
method of the main window object:
root.mainloop()
Code language: Python (python)
The mainloop()
method ensures the main window remains visible on the screen.
If you don’t call the mainloop()
method, the main window will display and disappear almost instantly – too quickly to perceive its appearance.
Additionally, the mainloop()
method ensures the main window continues to display and run until you close it.
Typically, in a Tkinter program, you place the call to the mainloop()
method as the last statement after creating the widgets.
Troubleshooting
The tkinter
module is a built-in Python module. But sometimes, it is not the case. For example, on Ubuntu, you may encounter the following error:
ImportError: No module named Tkinter
Code language: JavaScript (javascript)
In this case, you need to install tkinter
module using the following command line:
sudo apt-get install python3-tk
Code language: JavaScript (javascript)
Displaying a label
Now, let’s place a component on the window. In Tkinter, these components are referred to as widgets.
The following code adds a label widget to the root window:
import tkinter as tk
root = tk.Tk()
# place a label on the root window
message = tk.Label(root, text="Hello, World!")
message.pack()
# keep the window displaying
root.mainloop()
Code language: Python (python)
Keep in mind that you’ll learn more about the Label
widget in the upcoming tutorial.
If you run the program, you’ll see the following output:
How it works.
To create a widget that belongs to a container, you use the following syntax:
widget = WidgetName(master, **options)
Code language: Python (python)
In this syntax:
- The
master
is the parent window or frame where you want to place the widget. - The
options
is one or more keyword arguments that specify the configurations of the widget.
In the program, the following creates a Label
widget placed on the root
window:
message = tk.Label(root, text="Hello, World!")
Code language: Python (python)
The following statement positions the Label
on the main window:
message.pack()
Code language: Python (python)
Note that you’ll learn more about the pack()
method later. If you don’t call the pack()
method, Tkinter still creates the widget. However, the widget will not be visible.
Fixing the blur UI on Windows
If you find the text and UI are blurry on Windows, you can use the ctypes
Python library to fix it.
First, import the ctypes
module:
from ctypes import windll
Code language: Python (python)
Second, call the SetProcessDpiAwareness()
function:
windll.shcore.SetProcessDpiAwareness(1)
Code language: Python (python)
If you want the application to run across platforms such as Windows, macOS, and Linux, you can place the above code in a try...finally
block:
try:
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
finally:
root.mainloop()
Code language: JavaScript (javascript)
On Windows, the code in the try
block will work properly. However, on macOS or Linux, it will fail. However, the code in the finally
lock will always execute that displays the main window.
Summary
- Import the
tkinter
module to create a Tkinter desktop application. - Use
Tk
class to create the main window and call themainloop()
method to main the display of the main window. - In Tkinter, components are called widgets.