Tkinter window

A window is a top-level container that holds all other containers and widgets. In this tutorial, we are going to create one empty window using Tkinter.

Example

The following python script creates one empty window.

You need to write following code in any text editor and then save it with .py extension. To execute the application type py WindowExample.py command in terminal.

from tkinter import *

window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

window.mainloop()

Output

Explanation

The Tkinter module including the Tk toolkit must always be imported it contains all the widgets and other necessary things.

from tkinter import *

To initialize Tkinter you need to create a Tk root widget. This widget is a window with a title bar and other decorations provided by the window manager. Once it is initialized we can call setter methods to set various of its property, here title() method is used to set the title for window and geometry() methods set the size of the window.

window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

The last line containing the mainloop() method on the window object, calls the infinite loop of the window. Therefore, it waits for the user’s operation until the window is closed.

window.mainloop()