In this Tkinter python GUI tutorial, we will learn to create a window using Tk() class. Later we will learn about fixing the size of the window and set the title to application. To start first, you need to import tkinter package for the widget of GUI. All file should save as python Files with .py extension.
First Example of Tkinter
Let’s start with the first example of Tkinter, to create a basic application of GUI using Python Tkinter.
from tkinter import *
win=Tk()
win.mainloop()
Now save the code as python file (.py) and run the program.
Let’s Discuss the Steps
1. In the above example, the first line is to import(call) tkinter module, which store all the classes related to Tkinter learning. You can learn more about module in Python here.
from tkinter import *
2. Second line is to create object of Tk() class, which is actually use to create a window.
win=Tk()
3. The last line which calls mainloop() function for endless loop of the window. The mainloop() function is terminated when we click on close button or quit application.
win.mainloop()
One More Example
Next program to add title using title() method, and set size of window using geometry() method of Tk()
from tkinter import *
win=Tk()
win.title("CoderMantra Tutorial")
win.geometry('400x400')
win.mainloop()