Python provides an easy way to create powerful graphical applications with very few lines of codes. The default library for creating Python graphical user interface (GUI) applications is known as Tkinter. We will be discussing how to use Tkinter below.
What is Python Tkinter?
Tkinter is a cross-platform framework that comes bundled with the standard Python libraries. This means that you can deploy the same code on different environments (e.g. Windows and Linux), while still getting the look and feel of that environment in your application.
In this guide to GUI development, you will learn how to create your first Python GUI application using Tkinter.
How to Create a GUI Window in Python with Tkinter
A window is a container on which graphical components (widgets) of your application can be placed. To begin creating your graphical interface, you will first need to import the Tkinter library. You can do so with this command:
>>> import tkinter as tk
At the moment, you can enter these commands in your Python shell. Later on, you will see how to do so in a file. There are a few subtle differences between the two methods that will be shown later.
After importing Tkinter, you can now create your first window. Simply do this by instantiating Tkinter’s Tk class and assigning it to a variable, as shown here:
>>> window = tk.Tk()
Upon running the command, a window will show up on your screen. Notice that it has the title tk. This is the default title:
Python developer can choose to define their own title by passing a string to the title() method:
>>> window.title("Python GUI")
Your window will automatically update to reflect the new title. Give it a try to see the results.
How to Add Widgets to a Python GUI with Tkinter
Now that you know how to create a window, it is time to learn how you can add widgets to it. The general syntax for defining a widget with Tkinter in Python is:
var = tk.WidgetName()
In other words, you instantiate a widget class and then assign it to a variable. Remember to include tk. before the widget name so that the interpreter knows that you are getting it from the Tkinter library.
The first widget you need to learn is the Label widget. It enables you to display text to the user. The user can not edit this displayed text:
>>> label = tk.Label( text = "Tkinter is cool.")
After running this command, no change is reflected on your window. This is because you have not yet added the widget to the window. There are a number of ways a programmer can do so. However, the simplest way you can do so for now is using the pack() method. Using this method will allow the application to create the smallest possible window, while still maintaining the widget:
The text attribute in Label() allows you to indicate the text that you would like to have displayed. You can also modify other aspects, such as the background color, foreground color, and dimensions (width and height) of your label:
>>> window = tk.Label( text = "Tkinter is cool.", fg= "yellow", bg= "black", width=20 height=15)
Notice the attributes fg and bg. These are shorthand for foreground and background color, respectively. If you do not specify these values, then Tkinter uses your system’s default background and foreground color. Ideally, this is black and white (unless you changed these in your system settings).
It is also worth knowing that Tkinter supports a large number of color schemes, including the hexadecimal color codes.
Python GUI and Tkinter Examples: Working From Files
As mentioned earlier, creating your GUI application in a file is a little different than using the Python shell prompt. You need to use the mainloop() at the end of your code in the file. Otherwise, you will not see the graphical interface after running your code. Here is how you create the same GUI application in Python with Tkinter using a file instead of the prompt or Python IDLE:
import tkinter as tk window = tk.Tk() window.title("Tkinter App") window = tk.Label(text="Tkinter is cool.", width=80, height=25) window.pack() window.mainloop()
The mainloop() method creates an infinite loop that waits for any event from your application. These events can include actions such as clicking a button or pressing a key. The loop stops when you close the window.
Note that this method blocks any code after it from running until the window closes. Therefore, it should be put at the end of your code.
Tkinter Alternatives for Python GUI Developers
Tkinter is not the only Python GUI library that you can use. Actually, a lot developers in the Python community see Tkinter applications as having an outdated feel. This should not worry you, however. Tkinter is a lightweight framework that delivers well on functionality, and is a great choice for developing GUI-based programs.
If you are looking to explore some other frameworks that provide a rather modern look, then you can look at the Python Kyiv and PyQt GUI libraries.