In the last lesson, we saw how we could set a lot of the properties for the
methods associated with Tkinter,
or for initializing the components like our labels.
Now in the documentation,
they tell us that there's a number of ways that we can set these options.
We can either do it when we initialize a new object or we can access the
properties like as if they are keys in a dictionary and then setting the value.
And finally,
we can set multiple properties by using the config method and passing in the
value for each of the things that we want to change,
I recommend taking a look at the Tcl tk documentation
which lists all of the options. Like for example, here,
I've got my label chosen and there are standard options like background color,
or text. And there are widget-specific options,
so specific to the label, you can change its height, you can change its width.
Coming back to my label in my main.py,
if I wanted to change my label's property, so for example,
if I want to change the text that's inside and I want to make it different from
the initial text that it displays,
then I can do simply this by changing it as if it were a dictionary.
And I can set that to the new value, so new text.
Now I could also do it by doing my_label.config and then passing in the
text as a keyword argument. Either way
when I run this main.py, remember previously,
we were running our playground.
So go back to run and then click this and then change it to our main.py.
And now you should see the new text show up instead of the original text,
which is I am a label.
So this is how we configure and change or update the properties of a particular
component that we've created.
In addition to labels, we can also create buttons.
Let's create a button, which again, is going to come from the Tkinter module
and it's going to be the button class from that module
as you can see right here. Now,
all of the components that we're going to be using
come from this class. Very often
you'll see people instead of importing Tkinter, they'll say from tkinter
import,
and then the asterisk to denote import every single class.
This means that we can basically get rid of all the module mentions
and we can just initialize a window as tk,
our label as a label and our button as simply button, like this. Saves a little
bit of typing, depends on how many of the classes you're gonna use.
If you're just going to use one,
I recommend keeping the module so that you know, where it comes from.
But if you're going to use loads of them
like we are here, then it does make it a lot quicker to type.
So let's create our button. And again,
it has a whole bunch of optional keyword arguments. And some of those are
standard. So for example,
we can change the button text in the same way that we changed the label text.
So let's make it to say click me. And remember that in order to make anything appear
on screen, it needs to have some sort of layout.
So let's call pack again to get it to be packed onto the screen.
So now if I run this code as it is,
you can see that my button has been placed in the middle of the screen and my
label is packed to the left.
Let's go ahead and get rid of that left
so that it actually goes in order so that the first thing gets created,
it gets put on screen first and then the next thing is the button
and then this is all centered by default.
The next obvious question is how can we get this button to actually work?
Because at the moment clicking on it does absolutely nothing. As you remember
when we used our turtle module,
we could have an event listener. So we can create some sort of a function,
let's call it say button_clicked.
And then this function could do something say, for example,
I got clicked.
And when this button detects an event,
say a click event, then it could call this function.
So in the tkinter world, there is a property called command,
which you can set to equal the name of a function. So remember,
it's the name of the function, not calling the function.
So we don't actually need the parentheses at the end.
Now when we run our code, when this button detects a command
which is basically a click event,
then it's going to trigger this method and it's going to print this hopefully
into the console. So let's hit run. Now,
I'm going to click on this button and you can see it says I got clicked and it
does that every time I click on it.
So here's a challenge for you.
How can you make the label,
my_label, instead of reading new text, for it to say
button got clicked when I click this button?
So this button click should now change the text. Pause the video,
give that a go. All right.
So we know that we can change our label's text by using either of these methods.
So you can basically pick one of these and add it to this function.
So I'm going to use the
my_label.config just because it kind of makes a little bit more sense when I
read that line of code.
So I'm going to configure the text property to read button
got clicked.
And now when I hit run,
what you'll see is when I click this button, it's going to change the text.
So we're now getting a little bit more interactivity.
The next thing I want to show you is even more interactivity.
I want to show you a component called the entry component,
and this is basically just a input effectively.
So let's create a new object which I'll call input,
and it's created from the entry class
which comes from our tkinter module. Now again,
if we want something to appear on screen,
we have to give it a layout and we can use the automatic pack to do that.
So now when I run this code, you can see I've now got a input field. Now,
if I want to look at the options for my entry,
I can simply go to this tk documentation, find my entry,
click on it,
and you can see I've got standard options and widget-specific options. Now,
one of the things I want to change is the width. For example,
I could change the width of this input by simply using the width keyword
argument.
And I can specify an integer to indicate the desired width of the entry window.
So at the moment, if I think it's a little bit too wide,
then let's try modifying our input so that has a slightly lower width.
Let's change it to maybe 10
and you can see it's now a lot smaller than before. Now
what if I want to get hold of the value that came from that entry?
Well, in that case, I have to use the get
which basically just returns the entry's string.
So I could say input.get
and then this method is going to return the input as a string.
So I could, for example, print it out.
But unfortunately, the point when this line of code is run,
there's actually no input inside this entry.
So we would actually print absolutely nothing.
Here's a challenge for you.
Can you figure out how to get whatever is written in here
to be the text in the label
the moment when I click on this button?
Given that I've typed something in here, when I click this button,
I want that something to be displayed in the label instead of 'button got
clicked.' Pause the video and see if you can complete this challenge.
All right.
So we know that the trigger for the button is the button_clicked function.
So it's here where we're actually configuring our label. If instead,
we're going to get hold of our input here and call input.
get to get the new_text, well,
then we can actually use this new text and populate the label.
So we can say text = new_text.
And now when I hit run,
you can see that I can type whatever it is I want in here.
And then when I click 'click me',
it gets the text inside the entry and puts it in the label.
This is how we would work with some of the most basic components of tkinter like
label, button, entry,
and we've also seen the pack method. Now, as you can see,
they're all other things that you can do with tkinter.
So in the next lesson,
we're going to be working on top of what we've done so far
and I want to show you some of the other widgets that you can work with and add
to your tkinter program.