简体   繁体   English

使用Tkinter将CSV导入Python列表框

[英]Import CSV to Python List Box Using Tkinter

How would I import a CSV into a Listbox? 如何将CSV导入列表框? I would like CSV data to go into the list boxes below, instead of text, is there a Python File command that would allow me to do this? 我希望将CSV数据而不是文本放入下面的列表框中,是否有Python File命令可以让我执行此操作?

class RequestGUI():

    lblStatus = Label(root, text = 'Status')
    lblStatus.place(x =6, y =5)

    lblFacName = Label(root, text = 'Facility Name')
    lblFacName.place(x =150, y =5)

    lblDWGTitle = Label(root, text = 'Title')
    lblDWGTitle.place(x =525, y =5)

    colStatus = Listbox(root, height = 12, width =6)
    colStatus.place(x = 6, y = 32)
    colStatus.insert(END, " IN")

    colFacName = Listbox(root, height = 12, width =45)
    colFacName.place(x = 56, y = 32)
    colFacName.insert(END, " NW WASHINGTON")

    colDWGTitle = Listbox(root, height = 12, width =72)
    colDWGTitle.place(x = 340, y = 32)
    colDWGTitle.insert(END, " CAPACITOR VOLTAGE")
import tkinter as tk # from tkinter import * is bad, don't do this!

class RequestGUI():
    def __init__(self, root):
        self.lblStatus = tk.Label(root, text = 'Status')
        self.lblStatus.place(x =6, y =5)

        self.lblFacName = tk.Label(root, text = 'Facility Name')
        self.lblFacName.place(x =150, y =5)

        self.lblDWGTitle = tk.Label(root, text = 'Title')
        self.lblDWGTitle.place(x =525, y =5)

        self.colStatus = tk.Listbox(root, height = 12, width =6)
        self.colStatus.place(x = 6, y = 32)

        self.colFacName = tk.Listbox(root, height = 12, width =45)
        self.colFacName.place(x = 56, y = 32)

        self.colDWGTitle = tk.Listbox(root, height = 12, width =72)
        self.colDWGTitle.place(x = 340, y = 32)

        self.add_row(tk.END, (" IN", " NW WASHINGTON", " CAPACITOR VOLTAGE"))

    def add_row(self, index, rowdata): # will throw indexerror if not enough data is supplied
        self.colStatus.insert(index, rowdata[0])
        self.colFacName.insert(index, rowdata[1])
        self.colDWGTitle.insert(index, rowdata[2])

if __name__ == '__main__':
    win = tk.Tk()
    gui = RequestGUI(win)
    win.mainloop()

so now you have a function you can pass a tuple of the data on that rown, which will add to the listboxes. 因此,现在您有了一个函数,可以在该Rown上传递一个元数据组,该数据组将添加到列表框中。 so you can simple itterate over each row in the csv file and call that function with the row data 因此,您可以对CSV文件中的每一行进行简单的操作,并使用行数据调用该函数

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

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