0:00
After label, now we are going to learn
0:02
how we can create a button in Python
0:04
Tkinter. In order for us to create a
0:06
button in Python Tkinter, it's also very
0:08
easy task. So, we just need to do
0:11
First, let me remove all of this label
0:14
that we have here, okay? I can just come
0:16
here, delete this thing, save. In order
0:18
for us to create a button, so buttons
0:20
are used to trigger any action. When we
0:23
create a button, so usually we need to
0:25
give a function to it so that it can
0:26
trigger any action that we need it
0:30
In order for us to do that, for example,
0:32
I'm going to create one function, for
0:35
hello. Def hello. Oh, no, this is not
0:38
the way to write hello. Hello is this
0:40
way, okay? Def hello. Okay, this def
0:44
is a function that prints hello world.
0:46
Okay, let's just write something like
0:47
this, for example, print
0:49
open and close. What I'm going to do
0:51
is it it will print me hello
0:54
hello world. Oh, oh, hello world,
1:00
So, this my function, it prints hello
1:01
world. But, if I run this same function,
1:05
okay? If I run this same function,
1:08
nothing is going to happen.
1:11
Okay? So, let me run this function. As
1:13
you can see, I run my function hello
1:14
world, nothing's going to happen.
1:16
For example, if I want it to show, I
1:19
need to call it, for example, like
1:22
Okay? Hello, open and close, something
1:26
only if I run like this, it will
1:30
show me the result, for example, here,
1:33
But, we don't want it to like to run it
1:36
by itself. We want to run this function
1:39
click in the button, so that it will
1:42
trigger the action, okay? So, that it
1:43
will show this message hello world. So,
1:46
in order for us to do that, we need to
1:48
a button, because the button is the one
1:50
that it's used to trigger any action
1:52
into Python Tkinter. In order for us to
1:54
create a button, and to do something
1:56
like this. I'm going to create a button.
1:58
I'm just going to going to call it B.
1:59
So, this is my button B. It will be
2:01
equal a button. Okay, we need to call
2:04
the button this instance, okay? Because
2:07
this is where the button is.
2:09
So, after that we need to place this
2:11
button inside of one window. So, that's
2:13
why first it starts as always is the
2:17
is the window that we want it to show.
2:23
the first one is a window that we want
2:25
to show this button. That's why I have
2:30
Then, I'm going to have b.pack
2:35
b.pack open and close, save. So, here in
2:38
my window, okay? If I run this thing,
2:44
as you can see, I have my button here.
2:45
This is something that I can click, as
2:47
you can see, okay? But, now let's give
2:49
some things in these windows in these
2:52
buttons so that it can look more uh
2:54
professional or more good good so that
2:56
we can perform an action with it.
2:59
So, in order for us to do that, first we
3:00
need to give a text also. We can give a
3:02
text into our window. For example, I can
3:04
say something like click me. Click me.
3:08
Okay, click me, save.
3:11
Then, we can also change the background
3:13
color of our window. For example, I can
3:15
just give for example something like BG.
3:21
Um we're just going to give something
3:28
And for FG, I'm just going to give
3:34
will be equal I'm just going to do this
3:36
thing. I will say white. Perfect.
3:39
Now, if I save this and if I run this
3:46
as you can see, now I have this one,
3:51
now we want that every time when we
3:53
click in this button, it must run this
3:56
function, okay? Print hello world. In
3:58
order for us to do that is something In
4:02
a property called command, okay? So,
4:05
this property called command is the one
4:07
that we give the function, okay? So,
4:10
that every time when we click in the
4:12
button, it will kind of perform any
4:14
action. So, this property called command
4:17
is something like this, okay? We just
4:20
Let's write it, okay? I'm just going to
4:22
call something this command. Oh, oh.
4:25
It's better if I come here. Okay, come
4:30
Okay, command equal. Now, I just need to
4:31
give the name of the function, which is
4:36
Okay, see? Here I just give hello like
4:38
this, okay? I didn't open and close
4:41
brackets, okay? No. This is wrong. You
4:44
just give something like this one, okay?
4:46
It's just like this, hello. So, now if I
4:48
run this thing, okay? Oh, I comment the
4:51
It's very important for you to separate
4:53
the properties with comma, okay? Save.
4:57
Okay? Now, let me run this.
5:03
As you can see, we have in our button.
5:05
Now, just give attention here. If I
5:07
click in this button, boom, it will it
5:09
prints hello world. If I click again,
5:12
see? Now, this means every time when I
5:13
click in this button, it will perform
5:15
one action, okay? Which is printing the
5:23
So, apart of that we can also like give
5:28
for our button. So, in order for us to
5:30
give a width is very easy, just the same
5:32
way like a label, width.
5:35
I'm going to give something like uh
5:40
10, okay? 10. Oh, oh.
5:43
And also we can give height for our
5:45
button, okay? Height can be something
5:51
I save. Now, let me run this.
5:57
As you can see, we have our button. The
5:58
same thing, if I click,
6:01
it's going to perform the action that we