So here we go, I have open up Atom here.
So I'm going to use Atom to build the
bookstore program, so to say, that uses
tkinter and sqlite3 as the
libraries. So I'll go ahead and create a
new Python file in here.
[No audio]
Call it script1.py.
[No audio]
Now to build graphical user
interfaces with tkinter you need
tkinter library, and you don't have to
install it, because tkinter is a built
in Python library. So all you have to do
is go ahead and import tkinter. If you
are on Python2, this would be with a
capital T. In Python3, it's
tkinter, simply like that. Good
practice, however, is to actually import
everything from tkinter.
So, from tkinter import all. So why,
we do that? We do that because we're
going to use a lot of tkinter
objects in our scripts. So it makes
sense to just load them all instead of
referencing them with tkinter and dots, and
button, for example. Instead of doing
that, we can just say button, and then
we enter the parameters and so on. So
let me create a graphical user interface now.
Now, a tkinter program is made up of
two main things, we have the window and
you have the widgets. So for example,
the window is whole this
program here, as you see. And then you
have buttons and things like that, which
are called widgets. So first of all, you
need to create the window. And I like to
call my variable window, and that would
be equal to tk. That's it. That would
create an empty window for us but we
need to do something more here. We say
window.mainloop.
[Author typing]
This is always necessary. So we open a window, we
create a window and then everything goes
between these two lines. So you create a
widget between these two. And this is
necessary because if you don't have
this, your program will open up and it
will close in a split of a second, so
basically, this allows you to actually
press that X button on the
corner of your window and close the
program. So this should be always at the
end of your code. I'll save this script,
and I'm going to execute it.
[Author typing]
And we got an error, it says tkinter 'tk' is not
defined. So Python is not recognizing
this function here because it should be
capital T and execute again. This is
the window, and it has nothing inside. So
let's go ahead and add a button. So we
store the main window in a variable,
we're going to do the same for widgets.
So we're going to create variables.
Let's say, b1 equals to Button. So the
Button is a function that generates a
button widget. The Button function takes
some arguments. If you want to get a
full list of arguments, you'd want to do
something called introspection. Let's say I've
python here, and from tkinter import
full, and then let's say Button,
and a question mark. And here you can
see what parameters you can pass
to the Button function.
[Author typing]
Now the very first parameter you would want to add to
a widget is the variable of the window,
of the main window. So you need to tell
your widget, the window that the widget
has to be backed to. So window and then you
can pass parameters, like the one you
see here. Let's say one of the main
parameters would be text, so text
equals, let's say to the string Execute.
[No audio]
So that is the text, the label that
will be displayed over the button. Now
if we try to execute this script now
you'll see that we don't have a button
yet, and that's because we haven't
specified where to put that button. To
do that, you need to use the pack
method, which is a widget method, and then
execute again and now you see them
button. However, there is another way to
actually put your widgets in in your
window, that is the grid method, grid,
and often is a matter of preference. So
whether to use pack or grid, but the
idea of grid is that with a grid
method you have more control on the
position of your buttons or
your widgets in general, because here
you specify the row
[No audio]
and the column where you want to put your button. So
everything starts from zero, and so the
first row has an index of zero and the
first column has an index of zero, and
so on, I actually found illustration for
that. So this is tkinter program, and we
have two rows here. So we have this
first row which should have an index of
zero, and you have the second row which
would have an index of one, and you have
columns, so 0-1-2-3, and so on. Let's say,
the Okay button, which is this one here,
would have a row of 012. So 2 for the
row, and then 0123, 3 for the column. A window might
also have widgets where, which span in
more than one row or a column. In that
case, you'd add another parameter here
called rowspan. And that would be equal
to a number, let's say if you put 2, the
button would span in two rows.
In our case, this is not necessary actually, because
we don't have lots of widgets, we only
have one button. And so let me execute
this script now and see what we get. So
we get a button, which actually does
nothing, because we haven't specified what
action to perform when you're pressing
the button. And I'll explain that in the
next lecture, but for now let's add
some more widgets here, just for you to
get more familiar with widgets. So let's
add an entry widget. And let's say e1
for that. So e1 equals to
Entry, and let me guess what it is for
spirometer, and that's the window parameter.
And we can leave it like that
for now. And let's say a you want grid.
[No audio]
And row, let's keep it in the same row
and column, 1. Save, execute, and here
we have an entry. So what's an entry? An
entry is, it's like an area where you can
enter a value and than you can
interact with widget value in your
script. So let's say you enter
kilometres here, and you'd want to have
a button that converts those kilometers
to miles. So the button will get the
value of this area here. And I need to display
it in another widget, which could be a
text widget. So let's add a text widget.
Let's say, t1 equals the Text window,
of course, and let's say, t1 grid
row 0,
column 2.
That's it, execute. We got a very big text widget because
the default size, the default
height and width is actually quite a big
number, so we need to specify two
parameters for the text widget. We would
need to put the height, let's say, 1
cell and the width, let's say, 20.
[Author typing]
So this is the widget area, and this is
the entry, and this is the button. And
that's it for this lecture. In the next
lecture, I'll show you how to put these
widgets in life so that they can
actually do something. See you in the next lecture.