The simplest GUI using Python
You have always wondered what else can we do with Python rather than just some basic programs and games.
Well, your wait is over because in this tutorial you’re going to see the simplest and easiest code to build a GUI.
GUI stands for Graphical User Interface. It allows user interaction through graphical interfaces(icons and indicators). tkinter is the standard Python interface to the Tk GUI toolkit shipped with Python.
The very first and the most important step of creating a GUI using python is to import the tkinter module.
from tkinter import *
Build the simplest GUI using Python
Let’s see the full code for creating a very simple GUI.
from tkinter import * root= Tk() var= Label(root, text="It works!") var.pack() root.mainloop()
In Line 3,
‘root’ is basically an object and ‘Tk()’ is a class that is imported from the tkinter module. This line is important for creating a blank window.
In Line 4,
‘var’ is an object we have to create. The ‘Label’ consists of two parameters i.e,
Label(where the content should be placed, content)
In Line 5,
The object (var) is packed into the window i.e, it is displayed on the very first space where it can fit in.
In Line 6,
The ‘root’ that contains all the objects is bound with a function called ‘mainloop()’. If we don’t use this function the output will just pop up for only a few nanoseconds. This function gathers all the lines of code and puts them in a loop so that the output is continuously displayed on the screen until the user clicks on the close window button.
You may also learn:
Bind a function to a Tkinter button