Step by Step Guide to Creating a GUI App using Tkinter

Posted by


Tkinter is a standard GUI (Graphical User Interface) package in Python, that comes with the Python standard library. It is simple to use and makes it easy to create GUI applications. In this tutorial, we will learn how to create a GUI app with Tkinter in a step-by-step manner.

Step 1: Install Tkinter
First, make sure you have Tkinter installed on your machine. Tkinter comes pre-installed with Python, so you don’t need to install anything extra.

Step 2: Import Tkinter
To create a GUI app with Tkinter, you first need to import the Tkinter library. This can be done by adding the following line at the top of your Python script:

import tkinter as tk

Step 3: Create the main window
The main window in a Tkinter GUI application is known as the root window. You can create the root window by instantiating an object of the ‘Tk’ class from the tkinter module as shown below:

root = tk.Tk()

Step 4: Add widgets to the window
Now that we have created the main window, we can start adding widgets to it. Widgets are the basic building blocks of a GUI application, such as buttons, labels, text boxes, etc.

For example, let’s add a label widget to the root window:

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

The pack() method is used to display the label widget on the window.

Step 5: Run the main loop
Finally, we need to run the main loop of the Tkinter application. This loop listens for events from the user, such as mouse clicks and keyboard inputs. You can start the main loop by calling the mainloop() method on the root window object:

root.mainloop()

This will keep the GUI application running until the user closes the main window.

Putting it all together, here is the complete code for a simple Tkinter GUI application that displays a label saying "Hello, Tkinter!":

import tkinter as tk

root = tk.Tk()

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

root.mainloop()

Run this code in your Python environment, and you should see a window pop up with the label "Hello, Tkinter!" displayed in it.

Congratulations! You have successfully created your first GUI app with Tkinter. From here, you can further explore Tkinter’s capabilities and build more complex GUI applications by adding different widgets and functionalities. Happy coding!

0 0 votes
Article Rating

Leave a Reply

38 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@NormanSantiago-z1q
2 hours ago

Young Ruth Brown Matthew Thomas Helen

@ChrishnaBombay-c8x
2 hours ago

Your tutorials are amazing . How far you have been learning python ? When did you start python coding ?

@madpallilalitha7147
2 hours ago

You can explain the code for upi application building for fake logo detection system

@digitalmachine0101
2 hours ago

Good information

@aaronjaffetgutierrez1345
2 hours ago

Really! Really! Beautifull, Thanks!!!!!!!!!!! Mariya!!!!

@umarqayyum1098
2 hours ago

thank you dear

@doodo7381
2 hours ago

The best GUI tutorial I ever seen on youtube.
Really thank you 👍👍👍👍👍👍👍

@Luke_Homas
2 hours ago

How long did it take you to master all these modules? 😩😩

@danielx40
2 hours ago

You are truly the best teacher for this out there. Thank you.

@kapibara2440
2 hours ago

Great video Mariya, thank you!

@MiguelFernandez-kk2li
2 hours ago

Great.
Nobody can explain it better than Mariya does. Thank you.

@MakPich-lf6wd
2 hours ago

love your work simple easy to understanding

@phan5302
2 hours ago

I can't believe you have an exact video of something I'm trying to do. This is amazing. Thank you

@skinnyboy2386
2 hours ago

if you doing this in 2023 and your code is not working here is what you nee to exchange to make it work.

first raplace this line of code:
read_pdf = PyPDF2.PdfFileReader(file)

with this:
read_pdf = PyPDF2.PdfReader(file)

then the second line of code that you will need to replace is:

replace this:
page_content = page.extractText()

with this:
page_content = page.extract_text()

this should be all you need for your text to work. Enjoy coding have 🙂

@Ordainer30
2 hours ago

I hate this subject of python I almost got failed in its exam 😫 which is the best YouTube channel for learning python .

@kennywong888
2 hours ago

This is an excellent video helping people to learn Python. Thank you very much @PythonSimplified!! This video was made sometime ago, I can follow the video without error until 18 min. After that when it goes to PyPDF2, errors comes. The error should due to the changes and update of PyPDF2 package. To the best of my effort, I find that there should be a Naming Adjustments, the class PdfFileReader become PdfReader. Hopes that my comment and update can help other newbies.

@adipurnama_workshop
2 hours ago

this tutorial is amazing!

@justchris846
2 hours ago

Very Good tutorial, “CD” stands for “Change Directory” btw.

@siyabongawittes7928
2 hours ago

Which python are you using?

@Jamesucht
2 hours ago

In case anyone is watching in 2023, there are a few things in PyPdf2 that have been deprecated and now use a different function;

PdfFileReader is now PdfReader
reader.getPage(pageNumber) is now reader.pages[page_number]
extractText is now extract_text

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