How to make table out of lists using TreeView Tkinter Python

YashuC ッ

What I want.. is to how create a table using TreeView Tkinter and data which is to be inserted in the table should from lists.

For example:

This is the list i have

ID = [1,2,3,4,5]
Names = ['Tom', 'Rob', 'Tim', 'Jim', 'Kim']

And I want to create a table with headings "ID" and "Names" and I don't want to put each values in the table separately. Well this is a small list but even when I want to create a bigger table. So can anyone tell me how can I insert these list data in table automatically(loop).

JacksonPro

Just use for loop something like this:

from tkinter import ttk 
import tkinter as tk 

ID = [1,2,3,4,5, 6, 7, 8, 9]
Names = ['Tom', 'Rob', 'Tim', 'Jim', 'Kim', 'Kim', 'Kim', 'Kim']
  
window = tk.Tk() 

treev = ttk.Treeview(window, selectmode ='browse') 
treev.pack(side='left',expand=True, fill='both') 
  

verscrlbar = ttk.Scrollbar(window,  
                           orient ="vertical",  
                           command = treev.yview) 
  
verscrlbar.pack(side ='right', fill ='y')   
treev.configure(yscrollcommand = verscrlbar.set) 

  
treev["columns"] = ('1', '2') 

treev['show'] = 'headings'
  
treev.column("1", width = 90, anchor ='c') 
treev.column("2", width = 90, anchor ='c') 


treev.heading("1", text ="ID") 
treev.heading("2", text ="Names") 
  
for x, y in zip(ID, Names):
    treev.insert("", 'end', values =(x, y)) 

window.mainloop() 

If You want a better way of doing it then use a dictionary:


from tkinter import ttk 
import tkinter as tk 

titles={'Id': [1,2,3,4,5, 6, 7, 8, 9], 'Names':['Tom', 'Rob', 'Tim', 'Jim', 'Kim', 'Kim', 'Kim', 'Kim', 'Kim'], 'Column': [1,2,3,4,5, 6, 7, 8, 9]}

  
window = tk.Tk() 

treev = ttk.Treeview(window, selectmode ='browse') 
treev.pack(side='left',expand=True, fill='both') 
  

verscrlbar = ttk.Scrollbar(window,  
                           orient ="vertical",  
                           command = treev.yview) 
  
verscrlbar.pack(side ='right', fill ='y')   
treev.configure(yscrollcommand = verscrlbar.set) 

treev["columns"] = list(x for x in range(len(list(titles.keys()))))
treev['show'] = 'headings'

  
for x, y in enumerate(titles.keys()):
    treev.column(x, minwidth=20, stretch=True,  anchor='c')
    treev.heading(x, text=y)

for args in zip(*list(titles.values())):
    treev.insert("", 'end', values =args) 

window.mainloop() 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make border for my table using python tkinter?

How to make dataframe out of lists?

How to make two lists out of three on Python3

How to make a list of numbers out of strings in a list of lists in Python

python tkinter - Make a treeview widget for Directory display with a list of folders

Python Tkinter, How do I get the subitems of an Item in Tkinter TreeView?

Add styles to headings (text bold and backgroundcolor) of a Treeview Table - Tkinter Python

Is there a way to change treeview table's column numbers in python tkinter?

How to make a flat list out of list of lists?

How to make a flat list out of a list of lists?

How do I use tkinter 'Treeview' to list items in a table of a database?

tkinter: How to serialize a treeview?

How to make buttons to be only clicked once using Tkinter in Python?

How to make a live date display using Python and tkinter

sql output in treeview columns using tkinter module in python

How to make a poll in python with lists

How can I attach a vertical scrollbar to a treeview using Tkinter?

How to get tkinter treeview selected row values using keyboard?

tkinter: How to update Treeview item from another thread using queue?

how to make a backspace with TKINTER in python

How to open file from treeview in text widget python tkinter

Python Tkinter Treeview - How do I determine if the last child is displayed?

How to imitate this table using Tkinter?

Tkinter/Python Treeview change header

Python tkinter treeview column sizes

Image in treeview Tkinter library in Python

python : tkinter treeview colors are not updating

Python Tkinter Treeview displaying mysql

How to filter out equivalent lists of values in python?