How to Create a Basic GUI Application in Python with Tkinter



Python


In this article, we go over how to build a very basic GUI application in Python with Tkinter.

Tkinter is the Python interface to the Tk GUI system. It is not specific to Python, as there interfaces to it from many different languages. And it operates on pretty much any operating system, including Windows, iOS, and Linux.

Tkinter comes with Python, so there is no need to install it. It already comes with Python. It is the most commonly used tool for creating a GUI with Python.

So let's now go into the program that will create a basic GUI that has the text, "Hello" inside of the window.

The code for this program is shown below.



Let's go over this code now.

So, basically with the statement, from tkinter import *, we import all functionality and tools from tkinter, such as Labels, buttons, etc.

We then have to create a Tkinter object. This is done by the line, root= Tk()

Now in this variable root, we have created a Tkinter object. You must create a Tkinter object to get a GUI. This root variable represents the actual GUI, which is a Tkinter object.

We then create an instance of the class Label. The first argument in label is root, our Tkinter object and the second argument is "Hello" (what we display in the root object). We then use the method, pack(), to place the instance of Label in the Tkinter object, root.

The method, pack(), contros the layout of items in the window. We can use all types of alternative layouts.

But to make things simple, we just do this.

And after running this code, this is what we get as output.

Basic Tkinter GUI in Python

Of course, we can do more advanced things, such as add buttons, do different layouts, set the width and height of the GUI application, add text fields, but this is just the first tutorial on how Tkinter works and how to create your first GUI application with it.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...