2017-05-26 12 views
0

どのようにしてCSVをリストボックスにインポートできますか?テキストの代わりにCSVデータをリストボックスに入れたいのですが、これを行うためのPython Fileコマンドがありますか?Tkinterを使用してCSVをPythonリストボックスにインポート

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") 
+0

csvを読んで各列をリストボックスに挿入する必要があります。しかし、あなたはタプルとして行を取り、それぞれの値を各リストボックスに加える独自の関数を書くことができます –

答えて

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

は、今あなたがリストボックスに追加することrown上のデータの組を渡すことができる機能を持っています。 csvファイルの各行を簡単に繰り返し処理し、行データを使ってその関数を呼び出すことができます