1
00:00:00,030 --> 00:00:06,240
Hi again and in the previous lecture we
built this graphical user interface with

2
00:00:06,240 --> 00:00:11,940
tkinter, but the problem is this does
nothing for now so we press this

3
00:00:11,940 --> 00:00:16,619
but it doesn't do anything, so what
I'd like to do here is, the final output

4
00:00:16,619 --> 00:00:21,960
I'd like to have is when I execute this
button, so we have we have an entry here

5
00:00:21,960 --> 00:00:27,449
we can input a value and I want to
execute this button and get miles here

6
00:00:27,449 --> 00:00:34,190
in the text area, so miles is like one
kilometer unit is equals to one mile

7
00:00:34,190 --> 00:00:41,850
times 1.6, so if I execute this I want
something like 16 here to be generated.

8
00:00:41,850 --> 00:00:47,280
So a very simple program. Note that this
is not one of the real programs that I've

9
00:00:47,280 --> 00:00:52,020
promised to build in this course. So this
is just to get you up and runing with

10
00:00:52,020 --> 00:00:59,340
tkinter, so let's close this now and to
have a button do something we need to

11
00:00:59,340 --> 00:01:05,430
add a command parameter here, so the
command parameter takes a function here

12
00:01:05,430 --> 00:01:11,340
as argument, as a value and then when you
press that's that button, the execute button

13
00:01:11,340 --> 00:01:16,049
in this case, the function that you have
here will be executed. So let's say we

14
00:01:16,049 --> 00:01:28,020
have a function here def kilometers to miles.
Now let's say this function

15
00:01:28,020 --> 00:01:36,150
for now just prints out some text.
Let's say success for instance because

16
00:01:36,150 --> 00:01:42,090
you know it's good to before you write
your actual program it's good to use

17
00:01:42,090 --> 00:01:48,479
print statements to try things out
so that allows you to isolate your problems.

18
00:01:48,479 --> 00:01:52,920
If something happens here you know that
you can have a problem with a print

19
00:01:52,920 --> 00:01:57,659
statement, but if you had more
complicated expression here it gets

20
00:01:57,659 --> 00:02:02,420
harder to know whether the problem
happens in here that you are not

21
00:02:02,420 --> 00:02:08,520
pointing to the correct function or you
have a problem with your statements,

22
00:02:08,520 --> 00:02:13,050
so it's good to use simple statements first
like the print statement.

23
00:02:13,050 --> 00:02:17,080
So you'd go here and
kilometers to miles, that is a function

24
00:02:17,080 --> 00:02:23,050
and one thing you should keep in mind is
that you don't pass the brackets here,

25
00:02:23,050 --> 00:02:28,360
so this is not a usual function call.
You are just referencing to the function here

26
00:02:28,360 --> 00:02:34,599
and let Python take care of the rest.
So the function name goes here and let's

27
00:02:34,599 --> 00:02:43,629
try this out. Execute and you see that
we got success printed out in here

28
00:02:43,629 --> 00:02:47,560
Press again success success success,
so it's working.

29
00:02:47,560 --> 00:02:52,569
However we don't need to print our
success in the command line. We want to

30
00:02:52,569 --> 00:02:59,709
print out a value in the text area.
Now we need to look at the entry widget

31
00:02:59,709 --> 00:03:04,720
because series where we are going to get
the value from, so the user enters a value

32
00:03:04,720 --> 00:03:09,340
in the entry widget and then we
grab that value and then we use that

33
00:03:09,340 --> 00:03:17,859
value in our function and for this there
is a parameter called text variable

34
00:03:17,859 --> 00:03:24,220
and that would be equal to a string object
which we have to declare further

35
00:03:24,220 --> 00:03:31,269
up here, so let's say you want value,
so that's a variable and that'll be equal to

36
00:03:31,269 --> 00:03:39,099
the stringVar function and then we
point to e1 value, a variable which

37
00:03:39,099 --> 00:03:44,169
we have just created so this variable
will get the value depending on what the

38
00:03:44,169 --> 00:03:50,250
user will input in the entry widget.
Then we can use this value whatever we like.

39
00:03:50,250 --> 00:03:56,680
Let's say we want to print that out.
E1 value and then we also need to add

40
00:03:56,680 --> 00:04:02,620
something extra here which is
a get method of the stringVar object,

41
00:04:02,620 --> 00:04:09,489
so this is a special object. Is not a string.
A Python plain string. To get an

42
00:04:09,489 --> 00:04:15,549
actual string from out of this object
we need to apply the get method, so save

43
00:04:15,549 --> 00:04:22,300
this and try the script, let's say 10
here, execute and we get 10 printed out

44
00:04:22,300 --> 00:04:27,110
on the terminal, so we haven't yet
connected this string

45
00:04:27,110 --> 00:04:34,759
here with the text widget so let's do just
that here. The idea here is that we need

46
00:04:34,759 --> 00:04:41,750
to insert a value to the text widget and
to do that what text widget has a method

47
00:04:41,750 --> 00:04:47,629
call insert, so you need to refer to the
text widget that you want. So t1 is our

48
00:04:47,629 --> 00:04:53,889
text object and then apply the insert method.
And the method gets an argument first

49
00:04:53,889 --> 00:04:59,240
which is the place where you want to
insert the text. Let's say we want to

50
00:04:59,240 --> 00:05:05,029
insert at the end of the text widget.
For example let's suppose you are inserting

51
00:05:05,029 --> 00:05:11,330
many lines of text in your widget and
you have to apply to execute this

52
00:05:11,330 --> 00:05:17,659
method multiple times and each time if
you put END, you'd enter the new text

53
00:05:17,659 --> 00:05:24,800
at the bottom of the existing text, so you
are saying put at the end this text.

54
00:05:24,800 --> 00:05:31,430
E1 value.get.
So the string of value that the user

55
00:05:31,430 --> 00:05:38,500
will enter in the entry widget, so let's
see how these goes. Execute the function

56
00:05:38,500 --> 00:05:46,550
and put some text here. You can also put
anything. Hey there. Execute and you get

57
00:05:46,550 --> 00:05:53,210
the string printed out in a text widget.
All right but we don't want to just

58
00:05:53,210 --> 00:05:58,099
grab this value and insert it in the text.
We want to first do something with

59
00:05:58,099 --> 00:06:04,580
this value and then we insert the product,
the output in the text widget so let's

60
00:06:04,580 --> 00:06:12,560
say we will grab the e1 value.get.
And we will multiply that by 1.6

61
00:06:12,560 --> 00:06:22,330
and that would be miles, so miles
equals to the value that the user inputs

62
00:06:22,330 --> 00:06:28,909
times 1.6, so you are asking the user
to input kilometers and 1 mile is equal

63
00:06:28,909 --> 00:06:36,949
to 1 kilometer times 1.6. actually is
something like 1.606, I don't remember

64
00:06:36,949 --> 00:06:40,940
the exact value, but you get the idea.
And lastly you'd want

65
00:06:40,940 --> 00:06:46,160
to pass miles in here, so you want
to insert the miles output value in the widget.

66
00:06:46,160 --> 00:06:55,610
Save and execute and I expect to
get an error here let's say 10.

67
00:06:55,610 --> 00:07:01,730
Yes, we got an error and so when you get errors
don't just try to copy this error and

68
00:07:01,730 --> 00:07:07,070
paste it on Google and try to see what
other people are saying. You may waste a

69
00:07:07,070 --> 00:07:11,990
lot of time. Instead invest some time and
try to understand this error, so what this

70
00:07:11,990 --> 00:07:19,310
is saying is a type error. Can't multiply
sequence by non integer of type float.

71
00:07:19,310 --> 00:07:25,250
So a sequence can be a list, can be a string,
a tuple etc. And Python is trying to

72
00:07:25,250 --> 00:07:31,310
multiple a sequence with a non integer
which is a float, so the float here is the

73
00:07:31,310 --> 00:07:36,560
1.6 number and Python is trying to
multiply 1.6 with a sequence

74
00:07:36,560 --> 00:07:44,360
with this here so this is not a number
because the get method actually grabs

75
00:07:44,360 --> 00:07:50,660
a string out of a stringVar objects and
a string cannot be multiplied with

76
00:07:50,660 --> 00:07:56,480
a float number. If this was 2, it was
possible to multiply for example if you

77
00:07:56,480 --> 00:08:04,850
multiply me, the string me times 2,
you'd get me me, so, but you cannot

78
00:08:04,850 --> 00:08:10,460
multiply me by one point six because
it doesn't makes any sense. Anyway, so a

79
00:08:10,460 --> 00:08:16,220
solution to this is you want to convert
the value that the user inputs

80
00:08:16,220 --> 00:08:23,810
to a float and you do that by adding the
float function, so watch out for the

81
00:08:23,810 --> 00:08:29,000
brackets here. You see there is an
underline under the bracket and you have

82
00:08:29,000 --> 00:08:37,400
an underline here, so this means is bracket
it closes in here, so everything looks fine.

83
00:08:37,400 --> 00:08:45,310
So if we execute again. 10 and execute.
And we get the expected output.

84
00:08:45,310 --> 00:08:51,320
You can delete this print function.
We don't need it anymore, and I believe that

85
00:08:51,320 --> 00:08:54,830
was a good start with
the tkinter library.

86
00:08:54,830 --> 00:08:59,750
And you should not be able to make more
advanced interfaces, however we'll be

87
00:08:59,750 --> 00:09:04,940
building a real advanced graphical user
interface with tkinter which will

88
00:09:04,940 --> 00:09:10,880
interact with a database, so it'll be
real world program, and for now I'll just

89
00:09:10,880 --> 00:09:15,890
hand you an exercise in the next lecture
so that you practice tkinter a little bit,

90
00:09:15,890 --> 00:09:22,070
and you also get to know with some more
cosmetic aspects, so you will make the

91
00:09:22,070 --> 00:09:25,910
problem that we built in this lecture
you'll make it more visually appealing

92
00:09:25,910 --> 00:09:30,890
and you'll make sure you'll get your
hands dirty with tkinter a little bit.

93
00:09:30,890 --> 00:09:34,720
I'll talk to you later

