How to Validate an Entry Widget as an Integer - Python Tkinter GUI Tutorial #67

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from khou-tv common in this video I'm going to show you how to figure out whether or not something's an integer with Kenter and Python alright guys like I said in this video we're gonna figure out whether an entry box entry is an integer or not but before we get started if you liked this video I want to see more like if you sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm and check out Kona me.com where I've dozens of courses with hundreds of videos that teach you to code use coupon code youtube one to get $30 off membership that's all my courses videos and books for one-time fee it just $49 which is insanely cheap all right I've been getting this question a lot lately I think since the last time we did our flash card app and that is how do we determine whether somebody types a number or not into an entry box with Kenter right so think back to our flash card ad what's three plus two five what if they typed in FIV e instead of the number five you know that could throw off the the app quite a bit so how do we determine whether or not something we type into a box is an integer or a string so that's what we're gonna look at in this video and it's pretty simple it shouldn't take us very long to do so let's just create I've got our basic starter code here we're gonna blaim and the get bash terminal as always we've got an icon I'm just gonna call this code me calm and it is 400 by 400 in size and we've got our main loop so let's create a label let's just call it my label and that's gonna be a label and we want to put this in route and want the text to say enter a number all right and then we want my label let's pack this to the screen so that we can see it and let's give this a pad Y of 20 to kind of smoosh it down a little bit now let's create an entry box and I'm just gonna call this my box because I'm very creative and this is an entry widget and we want to put it in route and then we also want a my box pack this guy to the screen and let's give him a pad why of I don't know 10 or something - okay so underneath this we want a button and we're gonna creatively call it my button and it's a button and it's in route and we want the text to say enter a number and we want to give this a command let's just call it number okay so I'll copy this and we also want to put this guy on the screen so let's dot hack and give this a paddle Y of like I don't know five or something okay so then up here let's create the function number and we'll do something with that in just a minute then down here finally let's put an output answer so let's just call this answer ans wer and that's gonna be a label and we want to put it in root and we want the text to equal just nothing right now now let's pack this guy to the screen as well and give it a pad Y of 20 or so okay let's give this function a pass just for now okay so let's save this and run it just to see what we've got here and I called this is numpy so we've got this inner number we've got a box we click this button button nothing happens yet okay so now we want to figure out whether or not the thing we type into the entry box here is a number or not and there's a bunch of different ways you can do it but I think the best way is to just do a try and accept block so what happens when you enter something into an entry box is it hinter just assumes it's a string and even if you type a number in it thinks it's a string so you have to convert that to an integer anyway right so even if you use an int var as we've done in the past it's still thinks of it as a string which is kind of weird so instead of running and is num function on that to see if it's an integer or not we can just run a try loop here we can go try and let's say let's just go my underscore box get but let's just wrap this entire thing in an INT function and this will convert it to an integer if it can right so if you type a word in there or a letter it can't convert that into an integer and if it tries it'll throw an error so for instance if we don't do this try loop and we just for instance want to print this to the screen or something or even just run it we can save this and run this guy and if we if we type in you know some letters and click the button and then exit out we see we get an error because it's trying to convert that into an integer but it's it's clearly text it can't convert it into an integer so it throws an error so we can use that right since we know it'll throw an error if it tries unsuccessfully we can give this like I said a try block here so if we could say convert it to this and if it doesn't work out we can call an accept value error and then do something else so what do we want to do here if we can convert it well let's just put a little message on the screen that says hey yes that was a number good job right otherwise we'll put a different message out and so remember we have this answer label so we can just go answer dot config and give this a text equals that is a number and congrats right otherwise we can come down here and say that is not a number you silly person okay so now if we save this and let's come over here and run it close screen here so we say enter a number we can say 12 that is a number of congrats if we type in something else and click the button and that is not a number you silly person if we type in John elder that is not a number if we type in 122 and that is a number if we type in 5 that is not a number right so just that easy so that is how as one way at least that you can determine whether or not something is typed into the entry box correctly and you know you can then from here do anything you want if you want to then you know run some other function you could just call it you know like whatever function and run it like that or just right here you could just write some code to do whatever you want to do etc so pretty simple and pretty straightforward now there are probably dozens of other ways you could do this right to me this just seems the easiest you know a couple lines of code try except boom escape so like I said Python has some built-in things to test whether or not an integer is an integer or a string as a string or a float as a float etc but they don't work well with kinter for entry boxes for some reason so to my mind this is the best way now keep also keep in mind this is for an integer right specifically now if we run this again and we type in 1995 and click the button that is not a number because we're testing for integer and an integer as a whole number five seven it is not a decimal or a float if you want to do float you have to specifically call a float right so we could if we wanted to now we could just change this to float right then if we did this and tried again in 1995 that is a number Congrats and again if we typed in letters that is not a number but also if we typed in two it still works so that is a number right so floats consider our integers floats as well because it characterizes it as 2.0 right so I sort of keep that in mind is sort of an interesting thing but depending on your program and what you need if you need an INT you would convert it to an int if you need a float you would convert it to a float you can do both right you can do nested in here and other things like that but basically this is the the way you would go about doing it or one of the ways and at least in my mind one of the easier ways so that's all for this video if you liked it be sure to smash the like button below subscribe to that channel give me a thumbs up for the YouTube algorithm and check out Co to me calm or you can use coupon code youtube one to get $30 off membership they paid just $49 to access all my courses hundreds of videos and the PDFs of all my best-selling coding books join over a hundred thousand students learn to code just like you my name is John older from coda be calm and we'll see in the next video
Info
Channel: Codemy.com
Views: 66,379
Rating: undefined out of 5
Keywords: how to validate entry widgets in python tkinter, how to create numbers only entry widget in python, validation in entry widget in tkinter | python, validate an entry widgets with numeric data in python, how to validate an entry widgets with numeric data in python, how to validate an entry widgets with numeric values in python, tkinter try except, tkinter integer, tkinter integer entry box, python tkinter integer enty box, tkinter entry widget, entry widget
Id: IbpInH4q4Sg
Channel Id: undefined
Length: 9min 17sec (557 seconds)
Published: Mon May 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.