Hello again in previous lecture what we learned we learned how we can use labels right.
And in this lesson what we are going to learn we are going to learn how we can use buttons for our applications
and to do what we need to do ? We need to create a new python file right here python file
I will say and I will name it buttons
OK so what.
What is next what I need to do ?
I want to copy all these guys and paste them here.
OK.
And after that I I will remove these guys.
OK.
And here I want to create a text first.
Text equals Qlabel.
Right.
And here what I will say I will say my text OK.
And after that second parameter must be self right.
If I want to display my text I need use self parameter here and after that here what I need to do?
I need to create two buttons.
OK.
And here I will say enter button if I want to create a button what I need to do ? I need to use QPushButton
constructor method.
OK.
And the first parameter will be my text for my button.
And here I will say enter.
This is TAKES OFF MY BUTTON.
AND SECOND PARAMETER must be self.
OK.
And now I want to duplicate this button and control plus the here.
I will name it exit button.
And here I will saye exit
and now our widget ready right one label two buttons.
But what I need to do use? Also I need to use move geometry manager.
Right.
And here I will say text.move for text.
I will say 160 and for x value 50.
Okay.
And that for my button enter button dot move for enter button I will say 100 by 80.
And the for other one for exit button exit button dot move and for x button I will use 200 by 80.
OK now If I run my application.
I can see my images as you see now here.
My label is here and also two buttons they are here right.
But here you see when I click my button and it doesn't do anything right.
So what I have to do.
Don't forget that if you see a button in a GUI application that means this button has a functionality
OK then you click when you hit this button it should do something.
OK.
And for example for entry and exit.
What we need to do is to create a function We need to create a definition and We need to change something.
For example here for this scenario I want to change my text OK.
I need to set a new text value for this guy for this label for enter and also for exit so how can I do that?
to do that I need to create a click and connect function for my buttons.
Ok don't forget this always like that for buttons here what I need to do ? I need to come here and say enter
button dot clicked here without parentheses click and connect.
Okay.
And here what I need to do ? firstly say self.
And after that my definition name and I will name it enter enter function or enter func OK.
And also let's create this one for exit button exit button dot clicked and connect and here and say
self again and exitFunc.
OK so what I need to do? I need to create these functions OK and to create them what to I need do? I need to come here
I need to create a new definition.
OK.
def enter function and as you see now it takes an argument an argument is self OK.
Don't forget it.
If you don't use self parameter here your function will not work.
OK.
And after that here what I want to do I want to set a new value for my text right.
So how can I do that.
I can say text but as you see now here I got an error why is like that? Because here if you want to this
This is by the way UI definition this enter function definition there are different functions.
Okay.
So if you want to use this variable
Or this widget in
Another function that means you have to use self parameter for this widget.
Okay.
This important if you want to use a variable or if you want to use a widget in another function you
have to use self parameter.
So to do that I need to come here and say self dot text this is very important.
Okay.
Don't forget that.
And here also We need to use self.text.move()
Okay.
And the here what I will say I will remove this guy and I will say self dot text as you see now my
text is now their.
Color is something like blue.
That means they are same widget same variable or
Okay.
And now what I want to do? When I hit this enter button is function is enterfunc right.
I want to set a new value for my text widget.
Okay so I will say setText.
Okay.
This my function.
And here I will say enter.
Okay because I hit enter button and for other function I will say here I need to create a new function
exitFunc
Okay.
Self and here.
Again I will say self dot text dot setText
And here I will say "Exit"
Now if I run my application
and you see it's here.
If I hit enter button let's check now it is my text now.
Right.
And if I hit enter button as you see now it is enter I set a new value for my text widget.
If I hit exit Button as you see now it says exit.
But I want to also show you something else here
For example if I use here the longer takes like you clicked for example let's say you clicked
you click enter right or enter button doesn't matter.
And here also lets use you click exit.
Okay.
And I will pass it here.
And let's see.
Now let's run it.
And now if I hit enter button you see we only see here you click
You see but we have you click enter here also you see you click you see why is that it.
Because when you create your label you also create a size also you create a length for your label
Okay.
And your label is its length is I don't know maybe 50 pixels or something like that but for
this guy you click enter.
It's not enough.
So you have to also resize your label.
Okay size you need resize your label and update you can see whole takes here.
Okay.
And here are what I will say.
self.text dot resize and we will resize our text
for x value.
I will say 150 will be enough and also y value.
I will say 20 and this copy this guy this line and paste it here
now it will be fine.
And if I run my application now.
Now let's see.
It is my text right.
If I hit enter as you see you click enter.
it is working.
If I click exit as you see you clicked exit.
This is how we can use buttons and also click functions for buttons.
For now that's all see in the next.