Random Winner Generator - Python Tkinter GUI Tutorial #39

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from Cody we common in this video we're gonna write a program to pick the winner for my birthday contest using Python and kinder I guess like I said in this video we're gonna write a little script to pick the winner for yesterday's contest but before we get started if you like this video want to see more like it be sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm and check out Cody be calm where I have 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 a one-time fee it just $49 which is insanely cheap okay so yesterday if you've been watching the channel you saw the video where it's my birthday coming up tomorrow Friday and I'm giving away free code me membership $49 value and uh to be entered in the the contest thing all you had to do is comment below the video use the word Aspen somewhere in your comment and you get entered into the contest so in this video we're gonna write a little program that automatically randomly picks the winner we're not gonna leave this to chance we're gonna do some coding and learn some stuff might as well be fun so it shouldn't take us very long but it should be fun so we're gonna use kinter and python for this kinter is the graphical user interface that comes with python that allows you to create GUI desktop apps if you're not familiar with that check my playlist i've got a whole like 40 video playlist on kinder and I'm kind of continuously adding to it so that's cool so what I did was I just went back to yesterday's video and I just looked down and copied every single person that commented into a Python list and I've named this entries right so you can see there is a bunch of entries right so if you haven't entered by now too late this is it we're gonna pick the winner now so this is a standard kinter start up you know bit of script we import kinder from kent your import everything we create a route window we name a TK it's an instance of TK we create a title for it code me learn to code put a little icon on there and the size is 400 by 400 and then to run it we just run route main loop so if i save this and i'm saving this as winners pie in my GUI directory so i can just come over and run this and when I do I just get this Windows box with nothing in it but that's good so now we just need to write our program so let's start out let's create some labels a label for this thing and let's call the first one top label and I'm not gonna explain all this if you've been watching the kinter videos you already know otherwise you can go back and watch those to learn exactly what I'm doing I'm just gonna knock this out otherwise it would take all day so we want to create a label you want it in our root window and we want the text to equal what when o rama right and let's put the font as let's make it a little bit bigger so let's go Helvetica right and I want that to be size 24 so now we put this on the screen I'm just gonna pack it and let's give this a pad Y of 20 okay so now underneath this we want a button I want to be able to click the button and get a winner so let's create a button let's call it the win button okay and this is gonna be a button and we want it in root and we want the text to equal but pick our winner right and again let's give this a font and I'm just gonna copy this thing here and we also want to give this a command and let's call it pick we're gonna create a function called pick all right so let's put this on the screen win button dot grid or dot pack let's just pack it on the screen and let's also give this a pad Y of 20 so then up here we need to create a function called pick and I'm just gonna turn it for now all right so let's save this and we called this function pick because over here that's the command we we said we want to run we want to run this function whenever somebody clicks that button so alright let's save this and run it real quick and just see if that looks good all right oh yeah that looks really good so we know Rama pick our winner and nothing happens now so okay but it's looking okay and we don't really care how this looks we just are more interested in the functionality all right so that's fun so all right so I've got all my entries here I'm just gonna copy this and let's put the entries in our pick function right so now before we do random stuff I want to think about this list I created I just went through and copied all the names that of people that comment it and slap them into this list right and the reason why I did that is because we can generate a random number and say we generate number 42 right well we can then just call entries forty-two and whoever that is wins right so instead of 42 we could just you know type in our random variable that we create and that'll work so that's an easy way to do it so that's why I did that but I'm thinking to myself you know the people might have commented twice there's like I don't know almost 90 people commented it's very likely that people commented more than once and I didn't notice and I just copied and pasted copied and pasted so we really need to check to make sure that this is unique and that people didn't enter more than once right that seems fair to me I think so I'm trying to think of how to do this and we can convert this into a Python set and a set is a unique list right so it will go through there and and take out all the duplicates for us and then we can convert it back to a list from a set and we'll have unique stuff so I think that's probably a good idea before we do that though let's come down here well let's we can do it right here let's create a winner's label and that's gonna be a label and it's going to be enroute and we want the text to equal something and we want a winner's label dot hack and I'm probably one agree or I probably want to pad why this 20 also or a heck let's make it 50 and let's also put a social change the font I'll put this at 18 right so the text here let's see how many entries we have just right now so we can do we can run a Len function and inside of that we can just pass entries so let's save this and run it just to 6 i'm curious how exactly how many people signed up here so it looks like 89 of you signed up maybe but now we really want to make sure that there's no duplicates so so remember 89 well let me just make a comment 89 injuries so now let's really quickly let's convert to a set and it's really easy to do that we can just go we could just call this entries underscore set and we can set that equal to set and then we can just pass in our guy here so set entries right so now we want to convert it back to a list so to do that we just take variable I'm gonna call a unique underscore entries and we set that equal to list and then just whatever we called this thing all right so let's paste that in there okay so now instead of running a length on entries let's run a Len on this to see if these are the same so it was 89 the first time if there were no duplicates it should be 89 now if there were it'll be something else so let's just run this and see I'm curious this should be fun oh okay pick our winner Oh 85 so for have you tried to cheat ah all right well that's fun all right so let's come up here revert back to list we say 84 85 what was it let's run it again yeah 85 so all right so we have 85 total total entries so that's cool all right so now we need to pick a winner and to do this we need to create a random number now Python has random number stuff we can use we just have to import it into our program so from random let's import Rand int and this will allow us to pick random integers right and it's really easy to create random integers and in fact let's just come down here and let's let's create a random number between 0 and 84 right because remember lists start at 0 so this guy is the 0 with item so we need our and room number to be 0 or up to 84 now yes there are 85 entries but there are numbers only go up to 84 because we start at 0 so 0 1 2 3 4 5 84 that's 85 people right so from 0 to 84 is 85 people so we need a random number between 0 and 84 right basic Python so ok let's create a variable I'm just going to call it Rando and we need Rand int and we can say we want this between zero and what well 84 we know it's 84 but let's let's create create our list size variable so we can go unique entries so let's just call this our numb [Music] our number and it's going to be unique entries well it's going to be the Len of unique enters entries minus one right so this will give us 84 but if there was more than 84 and we didn't know right if there was a million and we didn't want to count them all or whatever or run the Len function together to get this number it would just be the length of the the list minus one all right so there's 85 entries minus 1 that gives us a number between 0 and 84 so you can just take this number now and instead of passing 84 we would we could do it like this probably a better way to do it okay so this will create a a number between 1 and 84 and assign it to this Rando variable right so now to get our winner all we have to do is call unique entries and then pass in Rando I think that seems right right I think so okay so let's just go through this again we've got our button down here when we click the pic but when we click the button the pick command gets run which is this pick function and we take our entries which is a Python list and we convert it into a set so that all the duplicates get removed and then we convert that set back to a list so that we can grab out a number sets don't have index numbers so we just can't call like the forty-second index number of a set because they don't have numbers but lists do so we need to convert it back to a list then we create a variable that has the numbers that we want to create our random number from and that's just going to be the unique entries list minus 1 so there are 85 entries 85 minus 1 is 84 so 84 so let's just see this is the 84 of 84 item in our list this is the zeroeth item so 0 plus 84 equals 85 things which is the number that we have in there hope that makes sense so we're gonna create a random number between 0 and our number which is 84 this will include also the number so it will include 0 and 84 so both 0 and 84 could show up and any number in between just randomly and then finally we want to print out on to the screen the the name of this random number in this unique entry list alright so I think the only thing to do is save this and run it it's very exciting it's the first time we've done something like this on the channel alright so is everybody ready we know Rama time to pick our winner so we have a countdown 5 4 3 2 1 spider spider wins congratulations spider get in contact with me you can go let's see so you can go to Kota me.com and click on contact fill out this form this will send an email straight to me with your contact information so I can set up your account or you know contact me through YouTube is there a messaging system through YouTube I'm not even sure comment below the video if you want so that was a lot of fun let's run this again let's pick another winner why not two winners today that's what I say hopefully spider won't win twice right so all right here we go pick another winner and the winner is a Christian Smoke Oh Christian's simogo so you win two we have two winners it's very exciting and you know what let's do it again that was fun so first let me write these down here so we have spider we have Christian Simcoe I'm mispronouncing your name I'm very very sorry let's do it again three times third time's the charm I always make three mistakes in these videos so we might as well have three winners boom banaji Andy woohoo congratulations and I mean I could do this all day that was fun we're gonna stop at three though I think that's pretty good so you know if you did not win you can also take advantage of just my normal discount when you click here memberships usually 79 bucks but if you use coupon code let's see YouTube one comes down to $49 but guess what I'm feeling even crazier today let's create another coupon code which is gonna be my b-day my bday my birthday it doesn't exist yet but right as soon as I'm finished done with this video I will create this coupon code and it will give you membership for just $27 $27 just today only next 24 hours my bday right so anybody wants to take advantage of that they can if you want just one of my courses at udemy can go to John elder calm and click on my you to me courses for the next day or so these will be $9.99 cons at udemy so all my courses here you can take individually one at a time for you know ten bucks each but this coupon only lasts for a day or two and then it will expire so be sure to take advantage of that if you're interested otherwise use this my bday and get everything for $27 and you know if you're not familiar code Aamir Khan this is my website has all my courses over 40 courses you can come to the main page here and read through all of these you can click on any of them to get more information on each one right see what's in them what's what's going on you get access to me you can ask questions you get access to all the other students you can interact you get copies PDF copies of all my best-selling coding books I have several number one best-selling coding books from Amazon that I've written over the years you get the PDF versions of all of those and then it's just a really cool place and I hope to see you there so that's all for this video if you liked it be sure to smash the like button below scribe to the channel give me a thumbs up for the YouTube algorithm and for my birthday and check out co2 be calm or you can use coupon code my bday to get a bunch off $50 off something like that 79 minus 27 however much that is so you pay just $27 today only taxes all my courses hundreds of videos in the PDFs of along best-selling coding books join over 85,000 students learning to code just like you my name is John elder from coding me calm and we'll see in the next video
Info
Channel: Codemy.com
Views: 9,399
Rating: undefined out of 5
Keywords: Python Random, python random number generator, Python Random Number, Python List, python lists, python list length, python len, python sets, python set, python list to set, python convert list to set, python convert set to list, python unique set, tkinter, tkinter gui, python tkinter, python tkinter gui, python gui, python gui random number generator
Id: f7HE2S4opdY
Channel Id: undefined
Length: 17min 17sec (1037 seconds)
Published: Thu Feb 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.