Tkinter Window Example

Tkinter Window Example

In Tkinter, you can create a window using Tk class of tkinter package.

First things first, import tkinter package.

import tkinter as tk

Now, let us create a Toplevel widget, say window, using Tk class.

window = tk.Tk()

We have the window. Let us call the mainloop() of Tk. mainloop() is kind of a indefinite loop to display the window until the window is closed.

window.mainloop()

Let us bring all the above pieces together to create our first program of creating a window using tkinter. This example is as simple as it can get.

Python Program

import tkinter as tk

# Create the main window
window = tk.Tk()

# Run the application
window.mainloop()

Output window in Windows

Output window in MacOS

Tkinter window example - MacOS
Tkinter window example – MacOS

Summary

In this Python Tkinter tutorial, we learned how to display a window using Tkinter, with examples.

Code copied to clipboard successfully 👍