WEBVTT 1 00:00:01.986 --> 00:00:03.579 - [Tim] All right so moving on now. 2 00:00:03.579 --> 00:00:05.238 So we're at the stage now where we're going 3 00:00:05.238 --> 00:00:07.308 to start populating the songs list 4 00:00:07.308 --> 00:00:08.918 when we actually click on an album. 5 00:00:08.918 --> 00:00:10.494 So it's really just more of the same. 6 00:00:10.494 --> 00:00:13.374 Now we don't really want to do what I'm about to do. 7 00:00:13.374 --> 00:00:16.524 The long-winded way is to create another function, 8 00:00:16.524 --> 00:00:18.010 get underscore songs, let's say 9 00:00:18.010 --> 00:00:20.449 and bind that to the album's ListBox. 10 00:00:20.449 --> 00:00:22.584 And the approach we'll take in the next video 11 00:00:22.584 --> 00:00:24.955 is to subclass our ScrollBox class 12 00:00:24.955 --> 00:00:27.565 and get the ListBoxes to populate themselves. 13 00:00:27.565 --> 00:00:30.128 Sometimes you have to start duplicating code 14 00:00:30.128 --> 00:00:32.213 before you spot that there is duplication. 15 00:00:32.213 --> 00:00:34.277 So I'm going to get this songs list working 16 00:00:34.277 --> 00:00:36.271 so we can see what the common code is 17 00:00:36.271 --> 00:00:38.679 and how we can ultimately avoid that. 18 00:00:38.679 --> 00:00:42.729 Now get_songs is going to be very similar to get_albums, 19 00:00:42.729 --> 00:00:46.896 I'm going to add it just below the get_albums function. 20 00:00:50.758 --> 00:00:52.207 And leaving two blank lines is normal 21 00:00:52.207 --> 00:00:54.989 between functions there or methods. 22 00:00:54.989 --> 00:00:58.156 Def get_songs(event): 23 00:01:00.480 --> 00:01:02.313 lb = event.widget 24 00:01:05.990 --> 00:01:08.715 now we want to do an index 25 00:01:08.715 --> 00:01:11.487 index = int(lb) 26 00:01:11.487 --> 00:01:15.070 Let's close down the update or do that update a bit later. 27 00:01:15.070 --> 00:01:19.256 (lb.curselection() 28 00:01:19.256 --> 00:01:21.861 then we want square bracket zero 29 00:01:21.861 --> 00:01:23.360 and a right parentheses 30 00:01:23.360 --> 00:01:25.777 album_name = lb.getindex 31 00:01:29.017 --> 00:01:30.997 or get parentheses index I should say, 32 00:01:30.997 --> 00:01:34.319 then that all-important comma on the end. 33 00:01:34.319 --> 00:01:36.269 And then we're going to get the artist ID 34 00:01:36.269 --> 00:01:38.107 from the database row. 35 00:01:38.107 --> 00:01:41.774 So # get the artist ID from the database row 36 00:01:44.007 --> 00:01:46.671 and when we do that by typing 37 00:01:46.671 --> 00:01:49.088 album_id = conn.execute 38 00:01:51.973 --> 00:01:54.162 parentheses and then double quotes, 39 00:01:54.162 --> 00:01:55.579 SELECT albums._id 40 00:02:02.783 --> 00:02:03.700 FROM albums 41 00:02:05.279 --> 00:02:06.696 where albums.name 42 00:02:09.741 --> 00:02:12.523 is equal to question mark, end of double quote 43 00:02:12.523 --> 00:02:14.606 comma, space, album_name, 44 00:02:16.259 --> 00:02:19.551 finish the parentheses and then dot fetchone() 45 00:02:19.551 --> 00:02:22.627 and make a call to that method. 46 00:02:22.627 --> 00:02:24.210 And alist = [] 47 00:02:26.527 --> 00:02:30.520 and we want to select the song, so we're going to do 48 00:02:30.520 --> 00:02:32.520 for x in conn.execute 49 00:02:34.367 --> 00:02:37.839 and the SQL line of code in double quotes 50 00:02:37.839 --> 00:02:39.339 SELECT songs.title 51 00:02:41.303 --> 00:02:42.636 FROM songs WHERE 52 00:02:44.187 --> 00:02:45.687 songs.album=? 53 00:02:49.014 --> 00:02:51.264 And ORDER BY songs.track"", 54 00:02:54.548 --> 00:02:56.297 double-quote, comma, space 55 00:02:56.297 --> 00:02:57.464 and album_id): 56 00:03:02.162 --> 00:03:03.495 and alist.append 57 00:03:05.245 --> 00:03:07.245 (x)[0]) 58 00:03:08.478 --> 00:03:10.561 then end the parentheses, 59 00:03:10.561 --> 00:03:12.126 then the last line of code 60 00:03:12.126 --> 00:03:12.959 songLV.set 61 00:03:15.254 --> 00:03:17.602 I've seen this in the get_albums method 62 00:03:17.602 --> 00:03:21.602 (tuple(alist)) 63 00:03:22.971 --> 00:03:24.725 So really apart from the column and table names 64 00:03:24.725 --> 00:03:26.208 this code is pretty much identical 65 00:03:26.208 --> 00:03:29.290 to the get_albums function. 66 00:03:29.290 --> 00:03:31.022 So we just have to bind the function now 67 00:03:31.022 --> 00:03:33.744 to the album ListBox selected event 68 00:03:33.744 --> 00:03:36.438 and the songs list will hopefully be updated 69 00:03:36.438 --> 00:03:38.493 when we click on a new album. 70 00:03:38.493 --> 00:03:41.243 So we'll go to our albums ListBox 71 00:03:42.098 --> 00:03:45.624 and after the config border=2 line 72 00:03:45.624 --> 00:03:46.614 we're going to type 73 00:03:46.614 --> 00:03:47.781 albumList.bind 74 00:03:50.545 --> 00:03:51.970 ('<< 75 00:03:51.970 --> 00:03:54.714 single quotes, less than, less than 76 00:03:54.714 --> 00:03:55.547 and it's going to be 77 00:03:55.547 --> 00:03:57.238 ListboxSelect>>', 78 00:03:57.238 --> 00:04:00.580 again greater than sign, greater than sign, single quote, 79 00:04:00.580 --> 00:04:01.413 get_songs) 80 00:04:04.478 --> 00:04:05.626 to call that method. 81 00:04:05.626 --> 00:04:07.238 Again there's no parentheses there 82 00:04:07.238 --> 00:04:09.676 after the get_songs. 83 00:04:09.676 --> 00:04:11.509 So now if we run this, 84 00:04:17.986 --> 00:04:20.474 we should see that the songs will update 85 00:04:20.474 --> 00:04:21.539 when we select an album. 86 00:04:21.539 --> 00:04:23.872 So let's just click on AC DC 87 00:04:24.775 --> 00:04:26.197 For Those About to Rock 88 00:04:26.197 --> 00:04:28.078 And you can now see the songs are now showing up here 89 00:04:28.078 --> 00:04:28.911 on the right-hand side. 90 00:04:28.911 --> 00:04:29.744 So excellent. 91 00:04:29.744 --> 00:04:30.951 We got that to work. 92 00:04:30.951 --> 00:04:33.261 But there is a bit of a problem here still, 93 00:04:33.261 --> 00:04:34.417 so we select another album, 94 00:04:34.417 --> 00:04:37.000 like for example Axel Rudi Pell 95 00:04:38.009 --> 00:04:40.461 select one of the albums to show. 96 00:04:40.461 --> 00:04:42.995 That's fine but if we now select a different artist, 97 00:04:42.995 --> 00:04:45.141 the songs from the Axel Rudi Pell album 98 00:04:45.141 --> 00:04:47.001 are still being displayed. 99 00:04:47.001 --> 00:04:48.547 So come over here 100 00:04:48.547 --> 00:04:50.137 they haven't been reset in other words, 101 00:04:50.137 --> 00:04:52.933 until we actually select a different album 102 00:04:52.933 --> 00:04:54.485 for that particular artist. 103 00:04:54.485 --> 00:04:56.653 So really we need to reset the songs list 104 00:04:56.653 --> 00:04:58.717 whenever the artist changes. 105 00:04:58.717 --> 00:05:01.543 So let's go ahead and do that. 106 00:05:01.543 --> 00:05:03.989 And that's pretty easy to do. 107 00:05:03.989 --> 00:05:06.741 And it's done down here, or up here I should say, 108 00:05:06.741 --> 00:05:08.824 In the get_albums method. 109 00:05:13.237 --> 00:05:14.625 So we initialise it in there, so we put 110 00:05:14.625 --> 00:05:18.792 songLV.set(("Choose an album",)) 111 00:05:29.513 --> 00:05:32.180 so let's try running that again. 112 00:05:33.908 --> 00:05:35.349 So we'll do the same thing now. 113 00:05:35.349 --> 00:05:38.003 We'll just choose Axel Rudi Pell 114 00:05:38.003 --> 00:05:39.429 choose an album 115 00:05:39.429 --> 00:05:40.433 that works okay. 116 00:05:40.433 --> 00:05:42.421 And if we go back to say to AC DC, 117 00:05:42.421 --> 00:05:45.525 we can see the songs is correctly updated, 118 00:05:45.525 --> 00:05:48.683 or the message, default message, is now showing 119 00:05:48.683 --> 00:05:49.792 in the songs list 120 00:05:49.792 --> 00:05:52.051 and the previous selection, or list of songs, 121 00:05:52.051 --> 00:05:53.384 has been cleared. 122 00:05:53.384 --> 00:05:55.103 So basically the song list now is cleared 123 00:05:55.103 --> 00:05:56.761 whenever a new artist is selected 124 00:05:56.761 --> 00:06:00.219 and that's actually quite important 125 00:06:00.219 --> 00:06:03.113 because it does have a slight impact on our subclass. 126 00:06:03.113 --> 00:06:06.696 But we'll see about that in the next video.