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!
Young Ruth Brown Matthew Thomas Helen
Your tutorials are amazing . How far you have been learning python ? When did you start python coding ?
You can explain the code for upi application building for fake logo detection system
Good information
Really! Really! Beautifull, Thanks!!!!!!!!!!! Mariya!!!!
thank you dear
The best GUI tutorial I ever seen on youtube.
Really thank you 👍👍👍👍👍👍👍
How long did it take you to master all these modules? 😩😩
You are truly the best teacher for this out there. Thank you.
Great video Mariya, thank you!
Great.
Nobody can explain it better than Mariya does. Thank you.
love your work simple easy to understanding
I can't believe you have an exact video of something I'm trying to do. This is amazing. Thank you
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 🙂
I hate this subject of python I almost got failed in its exam 😫 which is the best YouTube channel for learning python .
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.
this tutorial is amazing!
Very Good tutorial, “CD” stands for “Change Directory” btw.
Which python are you using?
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