1
00:00:05,200 --> 00:00:06,790
- [Tim] All right so moving on now.

2
00:00:06,790 --> 00:00:08,450
So we're at the stage
now where we're going

3
00:00:08,450 --> 00:00:10,520
to start populating the songs list

4
00:00:10,520 --> 00:00:12,130
when we actually click on an album.

5
00:00:12,130 --> 00:00:13,710
So it's really just more of the same.

6
00:00:13,710 --> 00:00:16,590
Now we don't really want
to do what I'm about to do.

7
00:00:16,590 --> 00:00:19,740
The long-winded way is to
create another function,

8
00:00:19,740 --> 00:00:21,230
get underscore songs, let's say

9
00:00:21,230 --> 00:00:23,660
and bind that to the album's ListBox.

10
00:00:23,660 --> 00:00:25,800
And the approach we'll
take in the next video

11
00:00:25,800 --> 00:00:28,170
is to subclass our ScrollBox class

12
00:00:28,170 --> 00:00:30,780
and get the ListBoxes
to populate themselves.

13
00:00:30,780 --> 00:00:33,340
Sometimes you have to
start duplicating code

14
00:00:33,340 --> 00:00:35,430
before you spot that there is duplication.

15
00:00:35,430 --> 00:00:37,490
So I'm going to get
this songs list working

16
00:00:37,490 --> 00:00:39,490
so we can see what the common code is

17
00:00:39,490 --> 00:00:41,890
and how we can ultimately avoid that.

18
00:00:41,890 --> 00:00:45,940
Now get_songs is going to be
very similar to get_albums,

19
00:00:45,940 --> 00:00:50,110
I'm going to add it just
below the get_albums function.

20
00:00:53,970 --> 00:00:55,420
And leaving two blank lines is normal

21
00:00:55,420 --> 00:00:58,200
between functions there or methods.

22
00:00:58,200 --> 00:01:01,370
Def get_songs(event):

23
00:01:03,700 --> 00:01:05,530
lb = event.widget

24
00:01:09,210 --> 00:01:11,930
now we want to do an index

25
00:01:11,930 --> 00:01:14,700
index = int(lb)

26
00:01:14,700 --> 00:01:18,290
Let's close down the update
or do that update a bit later.

27
00:01:18,290 --> 00:01:22,470
(lb.curselection()

28
00:01:22,470 --> 00:01:25,080
then we want square bracket zero

29
00:01:25,080 --> 00:01:26,580
and a right parentheses

30
00:01:26,580 --> 00:01:28,990
album_name = lb.getindex

31
00:01:32,230 --> 00:01:34,210
or get parentheses index I should say,

32
00:01:34,210 --> 00:01:37,530
then that all-important comma on the end.

33
00:01:37,530 --> 00:01:39,480
And then we're going to get the artist ID

34
00:01:39,480 --> 00:01:41,320
from the database row.

35
00:01:41,320 --> 00:01:44,990
So # get the artist ID
from the database row

36
00:01:47,220 --> 00:01:49,890
and when we do that by typing

37
00:01:49,890 --> 00:01:52,300
album_id = conn.execute

38
00:01:55,190 --> 00:01:57,380
parentheses and then double quotes,

39
00:01:57,380 --> 00:01:58,790
SELECT albums._id

40
00:02:06,000 --> 00:02:06,920
FROM albums

41
00:02:08,490 --> 00:02:09,910
where albums.name

42
00:02:12,960 --> 00:02:15,740
is equal to question
mark, end of double quote

43
00:02:15,740 --> 00:02:17,820
comma, space, album_name,

44
00:02:19,470 --> 00:02:22,770
finish the parentheses
and then dot fetchone()

45
00:02:22,770 --> 00:02:25,840
and make a call to that method.

46
00:02:25,840 --> 00:02:27,430
And alist = []

47
00:02:29,740 --> 00:02:33,740
and we want to select the
song, so we're going to do

48
00:02:33,740 --> 00:02:35,740
for x in conn.execute

49
00:02:37,580 --> 00:02:41,050
and the SQL line of code in double quotes

50
00:02:41,050 --> 00:02:42,550
SELECT songs.title

51
00:02:44,520 --> 00:02:45,850
FROM songs WHERE

52
00:02:47,400 --> 00:02:48,900
songs.album=?

53
00:02:52,230 --> 00:02:54,480
And ORDER BY songs.track"",

54
00:02:57,760 --> 00:02:59,510
double-quote, comma, space

55
00:02:59,510 --> 00:03:00,680
and album_id):

56
00:03:05,380 --> 00:03:06,710
and alist.append

57
00:03:08,460 --> 00:03:10,460
(x)[0])

58
00:03:11,690 --> 00:03:13,780
then end the parentheses,

59
00:03:13,780 --> 00:03:15,340
then the last line of code

60
00:03:15,340 --> 00:03:16,170
songLV.set

61
00:03:18,470 --> 00:03:20,820
I've seen this in the get_albums method

62
00:03:20,820 --> 00:03:24,820
(tuple(alist))

63
00:03:26,190 --> 00:03:27,940
So really apart from the
column and table names

64
00:03:27,940 --> 00:03:29,420
this code is pretty much identical

65
00:03:29,420 --> 00:03:32,510
to the get_albums function.

66
00:03:32,510 --> 00:03:34,240
So we just have to bind the function now

67
00:03:34,240 --> 00:03:36,960
to the album ListBox selected event

68
00:03:36,960 --> 00:03:39,650
and the songs list will
hopefully be updated

69
00:03:39,650 --> 00:03:41,710
when we click on a new album.

70
00:03:41,710 --> 00:03:44,460
So we'll go to our albums ListBox

71
00:03:45,310 --> 00:03:48,840
and after the config border=2 line

72
00:03:48,840 --> 00:03:49,830
we're going to type

73
00:03:49,830 --> 00:03:51,000
albumList.bind

74
00:03:53,760 --> 00:03:55,190
('<<

75
00:03:55,190 --> 00:03:57,930
single quotes, less than, less than

76
00:03:57,930 --> 00:03:58,760
and it's going to be

77
00:03:58,760 --> 00:04:00,450
ListboxSelect>>',

78
00:04:00,450 --> 00:04:03,800
again greater than sign,
greater than sign, single quote,

79
00:04:03,800 --> 00:04:04,630
get_songs)

80
00:04:07,690 --> 00:04:08,840
to call that method.

81
00:04:08,840 --> 00:04:10,450
Again there's no parentheses there

82
00:04:10,450 --> 00:04:12,890
after the get_songs.

83
00:04:12,890 --> 00:04:14,720
So now if we run this,

84
00:04:21,200 --> 00:04:23,690
we should see that the songs will update

85
00:04:23,690 --> 00:04:24,750
when we select an album.

86
00:04:24,750 --> 00:04:27,090
So let's just click on AC DC

87
00:04:27,990 --> 00:04:29,410
For Those About to Rock

88
00:04:29,410 --> 00:04:31,290
And you can now see the
songs are now showing up here

89
00:04:31,290 --> 00:04:32,130
on the right-hand side.

90
00:04:32,130 --> 00:04:32,960
So excellent.

91
00:04:32,960 --> 00:04:34,170
We got that to work.

92
00:04:34,170 --> 00:04:36,480
But there is a bit of
a problem here still,

93
00:04:36,480 --> 00:04:37,630
so we select another album,

94
00:04:37,630 --> 00:04:40,220
like for example Axel Rudi Pell

95
00:04:41,220 --> 00:04:43,680
select one of the albums to show.

96
00:04:43,680 --> 00:04:46,210
That's fine but if we now
select a different artist,

97
00:04:46,210 --> 00:04:48,360
the songs from the Axel Rudi Pell album

98
00:04:48,360 --> 00:04:50,220
are still being displayed.

99
00:04:50,220 --> 00:04:51,760
So come over here

100
00:04:51,760 --> 00:04:53,350
they haven't been reset in other words,

101
00:04:53,350 --> 00:04:56,150
until we actually select a different album

102
00:04:56,150 --> 00:04:57,700
for that particular artist.

103
00:04:57,700 --> 00:04:59,870
So really we need to reset the songs list

104
00:04:59,870 --> 00:05:01,930
whenever the artist changes.

105
00:05:01,930 --> 00:05:04,760
So let's go ahead and do that.

106
00:05:04,760 --> 00:05:07,200
And that's pretty easy to do.

107
00:05:07,200 --> 00:05:09,960
And it's done down here,
or up here I should say,

108
00:05:09,960 --> 00:05:12,040
In the get_albums method.

109
00:05:16,450 --> 00:05:17,840
So we initialise it in there, so we put

110
00:05:17,840 --> 00:05:22,010
songLV.set(("Choose an album",))

111
00:05:32,730 --> 00:05:35,400
so let's try running that again.

112
00:05:37,120 --> 00:05:38,560
So we'll do the same thing now.

113
00:05:38,560 --> 00:05:41,220
We'll just choose Axel Rudi Pell

114
00:05:41,220 --> 00:05:42,640
choose an album

115
00:05:42,640 --> 00:05:43,650
that works okay.

116
00:05:43,650 --> 00:05:45,640
And if we go back to say to AC DC,

117
00:05:45,640 --> 00:05:48,740
we can see the songs is correctly updated,

118
00:05:48,740 --> 00:05:51,900
or the message, default
message, is now showing

119
00:05:51,900 --> 00:05:53,010
in the songs list

120
00:05:53,010 --> 00:05:55,270
and the previous selection,
or list of songs,

121
00:05:55,270 --> 00:05:56,600
has been cleared.

122
00:05:56,600 --> 00:05:58,320
So basically the song list now is cleared

123
00:05:58,320 --> 00:05:59,980
whenever a new artist is selected

124
00:05:59,980 --> 00:06:03,430
and that's actually quite important

125
00:06:03,430 --> 00:06:06,330
because it does have a slight
impact on our subclass.

126
00:06:06,330 --> 00:06:09,910
But we'll see about
that in the next video.

