 Right, so hey guys and welcome back to another Python tutorial So today's actually going to be a pretty interesting tutorial Because what we're going to be doing today is going to be using Teginta to create a custom music player for us So what this is going to do is just going to allow us to select a song from our computer and then play it We're also going to have different controls such as increasing or reducing the volume or pausing or resuming the Song that we have playing so it's going to be a pretty basic music player But at the end of the tutorial you'll be able to have your own custom gooey version of a music player Which is pretty cool in my opinion. So first off like always we're going to go ahead and open up visual studio code I'm going to go ahead and do that Once it's all loaded up. What I want to do next is Create a new file. I'm going to save this as Music player dot pi dot pi because it's a Python file and we save Python files with a dot pi extension So let's save that up now that it's all saved up We're going to have to do a few imports for us to work with this. So we're going to need something called mixer Which is going to be imported from our pie game module. So we're going to do from high game Import mixer. So we're going to be using the mixer Class right here to later on carry out different tasks such as play music resume music pause music or change the volume of it Now for our tick inter we're going to be I mean for a gooey. We're going to be using the module tick inter Now we usually just do Tick inter import start. This is like beginner stuff just because you guys were beginning and I just taught you to use this but you shouldn't really be using this you should be Importing the classes that you need separately instead of importing everything together. There's different Reasons to why you shouldn't do this, but I'm not going to go for all of them in this tutorial either way We're only going to import the classes that we need for my library. So from tick inter import Tk because we're going to be needing that Label we're going to be needing that to create labels on a screen from tick inter Import button that's for creating buttons on a screen import Entry to get the actually we actually don't need the entry for this one So I'm going to get rid of that and finally from tick inter import file dialogue So what the file dialogue does is it basically lets the user select the file so something like this So when I click on file open file this dialogue right here, which lets me select a file for my visual studio code That's going to be the file dialogue That's going to pop up for our music player so that user can select a music file Cool, let's close this off and let's start coding the actual gooey. So first off what I'm going to do is Go ahead and start programming my main screen. So create a section for main screen And then I'm going to call my main screen master and assign that to TK if I run this quickly We're going to have a actually we need to also do Okay, let's get rid of that master.main loop in order for this to actually work So on this and as you see here right here, we have a little window showing up and it's called TK Great. So now let's go ahead and give it a title. So master.title music player That's fine. Now we're going to go ahead and add a few labels onto this. So labels First label is going to be place a master. The text is going to be Custom music player. Now, this is sort of like the title for my music player I'm going to be able to also set a font for it Which is going to be calibrary like we always use and then I'm going to set it with the size of 15 Then I'm also going to change the foreground To be equals to red which means I want the color of the font to be red And finally I'm going to use dot grid to place it on my screen now I'm going to be using sticky equals and then make sure that You are using speech marks and then end because since we haven't done from To get to import star we need to use speech marks here. Then you need to use common and row equals Zero then we're also going to be doing adding X Equals 120 which means it's going to have a space of 120 units from the left and from the right Now lastly, I'm going to try and figure out why I have this error over here positional argument Hmm weird actually I need to do font equals so after font you need to do equals and then save that up It should work fine Now let me quickly run this up to see if it has actually worked And as you see right here I have a little lovely window popping up that says music player as a title and then custom music player as the label and it's all Also left to 120 units of space on the left and from the right making the window a bit bigger in width Cool. Now, let's go ahead and add a few more labels that will help put some life to this program So I'm going to copy my first label and then I'm going to paste it right here just to save some time now for the second label I'm going to say I don't know please Please select a music track you would like To play cool, that's not friendly enough and I'm going to change the font size to actually be 12 because 15 is for the heading let's make 12 the normal Standard size for pretty much everything else. I'm going to change the foreground to blue. Why not just add some color in there Obviously my color choices terrible, but just for the tutorial. Let's go with it And then I'm going to set the sticky to north which is fine We don't really need the padding X because it's already been applied and we change road to one Let's run this to see if that worked and it has worked So we have the title and the subheading as well. Let's close this off and start coding the rest too So what's going to be next is we're gonna have a label that's going to be showing us a little volume section So let's go ahead and do that So label Master and then we do font equals Telepathy Paul cool, so What we do after that is we're not actually going to be Put it on a grid putting it on a grid straight away We're going to be assigning it to a variable and then putting it on a grid. So song um title Label, so let's do the song title label first and then we'll do the volume label the song some title label equals Calibri comma 12 and then song title label grid Now sticky is going to be equals to north and Row is going to be three now You might argue why row equals three because we've only got up to row equals one now I need to leave row two to actually be for a button that's going to allow the user So there's going to be a button below the please select a music track. You would like to play We're just going to allow the user to pretty much select the track. They want to play So we're leaving that space for the button. Cool. That's why we've skipped a row and then we've gone to three instead of two Now what we're going to do after that is create a volume label. So volume label equals Label and then in their master and then font equals Calibri and then 12 for the font size now, we're gonna use this and then place it on our screen. So dot grid sticky equals north Row equals five Cool So these two labels right here the song title label is going to display the title of the song or the name of the song And then the volume label is going to show the volume the current volume that the song is playing on Now obviously these are not going to be filled in from start because we don't know what song is being played or what volume The song is being played by default. We're going to be adding text to these variables later on. So that's fine since we have them ready We also went on to row five right here instead of four four because we're going to be needing the volume Buttons, that's why we have left a row. Cool. So now let's go ahead and create our buttons. So Buttons, I mean if I if I run this just to show you guys what's going on It's gonna look the same But just a bit of space right here because we've got empty labels that haven't got any text in them We're gonna be populating them later, but for now just stick with me. So we're gonna have a button It's going to be placed on master and the text is going to be select song Now as you see right here, I'm going to also set it a font of Calibri and 12 for the font size command we're gonna assign that later and for grid we're going to do Row equals to like I said before we left a empty row for this and We're gonna do sticky equals north because we want it to be right in the middle Let's run this and as you see right here I have a little button that says select song Now if I click on it nothing happens because we yet need to add a command to it Which is gonna run a function when we click on it. Cool. Let's close that off Let's add also a little padding Actually, we'll leave that alone. So once we're done with that, we have done our first button Now let's go ahead and add a few more buttons. So next button is going to be on master as well text is actually going to be fours and one is going to be Let's see Calibri and Then font size four With a dot grid Where the row is going to be three and then sticky is going to be east Cool, so we're placing this pause button on the same Same line as the song title label So it's going to be placed on the east which means on the very left. Let's run this. I Mean on the very right my bad. So it's going to be placed on the left I mean on the right because it's placed on the east and then in the middle is going to be the song label Which we're going to add later Okay, that's fine Now I'm going to copy this because we're wasting a bit of time tapping this out each time now I'm going to change the text to actually be resume because we need a resume button if we have a pause button and Then I'm going to change dot grid where the sticky equals to west and The row can stay the same which is fine because we want it to be on the same row just on to the left side of the screen So we're going to have resume on the left and pause on the right Which is fine And then we're going to have the song title in the middle because it is placed based on north which means middle Cool. Now, let's go ahead and paste that again because we need volume buttons as well Now for this I'm going to change the text to Let's just say plus for now because it's just volume plus I'm just going to Paste minus before that because we're going to have minus on the left and plus on the right so minus plus and then Dot grid is going to be world equals five And then we're going to do the same over here or equals five, but we're going to do sticky equals west Oops sticky equals west For our minus button and we're going to do sticky equals east for our plus button Let's run this to show you how it looks like And as you see right here, we have resume pause minus plus the minus plus is for the volume and the volume Label is going to go right in the middle because it's already been placed up here So it's on the same row row five, but it is placed sticky equals north which is in the middle Cool. So now what they're also going to do is give the button these two buttons need a width because they look Kind of small. I'm going to give them a small width So I'm going to go in my button attributes and then after once I'm going to do a new attribute for width Where I'm going to assign the width to five Same down here for the plus button. So minus width equals five. Cool. So let's run this now And as you see right here, we have our minus and plus buttons that are ready as well. Cool. So What we also need to do is As you see right here, we've skipped a row. We're missing row number four So that's what was meant to be for a volume label. So let's go ahead and do that Let's go back to our label section copy of form line 13 Create a new line and then paste it right here And then in here I'm going to change the text into Volume This is just to separate it kind of and then everything else is going to be the same We're going to change the foreground to red And then sticky equals north row equals four Now, let me show you what that's done As you see right here, it's created a volume label and created a separate a bit between the pause resume So it kind of looks a bit better than it did before Cool. So that's that done now. We need to actually add commands to these buttons So what we're going to do to add commands is I'm going to close this off first of all And in this tutorial, I'm going to be showing you how to add those commands too So let's go ahead and take a look at how to add the commands So first of all, we're actually going to do the command for playing the song the controls We're going to do them later. So to start with we're going to need a variable up here. That's going to be called Current volume. So this is going to store A float At point five. So the current volume is going to be point five So the volume is going to always start on point five. So half the volume of your system Which is fine. Now we need to also go ahead and this button right here and create a command so that it can actually look for a function comma command equals Play Song So I'm going to create a function very soon called play song because every time this select song button is clicked We're going to have this um play song function wrong So above my main screen. I'm going to have a section for functions. So let's do that Functions and def play song is going to have First it's going to have a new variable which is going to be called file name Now this is going to store a file name of the song that's being selected. So We use file dialogue for that. So file dialogue dot ask open file Ask open file name because we want the file name. Um, and then we're going to do initial Initial initial directory equals c. So whenever this um window opens up It's going to open in our c directory and then I'm going to title equals Please select a file Let me show you what that does and I'm going to print whatever oops Go down here and I'm going to actually print whatever file name is being returned here So let's run this Select a song and as you see right here We have a little pop-up window that opens up straight in my c drive because I've said initial directory to be there And it says please select a file update. So let me select the file right here And once I've selected it if you notice in my window down here since I asked it to print it out It prints out the exact location of the file including the name of the file So that's exactly what we want so that we can target the song we're looking for So what we're going to do next is get rid of this print statement and actually work with the file name that we just Gathered So we're going to create a new variable for current song and then we're going to equals that to file name Because that's just a bit more generic to remember than just file name Now we're going to do song title as well. Now the song title is going to be um us having to split the file name Using um forward slash and then we're going to print Song title, okay, actually we're going to have to override the song title variable With song title at minus one position So it's going to pick up whatever is in the last position of this array Which is pretty much going to be the name of the song and the extension of the song So if I print song title, we're going to be returned the name of the file and the extension of the file We're not going to have this long ass directory. So what I'm going to do next is on this Select the song I'm just going to select this random thing right here And as you see it's only returning the name And the extension of the file instead of returning c Then drive then users then whatever it's not returning the whole directory. It's just returning the name Which is what we want as the song title. So now we've got that done as well, which is pretty good Let's actually go ahead and create a try and catch exception because we need to work with that too Just in case any errors happen So mixer is what we're going to be using to actually play the music So mixer is an object that we have we need to initialize it by just typing in it open close brackets Now mixer dot load And then we're going to do mixer dot load um Actually mixer dot music because we're loading music music dot load And we're going to use current song variable, which is the directory to the song Then we're going to do mixer dot music. Why did I have caps on mixer dot music dot set Volume so this is going to let us set the volume of the song and we're going to set it to the current volume Which is point five, which is always going to start at that Then we're going to go ahead and do mixer dot music Dot play which is going to play the song Now lastly we're going to grab our song title label that we created earlier And then we're going to use dot config to actually add some text to it So foreground is going to be equals to green because it's a happy message. It's not an error And then we're going to do comma text equals now playing Hold on oops And then we're going to do plus string Song title So it's going to populate the label by saying now playing whatever the name of the song is So let's go ahead and actually give that a go but before we do that We're also going to populate what's wrong here unexpended unexpected indent Okay, that's fine. Anyway, so before we do that We actually need to also update the volume label to actually show the user what volume this song is playing at So volume label dot config foreground equals green on our text equals Volume and then we're going to do plus string Uh current volume Which is fine It should do the job for us and then we also need to do the exception So before we actually run this we need to do the accept statement too. So accept exception As e and now we're going to print the exception that happens just in case any exception does happen And then we're going to do some title label dot config foreground equals red possess and error And then we're going to do text equals error playing track Cool. Let's run this now to see if it actually works Um select song. I've got a song on my desktop. Which is one of the chill music I've got Let's play that And I'm not sure if you guys can actually hear But I'm going to go ahead and reduce the volume a little bit So I'm not sure if you guys can actually hear but the music is actually playing through my earphones I'm not sure if you guys would be able to hear the title of the song is showing up here as well showing up right here if I Um Obviously the window is a bit small for it But if we increase the window size that will be fine But it's showing us the label and it's also showing us the volume So I'm going to go ahead and actually try to error this out Actually, let me close this because it's a bit annoying Cool. So that's nice. Let me open this up Uh, what I'm going to do is select a song and instead of selecting a song I'm going to select a random file and as you see as we programmed it it says couldn't read from um Whatever it needs to read from because that's the exception that's occurred And then it says error playing track right here because it's an invalid file Cool. So that's our first bit down success. Now. Let's actually go ahead and program the different actions we have like pause resume Volume up and volume down It's going to be pretty easy. So let's start with increase volume and reduce volume So what we're going to be doing is going to uh volume functions right here. So minus and plus So on the minus I'm going to do a comma comma and then come on equals reduce Volume and then on the plus I'm going to do a comma command equals Increase Volume cool. So let's go ahead and program our increase volume function first So below here after the play music function, let's have death Increase volume. So once one of these functions is done We just got to copy and paste it into the other function But using a minus because we're just going to be minusing instead of adding So try and catch obviously again Now we need to do global current volume so that the function knows to use this variable up here And not make a local copy of it. So every time we refer to current volume It's going to be referring to this variable up here, which is fine. And we're going to do if Current volume is less or equal to zero. Then we do volume label dot config foreground equals red um text equals Volume muted so It's going to be muted So what this means is if the current volume ever reaches to zero or less than zero, which it will stop at zero anyway Uh label for the volume is going to change to muted because it is quite rightfully muted And then we're going to make a return from there so that so that the Volume cannot go in minus because that's just trash. That's not how it's meant to work, right? And then if that doesn't happen if the volume is not less or equal to zero Then we are going to do current volume equals current volume plus Um, actually I've done the thingy for the reduce volume in here. I've just realized So I'm going to create the reduce volume first. So let's go ahead and rename from increase to reduce Um because I just accidentally started programming my reduce function first So that's the reduce and this will make more sense in there So this is to reduce the volume and not increase it and then if the volume is not less or equal to zero then we minus current volume from Whatever It already had so whatever value it was on and we minus point one from it because each time we're going to just minus point one And let's make this a float just in case it doesn't already know float Oops Just fix that quickly Cool. So current volume equals current volume minus float 0.1. So each time the user reduces the volume if it's not zero or less than zero We just reduce um point one from the volume Which is fine. Now what we need to do next is go ahead and Overwrite this by creating a rounded value for this around current volume comma Um one which means I'm restricting this value to be just one decimal place so that it doesn't show me more than that I don't want to show more than that anywhere And then we're going to use the mixer again dot music Dot sets volume and then we're going to pass in our current volume So this is the updated current volume, which was um updated by taking away point one from it Which is fine. And then we need to update the volume label as well. So volume label dot config Uh foreground equals green Actually, we're going to do foreground equals. Yeah green is fine green comma text equals volume Plus string String and then we do current volume again because this is the updated volume remember, which we also rounded up And then finally we do the exception. So accept exception As e If there's any exception we print the exception and then we're going to do song title Uh, not song title label Uh, yeah, actually song title label dot config foreground equals red and text equals track hasn't Oops hasn't been selected yet So this error will mainly happen when the user tries to click on the volume minus button And the mixer is not initialized, which is when the track hasn't been selected So the user will be let know just in case that happens So this is done and hopefully you should work So what I'm going to do is copy this whole function because it's pretty much the same For our increase volume as well. So I'm going to copy this control c It's very hard for me to work through this because it's quite soon did but hopefully it helps you guys out And then I'm going to do increase volume here. Let's change the reduce to increase And now instead of if current volume is great less than or equals to zero I'm going to do if volume if current volume is Greater than or equals to one because one is the max volume we can have Then we do volume equals volume is max And then we got to change this to green because it's not muted Change the foreground to green Then we do the same thing, but we're going to add a float point one instead of taking away Then we're going to round it up. We're going to play it using the we're going to update the volume to the current volume Which is the updated one. Which is fine We're going to have the volume showing up and we're going to have the exception too, which is perfect Let's go ahead and see if this worked Hopefully it did. Let's go to my desktop now Play this up And I'm on volume point five right now. Let me reduce it And I'm not sure if you guys can hear but muted right now If I go point one It is actually playing at point one, which is pretty slow. And if I go all the way up to Max it shows up max So I'm going to mute that back down because I don't want to listen to or interrupt our tutorial So that is actually working because we're able to change the volume. So I'm going to close this off. Perfect I'm glad we haven't had a lot of errors in this tutorial actually first time pretty good So all the only two Commands we actually need to program now are the pause and the resume functions So let's go ahead and add commands to those two. So those are actually pretty short and simple Let's go ahead and do that. So I'm going to go to my pause button first up here comma command equals Pause and then I'm going to go down to my resume button down here Comma command equals resume, which is fine Now I'm going to actually have to go and create these functions because they have been already assigned Let's go ahead and do that So down here deaf pause Um, so for the pause what we're going to do is try first and then we're going to do mixer dot music Dot pause just as simple as that because the mixer library is pretty clean. It's pretty nice The class just lets us do things easily and then accept exception as e print e And then we're going to have to update the song title label to say there was an error song title label dot config foreground equals red text equals Track hasn't oops not track track Hasn't been selected yet Which is fine now that's the error exception done as well So our resume function is pretty similar, which is why I'm going to copy and paste this So I'm going to go ahead and copy And go below this right here Paste it and then we're going to change the function name of course to resume because it is not a pause function It's a resume function And in here we're instead of doing music mixer dot music dot pause. We're going to do mixer dot music dots unpause They should have just named it resume, but the method name is unpause, which is fine. No, um obligations with that And the exception will still remain the same just in case the user clicks on the pause or play button before The mixer has been initialized. We're going to say tracker or track hasn't been selected. Let's quickly run this up Hopefully we'll work in the first go because the tutorial is already quite long Let's select a song and go ahead and Select the actual song Now I'm going to reduce the volume all the way down to point one because I don't want to interrupting my beautiful voice, of course Just kidding. I'm going to click on pause And as you see right here, I'm not sure if you guys can actually hear my audio But hopefully you can when I click on pause the song actually pauses I'm going to click on resume it actually resumes so pause and resume are working fine as well So that brings an end to this tutorial guys. Hope you have enjoyed this tutorial and learned something new I know this is this was a pretty exciting and simple project for me to do Which is pretty cool to create which is um Less than 100 lines of code as well. So anyway guys, thanks for all the support that you guys have been showing me recently If you have any new ideas for the channel, you can drop them under the community tab Also, um You can join the discord channel that's going to be stated in the description follow up my socials and all that If you guys would like to contribute to the channel directory you can do so either using The patreon link that I have in my description by becoming a patron Or you can also purchase a super chat emoji or a highlighted message when the video premieres Anyway, guys, I will see your beautiful faces in the next tutorial. Peace