Add And Remove Records From Treeview - Python Tkinter GUI Tutorial #117

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys john elder here from cody.com in this video i'm going to show you how to add and remove items from our tree view with kinter and python all right guys in this video we're going to look at adding and removing items from our tree view 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 give me a thumbs up for the youtube algorithm and check out kodamy.com where i have dozens of courses with hundreds of videos that teach you to code use coupon code youtube1 to get 30 off membership that's all my courses videos and books for one time fee it just 49 which is insanely cheap okay in the last video we looked at our treeview thing here so we've got this thing we've got these headers we can move them around in this video i want to show you how to add items so we've got steve steve likes bacon we can add him boom how to remove items if we click on this we want to remove just one we can do that if we want to remove a couple we can remove them all if we want to do everything at once we can click that and get rid of all of them so that's what we're going to look at in this video so let's head back over to our code and i'm in tree dot pi and if you missed the first video this little series here about the tree view check the comment section below for the link to the playlist so i'm using the sublime text editor in the gate batch terminal as always so the first thing i'm going to do is i'm going to bop up the size of our app to 600 and let's let's add let's make our tree view a little bigger so i'll add 20 there add 20 to this column and i'll add 20 to that column just to space this out a little bit more so okay let's come down here and uh underneath where we packed this thing let's create a frame that's going to hold that those three little boxes that we can add records so i'm just going to call this add frame and this is going to be a frame and we want to put it in root so let's go add underscore frame dot pack to pack this onto the screen give it a pad y of 20 just to push it down a little bit so now let's create three labels and then three entry boxes so that we can add a record to our tree view so i'm just going to call this nl for name label and this will be a label and we want to put it in root and want the text to equal name and then let's go nl.grid we want this in row 0 column 0. so now we want to let's add an id and i'm going to call this id label il and it's going to be a label and we want to put it in root we want the text to equal id and then we can go i l.grid and let's put this in row equals zero column equals one and then finally let's go what toppings label tl that's going to be a label i want to put it in a root we want the text to equal topping and we can tl dot grid this guy put it in row 0 and column 2. so now let's do uh entry boxes and let's i guess comment this labels right so here let's let's call this one uh name box i guess and this is going to be an entry widget we want to put it in oh mistake we don't want to put these labels in root we want to put it in add frame there we go so this box also we want to put it in the add frame frame we want oh we don't have to put anything else but that so now let's go name underscore box dot grid we want to put this in row one and column zero so one row down and let me just copy this and let's paste it in a couple times this will be the id box and we want to put it in column one and this will be the topping box and we will put it in column two so let's save this and run it just to make sure this is working so python tree dot pi pull this over okay we've got our 3d entry boxes it's a little sloppy but we don't really care this is just for example purposes anyway uh to see if this will work so okay we've got this so now let's create a button to add record so close this back over to our code let's go buttons and let's go uh add record and that's a button we want to put it in let's just put this in root we don't have to put it in that entry or in that add frame frame because we're not going to put these like that we'll just put them down below so it doesn't really matter so we'll just do this and we want the text to be add record and let's give this a command of add record okay so now we should pack this guy while i'm thinking about it add record dot pack and let's give this a pad y of 20. so now let's create add record function so that's define add record so remember how we added things in the last video we did like this we used these things so i'm just going to grab this whole thing and basically we're just going to do the same thing come down here and add this now instead of each of these things right we'll put in we'll get the name box id box or topping box so let's just do that right now so instead of the name here we call namebox.get and remember that's a function this one will be id box dot get and that's a function and then finally this one will be what was this one topping box dot get and that's a function so we also need to give it an id so remember each one we give a sequential id so let's just use count because remember up here we use this count variable and to use this i'm going to make this global so let's go global count right here and remember it starts at zero and as we add these things in our loop we're adding one to the count so the last thing that will have been added will have been the last number so here we can just go [Music] global count and then instead of calling a specific iid can just use this count variable here so okay that will work and that looks good so we'll also need to increment our count so let's count plus equals one okay so we might also want to then clear those entry boxes right so let's go name.box.delete and we want to delete from zero to end and let me just copy this so this is id box and this is topping box and we don't want to put it here we want to put it after all this stuff there we go so let's comment this uh clear the boxes okay so okay i think that looks good let's go ahead and save this and run it just to see if that worked so we can add steve steve is let's say id six and steve likes what mushroom pizza you can add a record boom steve pops up all right so that's pretty easy oh this one didn't get cleared what did i do there topping by oh i misspelled topping yeah it's friday that happens okay so that works so how do we now remove things from our tree view well let's start out by uh remove all let's just remove everything so let's call this one remove all and this is going to be a button want to put it in root we want the text to equal remove all records and let's give this a command of remove underscore all copy this and then let's pack this guy onto the screen so remove all dot pack and let's give this a pad y of like 10 just to push it down a little bit so okay we can come up here to our sort of functions section and let's go remove all records and let's define this as remove all okay so how do we remove everything from our tree view well a lot of times we call the delete function so like an entry box will be uh entry box dot delete and then like right here you give it a range from zero to end and you can't quite do that with a tree view so a tree view is made up of a bunch of children's every sort of record or line or row however you want to call it in a tree view is sort of like a child of the parent tree view so we have to loop through the all the records all the children and delete each one so to grab each one we call our tree and ours is called my tree and then we would go get underscore children now this will get a list of all the children in our tree view right so now we can just loop through this list so let's go for a record in my tree.i get children and a colon there because this is a loop and then we would just go my underscore tree dot delete and then record so we're going to grab a list of all the records right and then we're going to delete them one at a time looping through them all so let's go ahead and save this and make sure that worked let's head over here run this guy again so here we can remove all records and boom they all disappear okay pretty easy so now what do we do if we want to just delete a specific one just one so like let's run this again let's say because we can click on one of these right so if we click on one of them and we want to delete just that one how do we do that well that's pretty simple let's create a comment down here and call this remove one and let's call this remove underscore one and this is going to be a button and we want to put it in root we want the text to equal remove one selected something like that we want to designate just removing one versus removing many that we've selected because you can select more than one if you want so let's remove one selected and let's give this a command of remove underscore one okay and now let's remove underscore one dot pack and give this a pad y of like 10 just push down a little bit and let's come up here to our function section and let's remove one selected and so let's define this function now in order to determine that something has been selected we just call our tree so my underscore tree dot selection and this is a function now this will get a list of all the things selected right if you just want one item you can designate it as if it were a list so we want the zeroth item selected because there's only going to be one item selected and this is a list and so lists start at zero so we could just say hey delete this zeroth item if we selected like five of them and we wanted to delete the last one that would be four because it starts at zero one two three four right if we selected two items and we wanted to delete the second item we would put a one there right so you can use this to select any specific one of a list of things that you've selected and highlighted in blue in our case we're just selecting one so we just want the zeroth item selected right and now we can we can add this to a variable i'm just going to call it x now we can delete this so my underscore tree dot delete just like we did earlier but instead of passing record here we just want to pass in x so okay that should work let's go ahead and save this and run it so if we grab error in here and we remove one selected boom she disappears now notice if we select two and to do that i'm holding the control key on my keyboard and selecting more than one command button i think if you're on a mac now we've selected two right but our code says to delete the zeroth item which is the first item which should be tim so if we click this it should just delete tim and leave bob and it did so so sort of keep that in mind so now what happens if we wanna like select several of them and delete them all how do we do that well we can look at that now pretty simple so let's come down here and let's go remove many selected so let's go remove underscore many and that's going to be a button it's going to be in root the text is remove many selected or whatever right let's give this a command equal remove underscore many and let's copy this and let's pack this guy on the screen so remove many dot pack give it a pad y of 10 push it down a little bit so let's come up here and remove many selected so let's define this function and how do we remove many well sort of the same thing right we're going to my tree dot selection but instead of picking a specific one we can just leave it like this because this is a list of everything that's selected so again let's assign this to a variable now there's a bunch of things selected here so we kind of probably want to loop through them all if we just went well i don't know maybe this will work let's see my tree dot delete i don't think this is going to work i think we have to loop through here and individually delete each item but let's give it a try and see let's go bob aaron and tim remove many selected does not work i was correct we need to actually loop through all of these things so what we can do is let's go for record in x then bring this up and instead of x we want to delete record right so basic for loop that should do the trick so let's go ahead and save this and run it and let's go bob aaron and mary so john and tim should be the only ones left click remove selected boom john and tim are the only ones left and that's all there is to it so i know i went through those really fast but they're pretty simple so it shouldn't be too bad just got to do some looping and that's all there is to it so that's all for this video if you like to be sure to smash the like button below subscribe to the channel give me a thumbs up for the youtube algorithm and check out kodamy.com where you can use coupon code youtube wanted to get thirty dollars off membership pages 49 to access all my courses over 45 courses hundreds of videos and the pds of all my best-selling coding books join over a hundred thousand students learning to code just like you my name is john elder from codiby.com and i'll see you in the next video
Info
Channel: Codemy.com
Views: 39,276
Rating: undefined out of 5
Keywords: tkinter treeview remove items, tkinter remove treeview items, tkinter treeview, tkinter treeview table, tkinter treeview tutorial, tkinter treeview style, tkinter treeview selection, tkinter treeview insert, tkinter treeview widget, tkinter treeview select event, tkinter treeview select row, python tkinter treeview example, python tkinter treeview table, python tkinter treeview ttk, using tkinter treeview, using ttk treeview, ttk treeview tutorial, ttk treeview
Id: rtR5wHXPKZ4
Channel Id: undefined
Length: 15min 6sec (906 seconds)
Published: Fri Sep 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.