How to import Tkinter in Python

In Python, Tkinter is a built-in GUI library to create cross-platform GUI applications. This library is lightweight and easy to use compared to other GUI libraries.

In this Answer, we’ll learn how to import the Tkinter module into the Python code. Install the latest Python as a prerequisite using the link.

Syntax

We’ll use Python’s import module syntax, as shown below.

import module_name as modeule_alias_name

Import Tkinter

As shown below, we import module Tkinter with an alias name tk.

import tkinter as tk

Example

Let’s take a look at an example of this. After clicking the “Run” button, go to the link below.

#import tkinter module
import tkinter as tk

#create window
window = tk.Tk()

#provide size to window
window.geometry("300x300")

#add text label
tk.Label(text="Hello from Educative !!!").pack()

window.mainloop()

Explanation

  • Line 2: We import the Tkinter module with an alias name as tk.

  • Line 5: We create a window.

  • Line 8: We provide window size using the geometry() method.

  • Line 11: We create text on the window using the label method.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved