All Posts

Python Tkinter Program

Python Tkinter Program

Python Tkinter is an in-built module used for creating simple GUI applications. It is the commonly-used module for GUI applications in Python language. We don’t need to worry about installing the Tkinter module, as it comes with Python by default.

Python Tkinter

Tkinter is a standard Python library for creating GUI(Graphical User Interfaces) applications. It is simple, lightweight, and easy to use, making it a popular choice for simple desktop applications.

The main steps of using the module are as follows:

  • Import Tkinter module
  • Create the GUI application’s main window
  • Add Widgets
  • Enter the main event loop

To begin with the GUI program, we must import the Tkinter module. After that, we create the main window where we perform operations and display visuals. Next, we add the widgets and the main event loop.

Python Program

Let’s understand the concept with a sample Python program. This program uses the Tkinter Python module to create the GUI application.

# Python Tkinter Program
import tkinter as tk

window = tk.Tk()

# Title of the window
window.title(“Tkinter Program”)

# display labels on the window

label1 = tk.Label(window, text = “Hello, World!”).pack()
label2 = tk.Label(window, text = “www.TestingDocs.com”).pack()

window.mainloop()

 

Python Tkinter Program

Program Output

Save the Python program before running it. We need to save the program using the .py extension.

Python Tkinter Program Output

After importing the Tkinter package, we define a window and give it a title, which appears on the tab when the application is opened.

We have made some final additions to the code. Two labels have been added that will be displayed on the window. Additionally, an event loop has been included in the code. This loop ensures the window remains displayed until we manually close it. The loop runs in the background on an infinite loop.

 

Python Tutorials

Python Tutorial on this website can be found at:

https://www.testingdocs.com/python-tutorials/