Introduction To GUI With Tkinter

In this article, you are going to learn how to create GUI apps in Python.

What is Tkinter?

Tkinter is Python’s de-facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk. Tkinter is not the only GuiProgramming toolkit for Python. It is however the most commonly used one.

  • Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.
  • GUI is nothing but a desktop app that provides you with an interface that helps you to interact with the computers and enriches your experience of giving a command (command-line input) to your code.

What Is Tkinter Used For

This framework provides Python users with a simple way to create GUI elements using the widgets found in the Tk toolkit. Tk widgets can be used to construct buttons, menus, data fields, etc. in a Python application. Once created, these graphical elements can be associated with or interact with features, functionality, methods, data or even other widgets.

How To Download Tkinter For Python 3

Step 1 –  Make sure Python and pip are preinstalled on your system.

  •  Type the following commands in the command prompt to check if python and pip are installed on your system.
# Check Python version
python --version

# Check pip version
pip -V

Step 2 – Install Tkinter using pip.

pip install tk

Install Tkinter for Linux using the following command.

sudo apt-get install python3-tk

 

Tkinter is a good choice because of the following reasons:

  • Easy to learn.
  • Use very little code to make a functional desktop application.
  • Layered design.
  • Portable across all operating systems including Windows, macOS, and Linux.
  • Pre-installed with the standard Python library.

Let’s Start to Create a GUI window using Tkinter.

The first thing you need to do is import the Python GUI Tkinter module:

import tkinter as tk

A window is an instance of Tkinter’s Tk class. Go ahead and create a new window and assign it to the variable window.

window = tk.Tk()
window.mainloop()

When you execute the above code, a new window pops up on your screen.

Now that you have a window, you can add a widget. Use the tk.Label class to add some text to a window.

window_label = tk.Label(text="Window Label")
window_label.pack()

.pack() = The Pack geometry manager packs widgets relative to the earlier widget. Tkinter literally packs all the widgets one after the other in a window. We can use options like fill, expand, and side to control this geometry manager.

Output:

 

I hope you’ll get impressed by the features of Tkinter. So let’s start coding within the next blog. If you’ve got any questions regarding this blog, please let me know in the comments.

Submit a Comment

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

Subscribe

Select Categories