Python Unit Converter GUI Application With Tkinter Part 2 | Commands And Entry Validation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in part one we use tkinta to create the graphic user interface of this python unit converter GUI application this is part two and we're gonna add all the commands and validation to start using it by the way before you get started if you're here I'm sure you want to become a programmer and yes here on YouTube you can learn a lot but one of the best ways to actually improve especially if you want to turn this into Korea is to take some courses made by experts in the field usually these courses are structured in a way that makes it easier for you to learn properly and I've selected for you some of the best courses and learning platforms you'll find the links in the description down below and by using those links you're gonna help this channel as well so this is what we've done in part one so we've created the graphic user interface like this but of course nothing works you can enter whatever and nothing works of course so in this video we're going to do the validation and also the whole thing so first of all we want to validate the entries so that the user can only enter numbers and all that is another characters so let's define the function to do this validation so we're going to go up here and I'm going to create the function so value validation I'm going to pass the value and you see what this value is in a second as you can see this is not a method of the class this is just a simple function inside of the create widgets method so let's pass for now like that so to be able to use this function to validate the entry we need to register it and save the result to a variable and then we can use this result in the validate command of the entry let's actually see what I mean so first of all let's register the function so value validation command just going to call it like that container values register I'm just using the container to register it like that and then here as you can see I'm still using a local variable I'm not using self but just a local variable and of course I need to go down here now and write something like validate he which means that whenever the user enters something the validation will take place and then we're going to set the functions run by using the validate command like that this is a tuple the first value is the validate validation command which is this one okay which is what is returned by the register method and then here P like this I'm not going a lot into what p is etc etc but basically each time the user tries to enter a character okay in the entry the value with the new character appended and the end is passed to a validation function and that's why we added the value parameter to the value of a date function so we actually added this thing here because this is passed automatically by the validate command here so let's actually see an example so let's say that you already have one two in the entry and you press a the entry in the entry you still see one two and this value here is the value that you would have if you allowed the change basically so this is the value that is going to be passed to this function here so value in this case is going to be 1 to a which is basically represented by the P down here okay so as I said the entry is still 12 and this is the value that the answer would have if allowed the change as I said in this case 1 2A is not allowed because this is not a number so in the value validation in this case you need to return false okay so you just return Falls like this by returning false the a is ignored and the entry will still be 12 okay so nothing changes basically but now let's say that you have one two and then you try to enter four for example okay and this is passed to this function again as the value in this case you need to return it true because 1 to 4 is a number and it's allowed in this case we need to return true as you've returned through then before is actually written in the entry and the entry now has the value one two four I hope that makes sense okay I hope that makes sense so let's delete this so now that you know what the p is etc etc we actually need to write something in the validate function in the value validation function because otherwise we don't validate anything so the first thing that we need to do is if not value return true and we need to do this because otherwise an empty value won't be valid and you won't be able to delete all the characters in the entry so if you don't do this and you have let's say one two you delete the two and you have one if you try to delete the one you would end up with an empty string and if the answer string is not valid you wouldn't be able to actually delete the one by the way if you're liking the video and you want to help me keep creating content like this you can support me and also get exclusive perks on patreon link in description if you want to check that out and thank you so much for this report then we need to try and see if we are able to actually convert the value to a float if we're not we'll get the value error and we return false because in this case if you are not able to actually convert the number that means that you have some characters that prevent this so basically going back to the example there will be one two a that will throw an error because one to a is not convertible to float and you return false and you don't allow the the change basically if you don't get any error then you need to return through because you want to allow the change so probably from the future here I was actually editing the video and I found this little bug because I was checking if everything was working as expected and something is not working as expected so I just wanted to add this little update so that your program is going to work as expected so basically this is something to do with the minus because when it comes to temperature you can have negative numbers like minus five minus 10 Etc so in this case when you try to enter the minus for example as you can see here this wouldn't be convertible to a float so the function will return false as you can see here and of course course this wouldn't be allowed and you wouldn't be able to add the minus so you would have to add D5 like that and then you would have to go like that and add the miners later because the 5 is convertible and the minus five the whole thing here is convertible but V when you start with the miners like that this wouldn't be convertible so this is not possible basically and the same thing applies when you try to delete so you've got V minus five you try to delete the five you would end up just with the minus and this wouldn't be possible so how can we actually fix that this is quite easy we just need to add this little thing here so instead of just if not value or value is equal to the minus so when the value is just an empty string or the value is just the minus you return true and that's it you can actually enter it and that's fine really important later on we're gonna have to change a little thing even in the convert value function but don't worry I'm gonna add a little video there as well I didn't want to actually read do the whole thing the whole video just for this little mistake because I actually wanted to show you that everyone makes mistakes and you can check and check and check and check but there is always going to be a mistake or a bug Etc don't worry about it just fix it and that's it because I see a lot of beginners worrying that oh my gosh I keep making mistakes etc etc this is normal so don't worry about it so now that this is fixed let's go back to the original video now we can actually try and see what we get so let's actually search round one two let's delete everything I want to to show you what would happen if I didn't have this one so as you can see now I have one two I delete the two and then as you can see I cannot delete it so this is really really important and also let's say I want to add the a as you can see I'm actually pressing the a but it doesn't work just a number so this is a validation so let's now add the functionality otherwise the little up doesn't do anything so first of all we need to connect a string bar to the converted value entry so that we can actually set it and show the result to the user so let's go down here converted value entry this one here and up here we're going to do something like self Dot value and let's actually change the name here and call it entry converted and down here and three converted value so that here we can use converted value for the actual value so TK stringva like this and then of course we need to use it down here and do something like text variable self converted value like this so This value here is going to be the text variable here and by doing this every time we set a new value to The Strain bar it'll be shown in the entry automatically even though it's disabled so let's now create a function with all the conversions so let's now add the method to convert the value so this is going to be here because this is actually a class method convert value self and event like that perfect we actually need the event here because it's automatically passed by TK inter so you need it there even if you don't use it because otherwise you would get an error now we need to get the conversion from the option menu so conversion it's going to be self conversion get because this is a string bar so you need to use get to get the value and if not value to convert get like that and now if we deleted all the values in the entry we would have an empty string so we actually want to set the string bar to an empty string so nothing will be shown as the result on the answer on the right so so converted value set an empty string and then we can exit the method without converting anything like that and now we can convert the string in the entry to a float and I'm sure it's convertible because I've already validated it so I validate it in the validation function before so I'm sure that this is convertible so value [Music] convert local float value to convert get like this so value from the future again as I said in the little update during the validation where we actually added the minus we need to change a few things here as well in the convert value function because here I was assuming that the value to convert was always convertible to float but in this case since we added V minus V minus alone is not convertible to float and this would throw an error so basically we just need to add something like this so up here we need to add something like self value to convert get is equal to V minus so basically when the value to convert is equal to an empty string or D minus you just set the converted value so the entry on the right to an empty string and you return so this is not run and you're not going to get the error basically so you just need to add that little thing and you're good to go so back to the original video and once we have the value and the conversion to do we can find the converted value here I'm going to use the match statement this is the most basic use case but I've actually made a complete guide about all the things you can do with the match statement and there are quite a lot I highly recommend you go and watch it after this one to learn more about it I'll leave the link in the description box down below and in the cards up above if you want to do so so much conversion is going to be case miles that to sell converted value set is going to be an F string and it's going to be a value to convert local times 0.62 then I'm going to format it like that these are of course the values in the option menu there we actually added to the to the list of conversions at the beginning of part one then I'm going to actually copy and paste this to know how many like that I think this is fine so this is going to be mild kilometers there kilograms pounds pounds coupons use and also F like that and of course we need to change those otherwise this is not going to work so 61 this is going to be 2.2 two like that then here like this then this is a little bit more complex so I'm going to do something like like this minus 32 divided by 1.8 and then here is going to be times 1.8 plus 32. I hope that everything is fine but I think so and also of course we need to make sure the function is called when we enter something okay so of course we need to actually up here by to convert grid two self value to convert bind I'm going to bind the key press so key press self convert value like this so when the user presses a key to enter something in the entry on the left the convert value function is going to be called but there is actually a huge problem this method is called before the validation takes place and before the value is actually assigned to the entry so when the method is called the entry value has not been updated yet so we're basically always one step behind so let's print something in the convert Value method to help you understand so let's go down here convert value down here we're going to add something like print self value to convert get and then also print after that let's also add something to the validation to show you that it's run after so let's actually go up to the validation okay here so value validation and I'm going to actually print just print so let's actually see what we've got so let's say that we press one as you can see here you don't get validation one Etc you get an empty string and then the validation why because the convert Value method is called before the validation takes place and also before the actual value 1 is written in the entry so basically this is one step behind and as you can see if you press 2 you get one because the convert value function is called and then the value is updated so this is the old value and then the validation takes place and then the two is added to the entry okay but then when you press three for example then the convert value is called before the three is added to the entry so you get one two and not one two three and then the validation and then you'll have the updated value in the entrance Etc so you basically when you call convert value you are calling convert value with the old value of the entry and not the new value of the entry if that makes sense and as you can see here you've got not the same because 123 kilometers is not 7.44 miles and if you actually delete three for example you get the value of the 123 et cetera Etc so everything is one step behind as you can see it doesn't actually work as expected by the way if you're finding this video helpful a like would be really really appreciated also if you want to become a better programmer subscribe to the channel as well I'll be posting a lot of really interesting videos and you don't want to miss them so we need to find a way to call the convert function after the value has been assigned to the entry and after the validation basically so we actually get the updated value and all the old value and it actually came up with a solution so instead of actually calling the convert value directly I'm going to create a new method that just calls my convert Value method after a delay so that it's run after all the validation takes place and after the value of the entry has been updated when you call the converter you method the entry has the new value and not the old one so I'm going to do something like cool convert value delay something like that self of course as you can see I've added the event here because this is this will be the method that will be bound to the key press event and we actually need to remove this from here like this we don't need that anymore and then we are going to do some DeLay So Mainframe after 100 self convert value like this so basically this tells tkinta to run the function to run this function after a delay of 100 milliseconds and this little delay makes it so that the self-convert Value method is called after all the validation etc etc and by doing so you get the right value and not the old one okay I hope that makes sense and of course now you need to bind this here to the key press so we're going to go up here key press event I'm going to bind the Google convert value delay and let's see how this works so let's see if that works as expected now so one two three four as you can see now that works as expected and here you can see that you get the validation and then the number the validation and then the number the validation then the number so that little delay is all that's needed to actually make this work and also there is another little problem so if you've got a value already here and you change the value like that you don't see anything here you don't see the change as you can see this stays the same until you actually enter something and then you see the change as you can see you need to enter something to actually see the change and this is not what we want we want to be able to change here and see here the right result according to the right conversion and we're going to do this by tracing the conversion string bar but before we can actually we can actually delete the validation here and also down here this thing here we don't need that anymore so let's actually go up here down here like that so Jeff call back call back Trace conversion hugs like that we need to use args like this because we want to capture all the arguments that are passed by Trace but we're not actually going to use them but we need to do that otherwise we will get an error and then here we can actually call the convert value again so self convert value like that and here as you can see we don't need the delay because all the variables already have the values from the last conversion so the entry has already a value in there etc etc so we don't need the delay and we just can write the convert value like this and of course we need to use Trace so self conversion which is the variable the string bar here Trace w callback Trace conversion like that and w means that we are tracing all the changes made to the string bar so basically whenever we change the value of the conversion in the option menu and here we have the Callback to call when you see a change so let's actually try to run that so now let's say that we have already 12 if we try to change here as you can see this changes accordingly foreign like that because we're tracing so whenever we do something like this and we change the value the trace function sees that and calls this callback and you call the convert value as you already have the value in here you don't need the delay and you see the result that okay so now you have a fully functional converter and this is incredibly incredibly cool so now on the screen you can see another really interesting video for you to watch and learn from don't forget to check out my patreon and also like the video and subscribe to the channel as well for more videos like this and I'll see you soon bye
Info
Channel: Fabio Musanni - Programming Channel
Views: 330
Rating: undefined out of 5
Keywords: python, python3, programming, coding, learn python, python tutorial, python for beginners, python tkinter, python tkinter gui tutorial, python tkinter gui, python unit converter with tkinter, python gui app unit converter tkinter, create a python unit converter using tkinter, tkinter gui app with python, tkinter graphic user interface, unit conversion with python, unit conversion in python, tkinter entry validation, validate entry in tkinter python, tkinter commands python
Id: w2f-GVdyIOA
Channel Id: undefined
Length: 22min 31sec (1351 seconds)
Published: Wed Sep 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.