Python Tkinter tutorial – part 1

Filed Under: Python
Python Tkinter Tutorial Part1

Introduction to Tkinter

There are multiple ways to interact with a program: either through the command line or through GUI. GUI is a desktop application that provides a user interface that helps interact with the computer or any electronic device visually through graphical icons. This makes GUI achieve a wide user-base with our software.

Python provides numerous packages like Kivy, PythonQt, wxPython, Tkinter, etc. for creating GUIs. Among these popular packages, the de-facto standard package supported by Python to build GUI applications is Tkinter.

Tkinter is the standard and inbuilt Python GUI library that provides users an object-oriented interface to the Tk GUI toolkit. Tkinter module is the most preferred GUI building interface by programmers due to its simplicity and ease of use.

Programmers can create GUI apps rapidly using Tkinter. In this tutorial, we will be using Tkinter to create GUI applications.

Creating a sample Window

We just discussed that while using a GUI, a user interacts with the elements of the interface that are called “widgets”. Widgets like buttons and checkboxes allow the user to interact with the program. Other widgets like windows and frames are used as containers for placing other widgets. Tkinter is readily available with standard Python 3 + installation. So, the user need not worry about the installation of the module. The user needs to just import the module, using:

import tkinter

Using Tkinter, creating a sample window is super easy, regardless of the operating system you are using. We can quickly create a window by the following lines of code.

import tkinter
root_window = tkinter.Tk()
root_window.mainloop()

After the first step of importing the Tkinter package, the next step is to create the root window widget for any application. There should be only one main window widget in the interface. All other widgets will be of lower hierarchy than the root. Hence, the main root window widget has to be created prior to creating other widgets.

As you see in the above code, we initialize the root’s `mainloop`. This calls the mainloop() function, which calls the endless loop of the main root window. This window will wait for any kind of user interaction until their quit() method is called, or in case the user closes the window. The above code creates a sample Window widget as follow:

Output

Create a Window usingTkinter

Creating a label widget

Now, we have learned to creating a Windows widget. Next, let’s add a simple Label widget that displays a sample text to the main root window. The below example creates a simple label widget. We can create a label using the label class as follows:

label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter")

The label class takes 2 mandatory arguments namely the label’s desired parent widget(i.e. the root_window) and the text to be displayed by the label. Next, we can set the position of the label using the grid function as follows:

import tkinter
root_window = tkinter.Tk()
label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter")
label_widget.grid(column=1, row=1))
root_window.mainloop()

Below is the sample output that displays a window having the label we just created. Isn’t that simple?

Output

Create a Label using Tkinter

Customizing the label widget

We can customize the font size of the label by changing the font style as below:

label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter", font=("Arial Italic", 40))

As you see, we can use the font parameter to change the font size of widgets.

Changing the Label Font using TkintetOutput

Customizing title and size of window

As seen in the above example, the window size is very small. We can set the default size of the window using geometry function, which takes the width and height of the window as parameters(in pixels).

root_window.geometry('950x200')

The above line of code resizes the window size to the above-mentioned parameters.

Similarly, we can add title to the window widget using the title function. For example:

root_window.title("Tkinter tutorial")

Let’s customize the window size and title by adding the above 2 lines of code to our existing application.

Output

Change Window Size using Tkinter

Conclusion

We have created a bare minimum GUI having a window and a label widget using Tkinter, in a jiffy. Python provides the Tkinter library along with its installation. Building your first window is just a step away. So, what are you waiting for? Create a GUI for your software that can be used even by non-expert users by using Tkinter.

Further Reading: Tkinter tutorial – part 2

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages