Hi again. And in the previous lecture, we
build this graphical user interface with
tkinter. And the problem is, this does
nothing for now. So we press these,
but it doesn't do anything.
So what I'd like to do here that, the
final output I'd like to have is when I
execute this button, so we have
an entry here, we can input the value,
and I want to execute this button and
get miles here in the text area. So
miles is like one kilometre unit is
equals to one mile times 1.6. So if I
execute this, I want something like 16
here to be generated. So a very simple
program. Note that this is not one of
the real programs that I have promised
to build in this course. So this is just
to get you a wrap around with tkinter.
So let's close this now, and to have a
button do something, we need to add
command parameter here. So, a command
parameter takes a function here as an
argument, as a value. And then when you
press that button, the Execute
button in this case, the function that
you have here will be executed. So let's
say we have a function here, def
km_to_miles.
[No audio]
And let's say this function for now, we just print out some
text. Let's say Success, for instance.
Because you know, it's good to before
you write your actual program, it's good
to use print statements to try things
out. So that allows you to isolate your
problems. If something happens here, you
know that you can have a problem with a
print statement, but if you had more
complicated expression here, it gets
harder to know whether a problem happens
in here that you are not pointing to the
correct function, or you have a problem
with your statements. So it's good to
use simple statements first, like the
print statement. So you'd go here and
km_to_miles, that is the function.
And one thing you should keep in mind is
that you don't pass in brackets here. So
this is not a usual function, cool,
you're just referencing to the function
here, and I left Python taking care of
all the rest. So the function name goes
here, and let's try this old.
[Author typing]
Execute.
And you see that we got Success printed
out in here, press again, Success,
Success, Success, so it's working.
However, we don't need to print out
Success in the command line, we want to
print out a value in the text area.
[Author typing]
Now we need to look at the entry widget,
because series where we are going to get
the value from the user enters a
value of the entry widget, and then we
grab that value, and then we use that
value in our function. And for this
there is a parameter called textvariable.
And that will be equal to a StringVar
object, which we have to declare
further up here. So let's say you want
value, so that's the variable, and that
would be equal to the StringVar
function. And then we point to e1_value
a variable, which we have just
created. So this variable will get the
value, depending on what the user will
input in the entry widget, then we can
use this value whatever we like. Let's
say we want that print out. e1_value, and
we also need to add something
extra here, which is a get method of the
StringVar object. So this is a
special object, it's not a string, a Python
plain string. To get an actual string
out of this object, we need to
apply the get method. So save this and
try the script. Let's say 10 here,
Execute, and we get 10 printed off on
the terminal. So we haven't yet
connected this string here with a text
widget. So let's do just that. Here. The
idea here is that we need to insert a
value to the text widget. And to do
that, the text widget has a method
called insert. So you need to refer to
the text widget that you want. So t1
is our text object and then apply the
insert method and the method gets an
argument first, which is the place where
you want to insert the text. Let's say
we want to insert at the end of text
widget. For example, let's suppose you
are inserting many lines of text in your
widget, and you'd have to apply to
execute this method multiple times. And
each time if you put END, you'd enter
the new text at the bottom of the
existing text. So you're saying put, at
the end, this text. e1_value.get.
So the string of the value that the
user will enter in the entry widget, so
let's see how these goes. Execute the
function. And put some text here, you
can also put anything, Hey there, Execute,
and you get the string printed out in a
text widget. All right, but how if we
don't want to just grab this value and
insert it in the text, we want to first
do something with this value and then we
insert the product, the output in the
text widget. So let's say we will grab
the e1_value.get, and we will
multiply that by 1.6. That would be
miles. So miles equals to the value that
the user inputs, times 1.6. So
you are asking the user to input
kilometers, and one mile is equal to one
kilometer times 1.6, actually it's
something like 1.6 or 6, I don't
remember the exact value, but you get
the idea. And lastly, you'd want to pass
miles in here. So you would want to
insert the miles output value in the
widget, save and execute. And I expect
to get an error here, let's say 10. Yes,
we got an error. So when you get errors,
don't just try to copy this error and
paste it on Google, and try to see what
other people are saying, you may waste a
lot of time. Instead, invest some time
and try to understand this error. So
what this is saying, it's a TypeError,
can't multiply sequence by non-integer
of type float. So a sequence can be a
list, can be a string, a tupple, etc. And
Python is trying to multiply a sequence
with a non-integer, which is a float.
So float here is the 1.6 number. And
Python is trying to multiply 1.6 with a
sequence, with this here. So this is not
a number because the get method actually
grabs a string out of a StringVar
object, and a string cannot be
multiplied with a float number. If
this was to a, it was possible to
multiply. For example, if you multiply
The string me times two, you'd get me, me.
So, but you cannot multiply me by 1.6
because it doesn't make any sense.
Anyway, so the solution to this is, you
want to convert the value that the user
inputs to a float. And you do that by
adding the float function. So what's all
for the brackets here? You see there is
an underline on the bracket and you have
an underline here. So this means this
bracket closes in here. So everything
looks fine. Save and execute again. 10
and Execute, and we get the expected
output, you can delete this print
function, we don't need it anymore. And
I believe that was a good start with the
tkinter library, and you should now be
able to make more advance interfaces.
However, we will be building a real
advanced graphical user interface with
tkinter which will interact with a
database. So it will be a real world
program. And for now, I'll just hand you
an exercise in the next lecture, so that
you practice tkinter a little bit, and you
also get to know with some more cosmetic
aspects. So you'll make the problem
that we built in this lecture, you will
make it more visually appealing, and you
will make sure you will get your hands
dirty with tkinter a little bit. I'll
talk to you later.