简体   繁体   English

如何将 csv 文件中的数据插入 treeview?

[英]How to insert data from csv file to treeview?

I have a treeview in tkinter:我在 tkinter 中有一个 treeview:

self.progress_view = ttk.Treeview(dataWindow, columns = ('Customer', 'Product'), show = "headings")
    self.progress_view.heading('#1', text = 'Customer')
    self.progress_view.heading('#2', text = 'Product')
    self.progress_view.pack()

and a csv file:和一个 csv 文件:

Customer, Product
Customer 1, Almonds
Customer 2, Flaked Almonds
Customer 3, Walnuts

How can I change this code:如何更改此代码:

def insert_Data_Custom(self):
    self.progress_view.insert("", 'end', values = )

to show values based on the csv values?显示基于 csv 值的值?

Customers to fill in customers treeview and products to fill products?客户要填客户treeview和产品要填产品吗?

Conclusion结论

Basically import csv data into treeview.基本上将csv数据导入treeview。

You'll need to import csv .您需要import csv For the function remove the following code self.progress_view.insert("", 'end', values = ) and try the following:对于 function 删除以下代码self.progress_view.insert("", 'end', values = )并尝试以下操作:

with open('file.csv') as f:
    reader = csv.DictReader(f, delimiter=',')
    for row in reader:
        customer = row['Customer']
        product = row['Product']
        self.progress_view.insert("", 0, values=(customer, product))

got this from the following link: https://www.sourcecodester.com/tutorials/python/12494/python-import-csv-file-tkinter-table.html从以下链接得到这个: https://www.sourcecodester.com/tutorials/python/12494/python-import-csv-file-tkinter-table.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM