如何将数据从csv文件插入到treeview?

时间:2019-11-08 14:18:18

标签: python tkinter

我在tkinter中有一个树状视图:

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()

和一个csv文件:

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

如何更改此代码:

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

显示基于csv值的值?

客户要填写客户树状视图,而产品要填写产品?

结论

基本上将csv数据导入树视图。

1 个答案:

答案 0 :(得分:0)

您需要import csv。对于该函数,请删除以下代码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))

从以下链接中获取了此信息:https://www.sourcecodester.com/tutorials/python/12494/python-import-csv-file-tkinter-table.html