Intellipaat Python GUI Programming Tutorial with Tkinter: Step-by-Step Guide

Posted by


Tkinter is a popular Python library for creating graphical user interfaces (GUIs). In this tutorial, we will learn how to use Tkinter to create GUI applications in Python.

Table of Contents:

  1. Installing Tkinter
  2. Hello World example
  3. Creating a simple calculator

  4. Installing Tkinter:
    Tkinter is included with Python so you don’t need to install it separately. However, if you are using an older version of Python, you may need to install Tkinter using your package manager.

To check if Tkinter is installed on your system, open a Python shell and type the following command:

import tkinter

If there are no errors, Tkinter is installed on your system. If there is an error, you can install Tkinter using the following command:

pip install tk
  1. Hello World example:
    Let’s start with a simple "Hello World" example using Tkinter. Create a new Python file and add the following code:
import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="Hello World!")
label.pack()

root.mainloop()

Save the file and run it. You should see a window with a "Hello World!" label.

Explanation:

  • tk.Tk() creates a new main window.
  • tk.Label(root, text="Hello World!") creates a label with the text "Hello World!".
  • label.pack() adds the label to the main window.
  • root.mainloop() starts the main event loop and keeps the window open until you close it.
  1. Creating a simple calculator:
    Now, let’s create a simple calculator using Tkinter. Create a new Python file and add the following code:
import tkinter as tk

def calculate():
    num1 = int(entry1.get())
    num2 = int(entry2.get())
    result.set(num1 + num2)

root = tk.Tk()

entry1 = tk.Entry(root)
entry1.pack()

entry2 = tk.Entry(root)
entry2.pack()

button = tk.Button(root, text="Add", command=calculate)
button.pack()

result = tk.StringVar()
label = tk.Label(root, textvariable=result)
label.pack()

root.mainloop()

Save the file and run it. You should see two entry fields, a "Add" button, and a label that displays the sum of the two numbers entered in the entry fields.

Explanation:

  • calculate() is a function that reads the values from the entry fields, converts them to integers, adds them together, and sets the result in the label.
  • entry1 = tk.Entry(root) creates an entry field for the first number.
  • entry1.pack() adds the entry field to the main window.
  • button = tk.Button(root, text="Add", command=calculate) creates a button with the text "Add" that calls the calculate() function when clicked.
  • result = tk.StringVar() creates a StringVar object that we will use to update the label text dynamically.
  • label = tk.Label(root, textvariable=result) creates a label that displays the sum of the two numbers.
  • root.mainloop() starts the main event loop.

In this tutorial, we learned how to create simple GUI applications using Tkinter in Python. Tkinter is a powerful library that allows you to create complex GUI applications with ease. I hope this tutorial was helpful for you to get started with Tkinter.

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Intellipaat
2 hours ago

▶Want to get Master in Python? Enroll in our Advanced Certification in Data Science & AI here: https://intellipaat.com/advanced-certification-data-science-artificial-intelligence-iit-madras/

👍 Do like, share & subscribe to our channel to get updates on upcoming videos. : t.ly/xqn9

1
0
Would love your thoughts, please comment.x
()
x