Create a Window Using Tkinter

This page explains how to create a window in Python using the Tkinter module.
Tkinter is a Python module that allows you to create GUI applications.

Setting Up the Environment

mkdir PythonTuto cd PythonTuto # For Linux (module venv of Python) python3 -m venv env # For Windows python -m venv env # Activate the virtual environment # For Linux source env/bin/activate # For Windows env/Scripts/activate # Install Tkinter pip install tkinter # Or for Linux sudo apt install python3-tk

Python Code

import tkinter as tk # Traditional form root = tk.Tk() root.geometry("500x500") root.title("Tkinter Tutorial") root.mainloop() # Custom function def createWindow(title, size): window = tk.Tk() window.geometry(size) window.title(title) return window #In main file import window as win root = win.createWindow("A Tk Window", "500x500") root.mainloop()