Let's get started by, as usual, importing the relevant library. Tkinter, like the
turtle module, is already pre-installed with every installation of Python.
As long as you've got Python running,
then you should be able to simply import Tkinter as the module
and then you'll be able to use all of the classes and properties and methods
inside. So for example,
we could start off by creating a new window,
and this is sort of equivalent to the screens that we've been working with
in turtle . So in order to do this,
we have to dig into the Tkinter module and then find a class called Tk.
And then once we initialize a new object from that class,
then we have ourselves a window. But if I run this as it is,
you'll see absolutely nothing happen
and it will just go straight away to exit code zero.
And the reason for this is because we need a way of keeping the window on the
screen. What Tkinter has done here is it's gone ahead,
created the window, sees that there's no more instructions
and then it basically ended the program. Now,
what we want instead is to sort of have a while loop that basically just keeps
on running, so something like a while true. And inside this while loop,
it holds onto this window and keeps on listening to see if there's anything else
that the user is going to do to interact with that window so that it can
respond when that happens.
So this loop is included inside Tkinter
and all you have to do is get your window to simply call the main loop.
This main loop is the thing that will keep the window on screen like this.
So now here is our window and here's the line of code that keeps it on the screen and
listens for what the user will do to interact with it. For example,
click on a button or type something.
Now this line of code always has to be at the very end of your program and all
the rest of the things that you're going to write
go in between creating the window and getting the window to run.
The next thing we can do is we can change the title of that program.
And we've done this before using turtle.
We just call the title method and we give a string as the input.
So 'My first GUI program',
and now if I rerun my code,
then you can see that this is what's showing up at the very top bar of my
program. Now,
other things that we might want to change with the window is the sizing of it.
So there is a default size and there's also a way of changing the size.
And we can do that by tapping into a method called a minimum size.
The window is going to scale to include all of the components that you put
inside. If you put a hundred buttons inside,
it's going to make the window massive.
But if you don't have a lot of things in the window,
then you might want to define a minimum size so that you can specify that, by
default, I want my width to be, let's say, I don't know,
500 and the height to be 300,
unless the user wants to resize the window,
or there's a lot of components that makes the window a lot bigger.
So when we run our code
now you can see this is a 500-pixel by 300-pixel window that we've just created
with our own custom title.
In addition to creating windows,
we can, of course, create some components to put inside the window.
So I want to show you firstly
how you would create a label. Just as our window was created from the tkinker
module and it's the Tk class that was used,
there's also a class inside Tkinter called
a label. So that we initialize a label class,
and we can save that to a variable called
my_label or anything else for that matter.
Now this label has quite a few things that we can change about it.
It might not be obvious from the documentation or from the prompt that PyCharm
gives us,
but we can, in fact, change a property called text so we can change what is
displayed inside the label. Let's just write some text called 'I am a label'
Unfortunately, that's actually not enough to get the label to display.
So when I run my program as it is, you don't actually see a label anywhere
on the screen. When we're working with Tkinter
what we have to do is we first have to create a component like a
label.
And then we have to specify how that component is going to be laid out on the
screen before it will show up.
And one of the easiest ways of laying out any component is just to get hold of
that component and then call the method called pack.
And this is going to place it into the screen and it's going to automatically
center it on the screen. So now
if I run the code with this modification,
you can see our label is now finally showing up and it's packed so that it's in
the center of the program.
Now there's other properties that we can modify about our label.
So we could initialize our label with a piece of text,
but we can also specify a font. The font is again,
going to be a tuple,
and that tuple is first going to take the name of the font,
so let's say I want to use Arial,
and then it's going to take the size of the font.
So let's make it a little bit bigger, maybe a 24 size font. And finally,
it's going to take whether, if it's bold or italic or, um,
let's just set it to bold. And now if I run my code again,
you can see I've now got a Arial font, size 24
and it's bold.
You can also change this to italic or you can simply just ignore it and leave
out that last item and it will just give you a regular piece of text.
But none of this would show up unless you have this line of code where you
actually use the packer to pack our label onto the screen. Now
in the course resources,
I've linked to the Tkinter documentation on the Python docs and they talk
about the packer in a lot more detail.
It's basically a geometry management system.
It's just a simple way to lay out the components that you're building.
And this packer has a number of options. So if we scroll down,
you can see that we can change the expand to a boolean, so true or false.
And that tells it whether if it should expand to take up the entire space or
stay as small as it can be. Alternatively,
you have things like side which changes
which side should it be packed to, on the left, right, top, or bottom?
So we know that currently our label is bang in the middle,
but if I change the side to equal left,
now when I run the code,
you can see it's now been packed to the left of the screen.
And if I say bottom,
then it's going to go to the bottom and right and top. All of those will work.
And if I change expand to true,
you can see it's now trying to take up the entire height and width of the
available screen size. Now,
when you're writing this pack function here,
as soon as I type pack and I open up my parentheses,
normally we're used to a whole bunch of properties being listed in here for us,
and we can specify each one and pick and choose basically.
But in this case, this is all that we see.
So what's going on here? Compare this to when we used our turtle module and I'm
going to build a turtle Tim turtle.turtle.
And then I say, tim.write. Look at how,
when I opened up the parentheses here, you can see there's the argument
which is the Object that's going to be written, and then whether
if the writing should be moved to the bottom corner,
which direction it should be aligned,
what's the font and all of the available inputs that we can give to this write
function. Now compare that to our pack function here.
This, you'll notice, is really different. Now,
if you actually look in the documentation for pack,
you can see it actually has a number of things that we can change; before,
expand, fill, in, side, and all of these things
which are modifiable and do various things.
But how is it that it doesn't seem to exist as one of the arguments that we can
use? The secret lies in this last part here,
the **kw. And in the next lesson,
we're going to talk about some more advanced features of Python arguments.
And we're going to look at how we can provide default argument values,
how we can define unlimited positional arguments and unlimited keyword arguments.
To understand this better, head over to the next lesson
and we'll do a deep dive on Python arguments.