creating tables in tkinter with the treeview widget

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the next widget we are going to look at is called Tree View and if canvas is paint treeview is essentially Excel or rather we are going to create a table this one is going to look like this in here we have a bunch of different entries that we can look at we could also delete entries like so and you can also select multiple entries and work with them it's a fairly straightforward table that is quite easy to work with and well that's what we're going to create once again I have some code if I execute the entire thing we have a basic window the one thing that is new are these two lists I'm going to use them to create some random names in a bit for now just don't worry about them I want to create a widget and this is called Tree View this we create with ttk.3 View as always I want to store this in a separate variable and this I'm going to call table in here we always need the master which is going to be the window and let me just pack the entire thing to see what we get by default we're getting a plain white square although if I go to the top we can already see we have some kind of top row that doesn't do anything right now that we can change inside of this the idea is that you are defining some columns for example what I want to do is I want to have a column called first then last and finally email if I run this now you can see we have three different columns although you can't see the title right now we have to add what is called headings so I want to get table and then specify a heading and now I have to get the name of the column and this is what we specified here the First Column is called first the second one last and the last is called email which means for the First Column I want to get my first column and the text in here is going to be first name and now if I run this we can see we have first name right in the middle to get it to the left side we have to add another argument inside of the original Tree View and this is called show and then here I want to add headings now if I run this we get first name on the left side by default tkinter is showing some elements on the left side or in the first columns and with this option we are disabling that cool now besides that for the last column I want to have let's call it surname and then for the email I want to have the email and with that we have different columns and then here you could add as many as you wanted and with that we can cover how to insert values into a table this we're doing with table and the insert method in here we need a couple of arguments the first one is called parent and this is usually going to be an empty string what this means is you want this item to be attached to this table and not have any intermediaries later on we're going to learn you could also attach this new item to an existing item inside of the table and then you will get the name of this item inside of the table as the parent but if you leave it empty you're going to attach it to the main table besides that we need an index and we need the actual values for the index for now let me go with zero and the values is going to be a tuple and since we have three columns first last an email we need three values in here let's call this John or the surname I want do and for the email I want to have John Doe at email.com or whatever you want to add in here if I run this now we have a first name a last name and the email so this is going to create one entry but I want to create a hundred entries so let me comment this out and instead I want to create a for Loop so for I in range 100 and I want to use these values here to create random names for that I'm going to need one more module and that is from random import choice choice we can use to pick one item from a list and this could actually be a really good exercise for you it's not going to be the main exercise for this video but try to create from these names 100 entries and add them to the table and see if we can figure this one out I want to create a first name a last name and an email the first name is quite easy I want to have Choice and then first names the same for the last names I want to choice and pick a random item from the last names for the email I want to create an F string and for this one I want to have the first name then I want to have the last name like so and then add email.com this information we can now turn into a tuple let me call it data and here we are first we have last and we have email finally I can actually get the table and insert the values once again for the parent I want to have an empty string besides that for the index for now I'm going to go with zero and then for the values I want to have data now I can run this and we can see we have a table with a ton of different entries this is looking really good and there's one very quick layout thing that I do want to do inside of pack I want to fill and add the option here for both and then expand I want to set to true I'm going to explain those values later but if I add them we're going to fail the entire widget with entries that looks much better and obviously you could also change these values as much as you liked so for example in the demo app I don't use the first name for the email I only use the first letter of the first name so index 0 here and now we have some slightly different email addresses now let's talk about index and the best way to understand it is if I play around with the value for this one we need parent with an empty string or the index by default this is going to be 0 and for the values I want to have something that's easily visible let me go with XXXX then we have yyy and then we have z z z if I run this now all the way at the top we have the values that I just specified with this entry here if I change this index to a 1 we are placing it on the first index So Below the first element and this I can keep on doing three we have three items on top and so on the question now is if you wanted to add an element at the end of this table how could you do it I guess in our case we know there are 100 elements so I could add 101 in here and now all the way at the bottom we have the entry I just created but sometimes you don't know the end of the table and for that we can use TK and end that way the new item is always going to be inserted at the end and I just realized this needs to be all in uppercase letters like so and now this is working if I scroll all the way to the bottom now we are always at the bottom and that is what index is doing there are two more things that we have to understand we have events inside of a table and we have items and those work really well together so let me start with events there's one special event at a table or a tree view has we need two smaller than and two greater than signs and then here we have three view select this is going to be triggered every time we are selecting an item inside of the table for now let me just Lambda with event and I want to print the event that we are getting so if I run this now and I click on one and we are not getting very much as a matter of fact the event is always going to be the same no matter what we click on the event here you can basically ignore what you rather want to do is get table and then what is called selection this gives us the curly selected items so now if I click on something we get a random number and I can click on multiple items the number is always going to be a bit random so what does that actually mean if this one here is the table this table is populated with lots of items this could be one item this could be another item this could be a third item basically one item is one row of data this is the item we are going to talk about in just a bit oh well we are talking about it right now and each of those items has a specific ID this was what we have seen something like IO and then two numbers 45 for example these numbers we can use to select items from this table and this I want to do in a separate function so let me Define item select this I want to run in here so item select in here I need one argument for the event although since the event is basically pointless I'm going to add an underscore in here to indicate that I don't care about this value in here once again I want to print my table and then the selection so if I run this now I can click on various items and we get some random ID this random ID I can now use the way you use it is you want to get the table and then the item and in here you can add the table dot selection if I comment this out there's going to be one problem because if I click on this you can see we don't just have the ID we have a tuple that has one element inside of them and that is the ID the reason for that is we can select multiple items so if I select Thomson and hold shift we have a bunch of different items the way you are going to work with this is inside of a for Loop that'd be an easy way so for I in table dot selection then I can use table and item and then here I want to look at the item this I could for example print and let's see what we get if I now click on let me show my mouse again if I click on Lisa Thompson we get text nothing image nothing and then values Lisa Thompson LT Thompson and so on those are the main items although there are a few more for this video I want to keep it simple so in here I only want to show the values now if I show this again I can click on one item and I can select multiple by holding down shift and we always get all of the different items or rather the actual values and with that we have also covered items so let me get rid of the comment and this is covering the basics of a table although there's one more thing that I do want to do I can find another event for this one we only need one smaller and greater than symbol and in here I want to look for delete if that is the case I want to delete items this delete here stands for the delete key on the keyboard so in here I want to delete items and create a function for the event again we don't care about it so an underscore and let me just print delete so we can see what's going on if I now run the entire thing and I have to table select it and I click on delete we get delete and I want to use this to actually get rid of a value and this I think could be a really good exercise for you try to figure out this function here one method you need is called table dot Elite in here you can pass an item and this is going to delete Whatever item you pass in here and that is going to delete the selected row although you have to figure out how to actually get all of the items so positively now and try to figure this one out basically what we have to do is very similar compared to this in fact let me copy it and I again want to look at my entire selection but now I don't want to print it instead I want to get my table and delete it and then here I want to add I so now if I run this again and I can select a couple of entries let's go with those two and if I click on delete they disappear this works with as many items as I can select and they always just disappear that way we have quite a fancy table that works really well and let me try to delete all of the values inside of this table then you can see it very well that this is indeed working quite good cool so with that we can delete items inside of this table or rather rows that usually makes a bit more sense and with that we have the basics of a table now tables do get quite complicated so there's a lot more stuff that you could cover but if you got so far and you have a documentation this shouldn't be too hard
Info
Channel: Atlas
Views: 29,681
Rating: undefined out of 5
Keywords:
Id: jRpHmF-iuMI
Channel Id: undefined
Length: 14min 44sec (884 seconds)
Published: Thu Dec 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.