Tkinter is Python GUI interface. Here we will study how to create Python tkinter window.
Below 3 line of code will display the tkinter window
import tkinter root = tkinter.Tk() root.mainloop()
When we run the above 3 lines , following window will appear

Let’s discuss each line
First step is to import the tkinter module using import keyword
import tkinter
next step is to call Tk interface from tkinter.
root = tkinter.Tk()
We call Tk() interface from tkinter module and assign it to some variable root.
Now, we will write
root.mainloop()
The purpose to write root.mainloop is to halt the tkinter program or GUI.W
Confused ? did not get. Let me explain
When we execute the following program, print ("After tkinter")
will execute after close the tkinter window. It shows that root.mainloop()
halts the Python code till tkinter runs.
import tkinter root = tkinter.Tk() root.mainloop() print ("After tkinter")
What happend if we dont write root.mainloop()
If we don’t write root.mainloop()
, tkinter window will not display and next lines of code will executed. see below example.
import tkinter root = tkinter.Tk() #root.mainloop() print ("After tkinter")
In this example, tkinter window will not displayed as we commented the root.mainloop()
.
How to label tkinter window
we can assign name to tkinter window. To do this, we have to set title. see below
#Python Tkinter window example import tkinter root = tkinter.Tk() root.title("Example") root.mainloop()
