 So hey guys and welcome back to another Python tutorial. So as it sounds out, this is going to be the last tutorial on the banking GUI app using tkinter. So in this tutorial, we're going to cover how to create our deposit and withdrawal functions which are the two main functions that are going to be used in our tkinter application. So first off, let's go ahead and take a look at the user requirements that we set ourselves when we started out. We're going to go ahead and cross off what we've already done so we've allowed the user to log in in the last tutorial and we have also allowed the user to view balance and personal details. I'm going to cross that off too. So in this tutorial, I'm going to show you guys how to allow the user to deposit money and update the file as well as allow the user to withdraw money and then this will update in the file for the user. Now for you guys as a challenge, I'm going to leave this bit out. I'm not going to do this and I'm going to expect you guys to do it on yourself just so that you guys have a bit of practice. So I'm going to let you guys try and figure out how to allow the user to edit his personal details in the file that they're stored. Right, cool. So to begin with, I'm going to go in my folder, banking, Go eApp. And then in there, I have my Yo-Han file where I've stored my details at the moment. Right, so if I double click that and if I open it in Notepad, as you see right here, my username is Yo-Han. The second line of my password is password, my age is 21, gender male, and zero is the balance. So that's the storage that stores our accounts and this is the code that we got to in the last tutorial. Right, so in the last tutorial, we were able to use our login buttons to pretty much log in into accounts that already exists and we also had validation. So if I logged in and if the password is incorrect, then it says that. And if I type in an account that isn't valid, it says account not found. Right, so let me log in quickly just to show you what we did last time. Log in. And if I clicked on personal details, it tells us all the personal details for this account that are in the text file or the plain file. Right, so in this tutorial, we're going to be adding some life to this deposit and withdrawal buttons because right now they just say deposit and withdrawal when you click on them. So if you happen to watch the first two tutorial guys, I recommend you do because those two are the base of this application. I'll be listing the link to the two tutorials on this series from the beginning, so part one and part two in the description. So go ahead and watch those first and then come to this tutorial. So without further ado, let's begin. So let's go ahead and navigate to the functions, deposit and withdrawal. So we're going to do deposit first because it's a bit easier than withdrawal. And then the withdrawal, it's going to be a lot easier. So let's start by erasing this line that says print deposit. And now I'm going to create some variables in here. So variables. Now in the deposit, we need the user to enter an amount that they want to deposit. So I'm going to create a global variable called amount. I'm going to create a global variable called deposit notif. So this deposit notif is basically going to be a label that's going to be created later down the tutorial, which is going to update the user on whether the deposit was a success or whether it was a failure or any other information related or in regards to the deposit. Whereas the amount is going to be the variable that pretty much temporarily stores the information about the amount that's being stored or that's being deposited. So I also need to do another one. So global current balance label. So as it may already suggest, this label right here is going to show the user their current balance and it's going to keep updating as the user keeps making more deposits or withdrawals. Cool. So that's just for the labels. Now let's create the actual variable. So amount equals string var. Now you guys may argue that it's a number and we're going to have to do operations with it, but let's begin by assigning it to a string variable and later on we'll convert it to a float, which means that we can have decimals. Obviously we're going to need decimals, so we're going to be creating a floating point later and converting this into that. Cool. So first off what we want to do is go ahead and open our file that stores all the details so that we actually know what our starting balance is. So in the last tutorial we had globalized this login name variable from the login session. So global login name. So whatever name the user uses right here and when it's validated and all successful, we're able to reuse that name again in our deposit so that we actually know which user file to open. So we're going to be opening the login name file so whichever user has just logged in successfully. We're going to be opening that file. We're going to be opening that in read mode because we only need to be reading right now. Then we're going to do something like file data equals file.read. So that's going to read the data from the file. Now user details equals file data dot split. Oops, what's going on with my typing? fold slash n. So what this is going to do it's going to return an array which is going to store all the details on a separate line. So name is going to be on the 0th line of the array. The age is going to be on the first I mean the password is going to be on the first line and so on. So if I do details balance I'm going to assign that to user details. For that's because I know that the fourth item in this array is going to be the balance because the balance is pretty much the last item that we saved in our file. And then when this is split it pretty much takes each line and then stores it as a list as an item in that list. So I've already experimented and I know that my fourth item in this list user details is going to end up being the user's balance. So the current balance. Cool. So we have the current balance now. Let's go ahead and create our screen. So deposit screen. Let's go ahead and call this deposit screen equals top level master. Which means we're going to create a pop-up window based off our master screen. Now let's go ahead and give this screen a title of deposit. And after that we're going to have to create a few labels. So label first label is going to be deposit screen. So that's what we're placing it at. Then the text is going to be able to deposit. So d e p u s i t font is going to be Calibri. Let's close that comma 12. And then we're going to use grid to place it on our screen. So row is going to be zero. It's going to be placed in the first row of our GUI. Sticky equals north. Which means it's going to be placed in the center. And padding Y equals 10. Which means it's going to have 10 units of space from the top and bottom. Cool. Let me show you how it looks like. Log in. And if I go to your hand. Password and login. Deposit. And we have no errors. Perfect. So as you see right here we have a nice little deposit thingy showing up right here. And it has a padding of 10 units from the top and bottom and it's placed on our first row. Perfect. So now let's go ahead and add a few more labels which we're going to be needing. So we're going to be needing the user to actually type in the amount that they want to enter. So before that we're going to need another label that shows the user their current balance. So we're going to have to name this label. So current balance label equals. I'm just going to copy and paste now. Because it can't be asked. And it saves time too. So copy and paste. Text is going to be changed to current balance. Let's use a colon and a pound sign. And then we'll do a plus details balance. So it's going to show the user the initial or starting balance of whatever is being read from the file. So we have this details balance right here being read from the file. So from the user's file. So we're going to be showing the current balance of the user in the start. Now we're going to have to place this on a grid. So current balance label dot grid. We have to place this to row equals one comma sticky equals quest because we want it to be placed at the very left. Cool. Let's go ahead and create another label which is going to allow the user to actually enter the amount that they want to deposit. So I'm going to copy and actually I do have a copied version already. So I'm going to change the text to amount. And add one of these colons. And then we're going to have to change the dot grid. So we're going to place it on our screen. Row equals two comma sticky equals west. Once again I wanted to stick to the very left of the screen. That's why I'm putting it to the west. Now we need another label which is deposit notice. So I'm assigning these labels to variables because we're going to be configuring them later to change their information. And in this label we don't need to enter any information yet. Font equals calibri comma 12. And then the only thing you've got to do is deposit notice dot grid. And I'm going to place this on row four. You guys might say why row four? Why not row three? That's because we still have another button that's going to be placed above this. And so that's why I've skipped a line. I mean a number. And then pad y equals five. Perfect. Cool. So that's our deposit notice done. And it's a global variable. So we can pretty much access it from anywhere when we need it to. So that's our deposit notice label done as well. Let's go ahead and do our entry now. So entry. We're going to have only one entry for the amount to be entered. So entry deposit screen. And our text variable as discussed in the start is going to be equal to amount. So that variable is going to temporarily store the amount that we're entering in there. And then dot grid. Row equals two, column equals one. Because I want it to be on the same row as the label for it. So row equals two, row equals two, but column equals one. Cool. Now lastly, I'm going to go ahead and add the button that's going to be running all of this stuff. So this button is going to be placed on deposit screen once again, comma the text is going to be called finish. And then font equals. I should have just copied and pasted, but that's fine. 12. And we're going to place this row equals three. So remember I said I saved that space for this button right here. Sticky equals rest and then padding Y equals five. Cool. I'm not giving it a command yet because we're going to do that in a moment. So let's run this and let's see if it actually works. So login your hand and password. It's going to be surprising if there's no errors. Oh, as I said, I just called it. Tuple object has not so deposit, not a dot grid. I probably have added an extra bracket or something like that. All right. So I forgot to put the, I'm not even sure what I did here. So this is meant to have a bracket right here that is meant to be deposit screen. And then the end is going to have a bracket. So if you guys could fix this line right here, sorry about that. Let's run this again. And hopefully we won't have more errors. But every time I say that, I kind of jinx myself. Password login deposit. Okay. I didn't jinx myself this time. Cool. So it's not the best looking GUI, but hey, it is something. We have something showing up right here. We have the deposit showing up at the top. We have the current balance equals zero down here. And we have an amount entry right here. I'll leave the designing up to you guys so you can design it to however you like. So essentially there's some space at the bottom underneath the finish button because that's the hidden notice label that we have. That label is going to show whether the transaction was successful or not successful. So when I click on finish, that label should show up later. Cool. Now what we're going to do next is we're going to assign this button to a function or a command in this case. So command equals, what did we call it? We called it finish deposit. Cool. So we're going to have to create a function that's going to be called finish deposit. Now what that function is going to do is it's going to grab these values. So it's going to grab the amount. It's going to grab the deposit notice. It's going to grab the current balance label. And first off, what it's going to do is it's going to find out what the current balance is. So it's going to read the user's file, find out what the current balance is. Then it's going to take the amount and add it onto that current balance. Once it's added it on, it's going to save it back to that text file so that the amount is now updated so that the user balance has updated to the amount that is deposited. And then it's going to update the deposit notice to save that transaction success. And it's going to update the current balance label to display the most updated balance. Cool. So that may have sounded confusing, but if you understood, perfect. What we're going to do next is create a new function called um, def, uh, finish, deposit. And then what we're going to do first in this function is we need to actually verify whether the amount being entered is a valid, um, if there's actually anything being entered in there. So if I did if amount, don't get, and now remember that amount is just a temporary variable that we had that was storing the amount in the last one. So we have to use the dot get method to actually get the data. So we're saying if amount is blank. So if the user doesn't enter anything and the button just gets pressed by accident, we're going to do, we're not going to crash, but we're going to do deposit notice, which is the, um, the empty label that we created to show notifications on the previous screen, deposit notif.config, we're going to add some text to it. And that's going to equals to text equals amount is required. Cool. And then we're going to make the foreground equals to red because it's sort of an error. And then we also need to check whether this amount that's being given to us is less than zero because if it is zero or less than zero, we're not going to deposit that because we can't really deposit negative items or negative values. So we're going to say if now remember what I said before, we're storing it as a string, but when, uh, in order to do math with it, we're going to have to change it to a float. So if float amount, don't get. So now this amount is going to be changed into a decimal number is less or equal to zero. Then we do deposit notif.config, um, text equals negative currency is not accepted. So this is sort of just us making sure that we're covering all the different validation rules that I can think of at the moment. And I forgot to say before, but at the end of it, we need to do a return because we're not using an L statement. So do a return at the first if statement as well. So we need two if statements want to check whether the entry actually has information and the second one to check if the value given is equal to zero or less. Cool. Now let's get into the actual code. So we're going to create a new variable called file. We're just going to be equal to open. And once again, we're going to be using the global variable called login name to find out who just logged in. Then we need to open the file in R plus mode because R plus means basically read and write so we can read and write to this file because we're going to be writing to this file to update the balance. So file data equals file dot read. Sort of just repeating ourselves right now. Details equals file data dot split. So this will take each line and save it as an item in the array. And our current balance equals details four because I know that the fourth item in my array is going to be my current balance, which is read from the file. Now we have a new variable called updated balance, which is going to equals to current balance. And then we take the updated balance each time and we do equals updated balance plus whatever amount is given to us. And we need to change the amount into a float because it's a math operation. We can't just add a number to a string. Cool. So now each time there is an amount that's being added, it's going to be updated and stored in our updated balance. Now we still need to write this information to our file. That's what we're going to be doing next. So the file data that we read from the file is technically now old because we have an updated balance. So what we need to do with that file is we're going to have to replace the data in that file. So we create, we're going to override the file data variable. So file data equals file data. So we're overwriting now dot replace replaces a function that pretty much looks for a string and then replaces it with whatever you want. So we want to look for the string called current balance, which is the old unupdated balance that it's going to be stored in the user file. And then we want to update it with updated balance. Now we've added str onto it because we want it to be converted to string before being saved as text because in text files, we're going to have an error free twice saving it as a number. Cool. So that's our file data overwritten with the most recent balance. And now we've got to just empty the previous file so that we can write the new data and the most updated data to it. So we need to do something called file dot seek and then file dot truncate. So truncate, g-r-u-n-c-a-t-e and zero because we want it to start from zero. And then file dot write the most recent and overwritten file data. So this is going to store and then finally file dot close. So that's going to store the most recent balance and that should do it. And then once it's done, we need to update the current balance label that we created, which was global with the most recent balance. So text equals current balance. We're going to use a colon dollar. I mean pound sign string plus str because it's a concatenation. We can't just add a number to it because the updated balance is in the format of a float. We need to change it to an str because we're concatenating and foreground equals green because it's not an error. So an update has just happened because current balance goes to green. And then it will show up with the most recent account balance after adding whatever we've just deposited to it. And then deposit notif dot config text equals balance updated. So previously we used this to display errors, but also going to be using it to show whether it's been a success or not. Green and that's about it. Let's go ahead and run our deposit function. I'll be surprised if there's no errors. Let's have a look. Let's add my name in there, Johann and password. So far so good. And deposit. So far so good. Cool. Let's enter a value of minus one. Finish. Negative currency is not accepted. As we added our validation, it's bang on. It says negative currency is not accepted. If I enter zero, it's going to say the same thing again because it's pretty much negative because we can't make a deposit of zero. If I enter point one and click on finish, we have an error as expected. Updated balance equals updated balance plus float amount dot get. Let's see. Oh, I forgot that. So on this line right here where we update the balance, this updated balance thingy right here is in string format because it's just been read from an array. So we need to make that a float as well. Forgot to do that. So we're going to have to do that so that it's an actual number and they can be added. Otherwise, Python just assumes we're trying to concatenate a string with a float, which is not what we're trying to do. That was a minor bug, which is sorted out now. Cool. Password. And let's make a deposit. Current balance is zero, which is perfect. Like a deposit of point one. Seek takes at least. Oh, I forgot to do seek zero as well because we wanted to seek from zero. My God. These are like the most minor mistakes ever. Oh, well, that's fine. Password login. Let's go ahead and deposit an amount of point one. Please don't give me any errors. Perfect. And as you see right here, we did a deposit of point one. And then when we said finish, balance has updated. Now if I do finish again, point two. Cool. Now let's do a bigger amount. Let's do 1,000 pounds, for example. Finish. And then it says balance updated and the balance is now 1,000 pounds and two pence. Cool. So if I finish again, it says 2,000 pounds and two pence. So pretty cool stuff. It's now updating whatever value we add in there. It's got validation too. So if I try adding zero, it will say no negative values. If I try adding minus one, no negative currency again. So you might argue whether this 2,000 pounds, point two pence is actually saved into the user file. And the answer is it is. So if I close this off and if I log in again, and if I have the same current balance of 2,000 pounds and two pence or whatever it was, as long as it's not zero, why did I type in login in there? So when I log in, if I check my personal details, it says 2,000 pounds and two pence, which was the last deposit we made. So it's perfectly working. Now it's stored in the file. So each time it's updated, it's storing in the file, which is what our code did. Now the withdrawal function is going to be pretty much just copying and pasting the deposit function, but we're going to replace the updated balance with a minus because we're going to be taking away from the current balance rather than adding to it. So this is going to be the easier bit. So what we're going to do is we're going to copy the entire bits for deposit. So from start till end. So copy the whole thing for deposit. And we're going to have to change a few things. Now we're going to go in a withdrawal function right here, paste it in there. And now when we paste that in there, we're going to have to change the variable name so that they don't conflict with each other. So first off, it's going to, we're going to add withdrawal to literally everything that we have. So I'm going to copy this withdrawal, I'm going to copy this withdrawal word right here so that I can quickly just paste it. And then everything should be sorted out. Now current account, current balance label can stay the same. Withdraw amounts and withdrawal notice will change. Now we've got to change the amount to withdraw amounts. There we go. The file stays the same, file data is the same, user details is the same, details balance is the same. Now this will change to withdrawal screen of course, because it's not the deposit screen anymore. Same here. And then we've got to change this placement for all the labels from deposit screen to withdrawal screen. It's a bit annoying to do, but it's fine. Now we have to change this to withdrawal screen as well. And that's fine. Now label needs to be on withdrawal screen. That's perfect. And now this deposit notice will change into withdrawal notice as well as changing the deposit screen to withdrawal screen. So if you copy the word withdrawal, and just we just got to keep pasting it in wherever it's required. Now entry is on deposit screen that changes off to withdrawal screen. And we also need to change the text variable to withdrawal amount. All right, cool, underscore. Then we also need to change this last button to withdrawal screen. And the command is going to be finish withdrawal. Perfect. So let's quickly create a function called finished withdrawal. Now we're literally doing the same thing that we did for the deposit. We're creating a little function for this to be run whenever the user clicks on finish, whenever they've entered the amount they want to withdraw and clicked on finish. Now we're going to call this finish withdrawal. And in here we're going to have to copy and paste the finish deposit because it's literally going to be the same. All that's going to change is we're going to add a minus in the updated balance thingy. Cool. So to finish withdrawal. All right. So in the finish withdrawal we have arrived in here. Let's paste it off. Now we still need to do the amount equals a speech marks. Then we need to produce an error, but we need to change this to withdrawal amount because that's the name that we had withdrawal amount. Let's copy this again. Let's copy it without the underscore actually. Withdraw. And now this deposit notif changes to withdrawal notif. Then we have the same file. Then we have file data, which is fine. Data is fine. Details is fine. Current balance is fine. Updated balance is okay. Update balance equals current balance. So right here before the balance could be updated, what we actually need to do is add a bit more validation in here because we need to actually be able to find out whether the amount that is being withdrawn is greater than the amount that already exists because we don't want the user to be able to go in overdraft or minus at the moment. So we need to prevent the user from withdrawing more money than they actually have. So we do if float withdrawal amount is okay .get brackets. Another bracket is greater than float current balance, which is the balance that's being read from the file. Then we do withdraw notif .config. Text equals insufficient funds because obviously there's not sufficient funds for this. If you guys want to enable overdraft, you can do that quite easily by changing the limits using which this message appears. foreground equals read because I want it to be an error and we use a return as well. Now after that the updated balance is fine. The file data is fine, file.seq is fine. Now we have to change the updated balance actually. All you need to change in here is change the plus sign to minus because we have withdrawing so we've taken away and the float updated balance stays the same but we need to change the amount to withdraw underscore amount.get because we're using the draw amount and not the amount from the deposit. Now in file data equals file.replace current balance stays the same man updated balance stays the same then we truncate then we write the new data that the file after removing the amount and then we close. In here the current balance label is okay that's perfectly fine but we need to change the deposit notif in to withdraw notif if I paste that in there and if I save and run okay so hopefully we don't have any errors might have jinxed myself but let's find out username and password is very secure withdraw okay so far no errors we have our current balance showing up completely fine so let's try going negative minus one and it says it floats amount okay okay I forgot to do that cool so in here if I forgot to type in withdraw amount withdraw amount okay there we go if withdraw amount that was my mistake and this deposit notif will change to withdraw notif as well cool so hopefully we don't have any errors now I have to password login withdraw minus one negative currency not accepted it's the same validation as before if I put zero we have the same message let's try and withdraw one pound and as you see right here current balance is one thousand one hundred and one thousand nine hundred and ninety nine point two pence which is what happens when you take away one I take away another one one thousand nine hundred and ninety eight now let's try and see if our validation works right let's take away one thousand nine hundred and ninety eight let's say we just bought a new car or something or paid off insurance now this amount is very viewed but it's like a decimal number which you can convert into having just two digits so 0.20 later if you like to you can research that now I'm going to try and go into minus so we have technically 20 pence in our accounts right now so if I go ahead and type in 30 it should prevent us from doing so finish and it says insufficient funds because I'm trying to take out 30 pounds and I'm pretty much short 29 pounds and 80 pence if I try to take out 20 pence still says insufficient funds let's try taking out 19 pence insufficient funds oh wait I'm trying my bad 0.20 if I try to take out 0.20 it gives me this weird weird value because obviously this is in like this is way um in the minus region after we've taken out so I'm going to do 0.1 insufficient funds because obviously this is this is a very low amount which is why we need to kind of round the number up so you guys can go ahead and do that you can round it up I'm going to quickly make a quick deposit in here just to fix this off and we have 100 pounds in here which is fine once you have rounded your number up this should this error shouldn't persist so you can use a round up function in Python which would be pretty easy to find so that's kind of like a task for you guys also we need to change this so if I go to withdrawal we need to change the actual title to withdrawal because otherwise it's a bit vague that we've copied and pasted withdrawal cool so the only issue there was that the float floats were showing us pretty much all the decimal points that could exist but you can pretty much round that up to the closest two decimals quite easily by looking it up on google or something like that so your task right now is two let's take a look your task is to allow the user to edit personal details and round up the updated balance to the nearest two decimals cool so that's your task guys if you manage to somehow complete the task just notify me in the comment section because that will make me feel real proud because I've helped you learn something and you were able to implement that to do something else and unfortunately that puts an end to the creating a custom banking GUI app using Python and take into hope you guys have thoroughly enjoyed this series and learned something new and would able to be used this information in the future also guys thank you so much for the support you guys have been showing the last tutorial was absolutely smashed by 100 views in a day so if you guys could keep sharing the same way it would be amazing if you guys would also like to support the channel directory I mean directly you can do so by either purchasing one of the custom emojis or buying a highlighted message I'm not forcing anyone to do so but if you like to you're more than feel more than free to do so it really helps me out also guys we also have a little discord channel going on right now with about 20 plus members which is really fun we pretty much just chill in there and like discuss stuff about programming and other future ideas that could be useful so I'm going to link the discord and other links to my socials in the description along with other helpful tutorials and the part one and part two of this if you guys are interested go ahead and join them or watch them and guys thank you as always I will see your beautiful faces in the next tutorial peace out