In the last video, we saw how to Update data & Delete data from the database in Python.
Today we will talk about the Tkinter GUI based framework.
Actually Tkinter works for GUI.
We will see how to make applications through GUI in Python, how the framing is made, how the buttons are used as well as how to perform it practically, how to use it, how to import it, how to use its Widgets etc.
I’ll tell you all about it with both the theoretical & practical parts.
First of all, we will see what GUI base is.
GUI means Graphical User Interface.
Graphical User Interface is with what the user interacts with.
For instance, if you open Google, it is a user interface.
Now Tkinter is used to make Desktop based applications in Python.
It is one type of GUI based library used in Python.
Tkinter is the only library in Python which is fast, makes the GUI based application easily & most commonly used for GUI based application in Python.
There are many more GUI based applications but if you want to make a GUI based application in Python, Tkinter is the most used one.
There are particular steps on how to make it...let’s see those steps first.
Steps to Create GUI Application.
First of all you have to import a Tkinter module. Import a Tkinter module.
Then create the main window in Tkinter.
The main window is where we will add all the widgets, that is our buttons, labels etc.
3rd is Add any number of widgets to the main window.
So once your window is made, the 3rd step is to add widgets.
Suppose you want a button then you’ll keep the button in the main window.
If you want labels, you can keep labels in the main window.
If you want a text box then you can keep the text box in the main window.
Then comes the 4th step. That is to trigger the event on the widgets.
Suppose you created a button. If you have to perform anything at last by clicking the button, that last step is the event trigger.
Click event, you have to enter something, you have to run something, all of these are end movements.
First of all, what are Import & methods of Tkinter.
How will you import Tkinter?
You only have to write Import Tkinter.
It will automatically get imported into the library.
There are 2 main methods.
1 is Tk which makes your main window.
The main window is made by Tk method.
Another is mainloop method.
Let’s understand both of these methods first.
Tk is used to make the main window.
Meaning the outer boundary of the GUI based application will be made by Tk method.
Next is the Main Loop.
What the main loop does is, suppose your application is ready, you’ve finished the coding and now you have to run it.
Without this method you cannot run your application.
Why? Because you can run your application only & only through this method.
This method works like an infinite loop.
It’ll keep running your application in a loop until you stop it.
Moving on to see the widgets of Tkinter.
Such as Button. What does button do?
Button is used to add buttons.
Entry is used to take user input that is the text boxes.
Then Label. Labels are such as first name, last name.
We have to use the Label method for that.
There are particular syntaxes here.
Such as a Button for buttons. Master and option = value.
Master means we have to add in the window.
We can add the particular attributes in Option & value.
Then Entry method syntax.
Entry method syntax works in the same way where you have to write Entry, master means add your window variable.
Then its option & value. Attributes & values.
Label method for Label. Master, Option & Value.
This is how the widgets are used.
If I give you an example of Screen...I have an example here.
Roll no. First name. Last name. Email. Mobile.
What are these? These are Labels. Alright.
I’ll note these down in an easier manner for you.
I have labelled here for you to understand clearly.
Labels. These are the labels.
The below ones are buttons.
This is the main window.
All the labels, buttons, textboxes are on the Main window.
This is a text box. It is called Entry in Tkinter.
So this is how the widgets work in Tkinter.
Let’s see practically how they are made, how the design is created etc.
Then you’ll be able to understand it more clearly.
I opened my Jupyter here.
First of all, we will see how to import.
You simply have to write from Tkinter import.
we want all the methods so we will write a “star” here.
This means that we don’t need to separately import different methods.
First of all, we have to make a window.
What will we do to make a window?
Suppose I take a variable R.
I’ll write here “Creating a main window”.
You only have to call the Tk method.
Tk method will be stored in R.
Our main loop. Main Loop method called.
We will use the Main Loop method to run it., which I have told you in the past.
As you can see, our frame opened up right here.
This is our frame where we made our Tk method through which this main window was created & it was stored in R which is working with the help of the main loop.
It is in the running mode right now, I’ll click here to stop it.
This is how your main window is made.
Now I want to fix its size, keep a background & its Tkinter…
See this frame, I want a different name instead of Tk.
Let’s see how to change the name.
Creating Customised Window.
How will we do it?
First of all we need to import Tkinter.
From Tkinter import star.
Next up, we have to create a window.
Creating Main Window, where we took the R variable & called the Tk method.
Now I want to customise the size of my window.
There is a method called R . (dot) geometry method.
Geometry method will decide the size of the window.
Suppose I want a screen of 500 into 500 pixels.
500 by 500.
And I want to change the title of my window.
There is a title method.
Which method? Title method.
I’ll write down “My Title”.
Now I want to change its background.
R . (dot) configure method.
There is a property named BG here in which I wrote Yellow.
Here, we customised the window.
Now we just have to run it.
I’ll call the Main Loop method.
Let’s run it.
A screen of 500 by 500 is made where the title of the window is what I wanted, the one I entered here.
The background is Yellow as well.
Suppose I take Orange instead of Yellow here.
And then I run it…
See? The colour changed to Orange.
So this is how you can customise your window in Tkinter.
Now I’ll decrease its size.
Suppose 400 by 400.
I’ll run it again and see...the size decreased.
So this is how you can customise your window in Tkinter.
Now, I wish to add widgets into this, in our main window.
Adding Widgets into Window.
First of all, we need all of these steps so I’ll copy & paste here.
Now we have to add our widgets.
We need a label first.
Suppose I need a roll number label so RN & used the Label function.
Now, our main window is under here.
So our master is R.
And its text. What text do I want? Roll No.
I’ll have to place that label at a certain place in the window.
There is a place function available for that.
Our label will be stored in RN.
Rn . (dot) place function.
1 will be value X, another will be value Y.
We will pass both the values.
Suppose I want to give X’s value 20 & Y’s value 20 as well.
We have to use the X axis & Y axis.
I’ll run it again & you can see the Roll No. Label.
So this is how you can take Labels.
For instance, I’ll take the label FN for First Name.
Label R, text. First name.
Now RN . (dot) place.
Where? Right below the Roll No.
We already have a position of 20 in X.
So I’ll write X = 20.
We only have to change the position of Y.
We have the X’s position & we only have to change the position of Y.
Suppose I take Y = 50. I’ll run it.
Okay sorry, we had to take FN here instead of RN.
It changed the Roll No. as well because of that.
Did you see it came exactly below the Roll No.
If you want it more downwards, change Y to 60.
See? You can keep the labels in this way.
I want multiple labels in this way.
This is my first name, this is my last name and email.
I changed this to LN. As well as the Label to Last name.
I’ll change this to 100.
I took a label for Email. I'll write Email.
And change the position to 140.
Let’s run it.
Okay we have to change these too.
This is FN, this will be LN & this will be EM.
Now we have 4 labels.
Now we have to make text boxes right beside these 4 labels.
Let’s see how to do it.
I’ll write down “Adding Labels in Name Window”.
I’ll add text boxed now.
Adding Entry box into Main window.
We will place the text boxes exactly like we did for Labels.
I’ll write here ERN, which is Entry of Roll No.
We will use the Entry method here.
Where we will write R.
It’s not necessary to write R here.
Then set the position here. Which we set through the place method.
Now, the most important thing.
We’ll have to move Y & X both.
Meaning we have to change both of their values.
How will we do it?
X = 100. And Y = 20.
In the above steps, X used to be steady while Y’s position used to keep changing.
Here, Y’s position will remain the same or change a little bit while X will keep changing.
As I run it, you can see that the textbox is exactly in front of the Roll no.
We want multiple text boxes in the same way.
I’ll copy paste this here for the multiple textboxes.
We have 4 labels so I copy pasted 4 times, alright.
Let’s change their names.
Enter F name.
We’ll change the position as well.
Suppose we’ll take the place 100.
Change the Y to 60.
Before we changed Y’s value & in the same way we changed X here.
The Y’s value will remain the same.
Change this to ELN. Change the name here as well.
Here too. Will keep 100 here.
Change this to 100.
Entry of Email. We’ll change its position to 140.
Let’s run it now. It’s all ready.
Did you see we got all the textboxes here right in front of the labels.
So this is how you can add the text boxes in front of the labels.
Understood so far?
Now I have to add the buttons.
I wish to add the buttons.
I’ll write here Adding Buttons into the main window.
Let’s see how to do it.
We’ll take button 1 here.
We have a button method to add a button here.
We are going to add it in master so I’m going to add R here.
Which text do we want to keep here?
I’ll keep button 1.
Now we want to keep a button colour as well.
We have BG for colour.
We’ll keep the colour White.
Now we’ll have to place them.
Always remember that you have to set a place for all the widgets that you add.
Button1 . (dot) place.
Suppose I placed it at the end. So X = 20.
Y I’ll take much below 200.
Did you see we got the button 1?
So this is how you can add buttons in the main window.
This is how Tkinter works using Widgets, using Customised Window and with your main window.
Okay, Thank you guys.
If anyone has any doubts or want any files, you can click on forums.learnvern.com.
And download or ask any questions over there.
Okay? Thank you everyone.
Tkinter is Python's standard GUI library. When Python is used in conjunction with Tkinter, creating graphical user interfaces is quick and simple. The Tk GUI toolkit has a sophisticated object-oriented interface called Tkinter. Add the Tkinter module to your project.
TKinter is a GUI module for the Python programming language. It is a library which provides a toolkit of classes for creating graphical user interfaces.
Tkinter is a Python GUI library which is designed to be easy to use, and it operates the same way as the GUI toolkit of the Tcl/Tk language. It provides a number of frames that you can add widgets to, such as a menu bar, a button bar, and several kinds of window.
Tkinter is one of the most widely used Python GUI libraries for desktop application development. It's a hybrid of the TK and regular Python GUI frameworks. Tkinter provides a wide range of widgets for use in graphical user interface applications, including labels, buttons, text boxes, and checkboxes.
Tkinter is a toolkit for developing graphical user interface (GUI) applications. Python comes with Tkinter and you can use it without any additional install.
Arthik Nashik Chaware
I am satisfied to python and html faculty
Great Platform for learning.
the course is very good teaching and practical assignment