- [Tim] All right so moving on now.
So we're at the stage now where we're going
to start populating the songs list
when we actually click on an album.
So it's really just more of the same.
Now we don't really want to do what I'm about to do.
The long-winded way is to create another function,
get underscore songs, let's say
and bind that to the album's ListBox.
And the approach we'll take in the next video
is to subclass our ScrollBox class
and get the ListBoxes to populate themselves.
Sometimes you have to start duplicating code
before you spot that there is duplication.
So I'm going to get this songs list working
so we can see what the common code is
and how we can ultimately avoid that.
Now get_songs is going to be very similar to get_albums,
I'm going to add it just below the get_albums function.
And leaving two blank lines is normal
between functions there or methods.
Def get_songs(event):
lb = event.widget
now we want to do an index
index = int(lb)
Let's close down the update or do that update a bit later.
(lb.curselection()
then we want square bracket zero
and a right parentheses
album_name = lb.getindex
or get parentheses index I should say,
then that all-important comma on the end.
And then we're going to get the artist ID
from the database row.
So # get the artist ID from the database row
and when we do that by typing
album_id = conn.execute
parentheses and then double quotes,
SELECT albums._id
FROM albums
where albums.name
is equal to question mark, end of double quote
comma, space, album_name,
finish the parentheses and then dot fetchone()
and make a call to that method.
And alist = []
and we want to select the song, so we're going to do
for x in conn.execute
and the SQL line of code in double quotes
SELECT songs.title
FROM songs WHERE
songs.album=?
And ORDER BY songs.track"",
double-quote, comma, space
and album_id):
and alist.append
(x)[0])
then end the parentheses,
then the last line of code
songLV.set
I've seen this in the get_albums method
(tuple(alist))
So really apart from the column and table names
this code is pretty much identical
to the get_albums function.
So we just have to bind the function now
to the album ListBox selected event
and the songs list will hopefully be updated
when we click on a new album.
So we'll go to our albums ListBox
and after the config border=2 line
we're going to type
albumList.bind
('<<
single quotes, less than, less than
and it's going to be
ListboxSelect>>',
again greater than sign, greater than sign, single quote,
get_songs)
to call that method.
Again there's no parentheses there
after the get_songs.
So now if we run this,
we should see that the songs will update
when we select an album.
So let's just click on AC DC
For Those About to Rock
And you can now see the songs are now showing up here
on the right-hand side.
So excellent.
We got that to work.
But there is a bit of a problem here still,
so we select another album,
like for example Axel Rudi Pell
select one of the albums to show.
That's fine but if we now select a different artist,
the songs from the Axel Rudi Pell album
are still being displayed.
So come over here
they haven't been reset in other words,
until we actually select a different album
for that particular artist.
So really we need to reset the songs list
whenever the artist changes.
So let's go ahead and do that.
And that's pretty easy to do.
And it's done down here, or up here I should say,
In the get_albums method.
So we initialise it in there, so we put
songLV.set(("Choose an album",))
so let's try running that again.
So we'll do the same thing now.
We'll just choose Axel Rudi Pell
choose an album
that works okay.
And if we go back to say to AC DC,
we can see the songs is correctly updated,
or the message, default message, is now showing
in the songs list
and the previous selection, or list of songs,
has been cleared.
So basically the song list now is cleared
whenever a new artist is selected
and that's actually quite important
because it does have a slight impact on our subclass.
But we'll see about that in the next video.