Python 3 – GUI Programming (Tkinter)
Python is a high-level programming language that is known for its simplicity and readability. One of its key advantages is its ability to interact with other programs and platforms. Python’s versatility includes the fact that you can use it to develop Graphical User Interfaces (GUIs). GUI programming in Python creates interfaces that make it easy for users to interact with computer systems. This article explores GUI programming in Python 3 using the Tkinter module.
What is Tkinter?
Tkinter is a module library in Python used to build graphical user interfaces. It is one of the most commonly used modules for this purpose. Tkinter allows developers to create applications with buttons, labels, text boxes, menus, and other elements of a typical GUI. It works on a variety of platforms including Windows, macOS, and Linux.
How to Install Tkinter?
Tkinter comes preinstalled with Python 3. Therefore, there is no need for installing it when working with Python 3. However, you can confirm if it is available in your Python environment by running the following code:
import tkinter
If the import runs successfully, then you are good to go. However, if it fails, it means that your Python environment doesn’t have Tkinter. In this case, you can install it manually using PIP by running the following command:
$ sudo apt-get install python3-tk
Creating a Window
The first step when creating a GUI in Tkinter is to create a window. You can do this by importing the tkinter module, creating an instance of the Tk()
class, and then calling the mainloop()
method to display the window. Here is the sample code:
from tkinter import *
root = Tk()
root.title("My Window")
root.geometry("400x400")
root.mainloop()
The first line imports the tkinter module. The Tk()
class creates a new window instance, and title()
sets a title for the window. geometry()
specifies the size of the window in pixels. Finally, mainloop()
draws the window on the screen.
Adding Widgets
Widgets refer to the UI elements that make up an application. Tkinter provides different widget classes such as buttons, labels, text boxes, and menus. To add a widget to a window, you create an instance of the widget class and use a geometry manager to position it on the window. A geometry manager determines the size and position of the widget relative to other widgets in the window.
Here is an example of adding a button widget to a window:
from tkinter import *
root = Tk()
root.title("My Window")
button = Button(root, text="Click me")
button.pack()
root.mainloop()
The code creates a button widget and uses the pack()
geometry manager to display the button in the window. The syntax for creating widgets is similar across all widget classes. Here is a table of commonly used widgets and the syntax for creating them:
Widget | Syntax |
---|---|
Button | Button(root, text="Click me") |
Label | Label(root, text="Hello World") |
Entry | Entry(root) |
Text | Text(root) |
Menu | Menu(root) |
Binding Functions to Widgets
In Python 3, you can bind a function to a widget such that the function is called when the widget is clicked. For instance, when a button is pressed, a function can be executed. Here is an example of binding a function to a button:
from tkinter import *
def clicked():
print("Button clicked")
root = Tk()
root.title("My Window")
button = Button(root, text="Click me", command=clicked)
button.pack()
root.mainloop()
When the button is clicked, the clicked()
function is executed, which prints “Button clicked” in the console.
Conclusion
In a nutshell, GUI programming makes it easy for users to interact with computer systems. Python 3 provides powerful tools for creating GUIs using the Tkinter module. With Tkinter, you can create windows and add widgets such as buttons, labels, menus, text boxes, and more. You can also bind functions to widgets to create interactive applications. We hope this article has given you a good introduction to GUI programming in Python 3 using Tkinter.