Building Out The GUI for our Database App - Python Tkinter GUI Tutorial #20

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from Cody my calm and in this video I want to continue building out our database app with kinter and Python or in the last video we created the database connected to it created a table in this video we want to start to build out the graphical user interface the things that allow us to type in entries and save them into the database and all that good stuff but before we get started if you like this video I want to see more like it be sure to smash the like button below subscribe to the channel and check out coding me comm where I have dozens of courses with hundreds of videos that teach you to code use coupon code youtube to get $22 off membership that's all my courses videos and books for a one-time fee just $27 which is insanely cheap ok so we got our database top PI file from the last video we've connected to our database created the database we have our cursor we've created our table now we can comment out the table because we don't want to recreate the table every time we run the program we just need to do it one time so now we can add stuff to the table and that's what we want to look at in this video so the first thing we want to do is create some text boxes that we can type information into so we want one for first name last name address city state and zip code and we're going to use entry boxes entry widgets with tkinter kinter we've done this in previous videos so you should remember how to do it if not take a look back at the older videos so I'm gonna create one called first name and let's call it's an entry widget and we want it in root and we want the width to be what's a 30-ish it's probably good and we need to now put this on the screen so f underscore name dot grid and we don't want to pack this because we're gonna have a whole bunch of stuff here so we don't want to really just pack everything we want to have a little bit more sort of options to place them exactly where we want so of course we're gonna use the grid system so we want this to be in row equals 0 and column equals 0 and for this one well first let me just copy this and then for the first one here I'm also gonna put excuse me a pad X of say 20 just to Pat it a little bit so now we need one for last name address city state and zip so I'm just gonna copy and paste so last name address city state zip and then just come through here and change these to L name this one will change to address and address this is very glamorous work here a city city and state and state and finally zip code and zip code okay so we probably want them in column 1 because we need to put text next to them a little label to describe which you know boxes which so I should probably change this to one for each of these now that I think about it it's a good thing to think these things through first I should have really done all right so the first one is in row 0 the next one we want on the next line down so that's Row 1 Row 2 Row 3 4 5 and that looks like it so all right again these all back together here and let's make a little comment let's say create text boxes alright so like I said we also want to create text box labels and let's see let's just go let's call them first name underscore label and that's of course gonna be a label and we want it in root and we want the text to equal first name and that should work now we want a first underscore name underscore label dot grid and we want this to be row equals zero and column equals zero okay so same deal we just need one now for last name address city state zip and just come through here and change these again so address label and address label city label city label state label very exciting state label as zip code label zip code label and again we want Row one two three four and five and up here we just need to change these two last and then again address bear with me city the joys of kinder state and finally zip code all right other I just put line spaces just so it's a little easier to make sense of it as we're typing them okay so now we also need buttons right so let's create we want to create a submit button okay okay so let's call this submit underscore button and that's gonna be a button it's gonna be in route and the text is gonna be what add record to database works and we want this to have a command and let's just call this what submit good alright so we want submit button dot grid and where do we want this we want this in row equals six because underneath there and column equals probably zero right and when probably want this to span across both columns so let's go column span equals two and what else do we want I probably want to put some padding so a pad of y equals ten let's say and that'll put it down a little and I want pad X to equal probably another ten that'll put it over a little bit and I want I pad X to equal 100 so we want to stretch it out a little bit okay so I think that will work now we need to create this submit function so let's kind of head up here somewhere up here this looks good let's go create submit function database yes so define submit and let's just return it for now actually let's clear the text boxes so every time we enter stuff into the text boxes and click the button to submit it into the database we want those text boxes then to clear so then we can afterwards type in another record if we want we don't want that information to just be sitting there so we can do that with F underscore name call the actual name of the entry widget dot delete and then give it a zero and an end we just do this for each one first name last name address city state zip and one more time fun work of typing these and over and over address city state and finally zip code all right so let's save this and run it pull up our thing here and we want D to be stopped I pull it over and now it's looking pretty good so now if we type some stuff in here and then click this button boom it disappears all right so far so good not bad so now what do we want to do we need to actually create some code to submit the stuff that we type into the database itself so the first thing that's kind of weird is you gotta connect to your database and create a cursor inside of your function when you use functions I'm not really sure why that is but it is the case so make sure these tabbed over and let's head down to the bottom here and grab these two lines we want to commit our stuff and close our connection we need to do that inside of our function as well have those over okay so now how do we actually submit the stuff from our form right okay so let's go insert into table right and just like before when we had our see at execute to add to create a table we're gonna again see that execute and we almost always execute our cursor it's just what we do so we're gonna use sequel now to insert into and we name the table that we want address says right and the values of something right so for we use this doctype string thing doc string type whatever it's called this time I want to use placeholder variables just a different way to do it and you do that by just creating sort of dummy variables and you start with a colon each one starts with a colon so I'm going to name these the same as our text boxes you can name of anything you want but it makes sense to name them what they're gonna be so first name underscore last name we need a colon separate them each with commas and then call an address comma colon city appear at ecole a comma colon state comma colon zip code okay now at the end of this we need to there we go stick a comma and then come down now inside of here we need to create a almost at Ruby dictionary a Python dictionary that's just this and Python dictionaries have key value pairs and the key will be this dummy variable and the value will be whatever's in our text box right so that's how that works pretty simple so let's go F underscore a name and you separate them with colons and then remember inter variables we can get them and set them well in tree widgets are like inter variables we can get or set them so that's what we're going to do so let's go F underscore name dot get and then comma and then just on the next line we just go through and do one for each of these last name colon L underscore name dot get it's very exciting work here address colon address dot yet and then what city colon city dot get and state : state get and then finally zip code : zip code dot get and this is the last one so we don't have to put a comma and remember these things are these so if you named these up here ABCDEF this column here would be ABCDE F we just happen to name them the same as we named our widgets or entry widgets just because that kind of makes sense okay so now we can kind of do some stuff here to make this look better okay so if we save this and we can run this just to make sure it worked to be able to see if it worked yet because we haven't created any functionality to output the stuff from the database onto the screen but at least we can see if we get an error so let's go John elder 10 West Elm used to live on Tim West Elm in Chicago years and years ago live in Vegas now but he's living very cold Chicago downtown pretty cool so add record boom it disappeared and if we end it we didn't get any errors up here so we can assume that it added the thing in and we're good to go okay so now we need to create a button to actually pull whatever's in the database out and then put it on the screen so we can see whether or not this first part worked yet so let's go down here and let's just create a query button and we want to call this what query underscore button and this is a button I want it in root and we want the text to equal what show records maybe I think it's probably good and we want to give this a command of what let's call this query create a query function and we want to query underscore a button dot grid this thing and so we're in row what seven now column equals zero and again we want to column span this guy to two and let's go pad y equals 10 and had x equals 10 and let's stretch this with iPad x equals 137 which I happen to know this is a very nice size so if we save this real quick and just look at this we can see you quarry's not defined we got to create that function real quick so what do we call it query let's head up here and let's say underneath our submit guy submit function let's go create query function and let's go define query you ER why let's just return for now alright save this so that run this again boom yeah what today oh I misspelled column span I could do that sometimes I call them spanned oh that's a ugly typo alright so save this run it again third time's the charm and boom so that button looks pretty good-sized it's compared to the other one and of course it doesn't do anything yet so we can close this or the screen so we're iing the database we haven't really talked about this yet but it's it's fairly simple again let's come up to our submit guy and let's copy we still need to connect to the database again and we still need to commit and close grab that there we go okay so to query the database it's pretty simple we just again use our cursor and execute like always so it's that seed execute and it's a function so inside of here we want to run some sequel a sequel command to execute our sequel command and the command we want is select so we want to select and what do we want to select we want to select everything so the star means everything and we want to select it from our ad dresses table right so one more thing we want to do is in most databases you have to designate and create a primary key for each record and a primary key is a unique record a unique number so you know every single entry you make it's a unique sort of ID number and in sequel 8 3 it creates it for you so we don't even need to do that ourselves which is really really cool but it kind of ignores it since it creates it for you it ignores it unless you specifically tell it to print that number and we kind of want to see what that number is so we want to select everything and the OID a no idea I'm not sure what that stands for I don't even remember at this point original ID maybe I don't know but it's the primary key right so primary keys are useful for a lot of different things specifically later on if you want to delete a record you don't want to delete John elder because there may be 20 records of somebody named John elder but each record has a unique ID so you can delete record number 87 for instance and there are no other records that have the ID of 87 so that's what that's used for so okay so we can we create this this command here want to select everything and our IDs from addresses we also need to then do something called fetch you also see fetch all fetch all does just what it sounds like it fetches all of the records now you can do fetch one and it will just bring back one record the first record you can do fetch many and then in here say designate how many records you want to fetch 50 for instance we don't want to do that we just want to fetch all okay so normally you could just print out see well just print out this thing right but this is kinder and print doesn't really work with Kenter so we need to create a label and kind of print that onto the screen but first let's just run this by actually doing that well actually instead of using CDF fetch all let's smush that into a variable so that we can then put that variable into a label as we've done so many times so let's call this records and set that equal to that now we can pet print out records and it won't print it to the screen in our app but it will print it to the terminal after we close the app so let's run this real quick just to see what this record looks like that we've already put in and back over run it again and if we click show records nothing happens but then if we then close that we see boom this record appears and you'll notice these brackets that means that means that this is returning a Python list and inside of that list there is a Python double and then inside of the temple you can access each of these things by their index number so John is the zero if item in the list in the top will one two three four five and then six so that's the way it's returned we can of course do anything we want with this information this data we can sort it out and and put it on the screen however we want so what exactly do we want to do well instead it will leave the print there for now in case we need to troubleshoot but since we just have one record let's go let's create a for loop right so let's go for what do we want to call this so we've called this one records so let's call this for record in records right and then we want to let's create a variable called print records and we want that to plus equal out whatever is in records and since since it's a list with a couple inside of it the zeroth item of the list is our tuples so we can actually call the zeroth item we'll change this in a minute you'll see why but for the very first time we do this since there's only one record we can do it like this now I want to sort of print out record right which is the item in the loop that we're going to loop through and we also want to concatenate and then put a line break and the reason why I want to do this is because we're going to create a label here and we want each item that gets printed out to be on its own line so we have to put a line break in here now this is a problem because record the thing we printing out you can see one of these a couple of these things are integers and you can't concatenate a string with an integer so we actually need to convert this whole thing to a string which is not too hard we can just wrap this whole thing in the string function okay so now outside of this loop we want to create a label so let's call this the query label so we're making a query and set that equal to label and then it's in root and the text we want to be print underscore records right now we actually need to before we start looping we need to create that variable and set it equal to nothing since we're going to function here and that's how that works so let's go loop through results so if we need to query underscore or label dot grid this thing out and I think we're in row eight now down here and look yeah so that row seven is the last thing we did so now we're in row eight and we want column equals zero and we need to column span this thing out to equal two and I think that will work so if we save this and run it again pull this over click show records boom on each line we get an item in that tupple John elder and we can do whatever we want with these things okay so that works with one record but we're gonna have more than one record so let's create another one real quick let's go Bob Smith he lives at twenty East cedar cedar Street I don't know st. louis MO missouri and what the zip code is there six to nine oh one who knows okay so now we add this to the record boom it disappears if we click here nothing happens why well let's close this and we can see whoops it looks like this has been added twice what's going on there oh that's from the first time we click the button disregard that yeah okay so look when we call this loop we're calling the zeroeth item in our list which if we pull this back up here's our list and there are two items in it item one is this tuple and then there's a comma and then it here's item two so we're calling item one or item 0 which is this item 0 with item so that's the only thing it prints to the screen we don't really want to do that so let's just take this off and just print out everything alright so if we save this and run it let's clear the screen run it again pull this over show records boom now we get each tough old printed on its own line and has you know everything inside of there it's in our records so and we see the last thing is the ID the primary key the OID so there's one there's two very cool so now this is tuples we know from just regular Python how to do stuff with tuples so we can format this any way we want and it's still printing this out on the screen so I think now we can get rid of this print thing right here I'll just comment it out in case you want to reference this code later but down here let's see in our for loop we can tell this to print out anything we want so each record is an item now is a tupple and inside of that it has item numbers couple numbers right so the zeroeth item of each of these records is the first name so if we just do that and save this and run it now we will get first names printed John and Bob okay very cool so you know we can do anything we want with this we can concatenate some more let's put in a space and then concatenate it again and then let's just grab this whole thing and paste it and then concatenate the line break again this guy needs a quotation mark okay instead of record the zeroeth item let's call the first item which is the last name excuse me save this run it again show records John elder Bob Smith very very cool if we had another person Tina Miller I don't know she lives at 89 Apple Street and I don't know that's a good Town New York New York 1:09 - I have no idea what the zip code is there now if we add this boom that disappears if we click this button again boom Tina Miller pops right on up so like I said you could format this any way you want I'm gonna leave that to you I'm just showing you the basic functionality of how to do these things there's a thousand ways you can create reports and things and output data however you want that's the beauty of Python and kinter so I'll leave that to you I think in the next video we need to build a thing in here to delete a record if we want to remove Bob Smith there's no way to do that yet so we'll do that in the next video meantime if you're interested in this database stuff specifically the sequel Lite database head over to my website go to me.com I just released a course not long ago in sequel I this is just pure sequel Lite and Python and it's 22 videos hour and a half long and it cost $29 of course you can sign up for total membership using that YouTube coupon code I'm always going on about and you'll pay just $27 for all of my courses including this one which is better than one course for 29 so if you're interested definitely use that coupon code some people don't understand it and you'll learn all of this stuff in great detail we really go in in more detail than I'm gonna go into in this series I'm just gonna show you some basic stuff right now like I said if you're interested take a look at that course to learn you know in-depth stuff about sequel light alright so that's all for this video if you liked it be sure to smash the like button below subscribe to the channel and check out Cody be calm or you can use coupon code youtube get $22 on membership pages $27 to access all my courses hundreds of videos and the PDFs of all my best-selling coding books join over 50,000 students learning to code my name is John elder from Cody me calm and we'll see in the next video
Info
Channel: Codemy.com
Views: 147,431
Rating: 4.9674797 out of 5
Keywords: python, tkinter, gui, python tkinter, tkinter python, python gui, tkinter gui, gui python, graphical user interfaces python, tkinter grid system, grid system tkinter, python grid system, build graphical user interfaces with python, how to build graphical user interfaces with python, how to python tkinter, sqlite3, sqlite python, sqlite gui, python and sqlite, python sqlite, address book app, address book python
Id: AK1J8xF4fuk
Channel Id: undefined
Length: 28min 13sec (1693 seconds)
Published: Fri May 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.