LoginSignup
1
1

python,tkinterでcsvファイルの数値を読み込み、出力する

Posted at

今回はtkinterからcsvファイルを指定し、
数値を読み込み、tkinter上にデータを出力するアプリを作成しました。

以下が全体のソースコードです。

qiita.py
import tkinter as tk
import tkinter.filedialog as fl


def get():
    #ファイルパスをマウスで選ぶ
    #ユーザー名にお使いのPCの名前を入れる
    filetype=[("all file","*")]    
    path=fl.askopenfilename(initialdir="C:\\Users\\ユーザー名\\デスクトップ",filetypes=filetype)

    #csvファイルの読み込み
    f = open(path,"r")
    text = []
    for i in f.readlines():
            i=i.strip()
            i=i.split("\n")
            if i == ['']:
                pass
            else:
                text.append(i)

    line = text[0][0]
    line_split = line.split(',')
    line_split = list(map(int, line_split))


    # ラベル
    lbl = tk.Label(text='数値')
    lbl.place(x=150, y=70)

    # csvファイルの数値を出力する
    txt = tk.Entry(width=50)
    txt.place(x=200, y=70)
    txt.insert(tk.END,f"{line_split}")


##main処理
root=tk.Tk()
root.title("csvファイルの読み込み&出力")
root.geometry('600x400')
    
button=tk.Button(text="csvファイル開く",command=get)
button.pack()

root.mainloop()

tkinterの起動時のデザインです。
qiita_csv_ui.png

tkinterのファイルを選択後に、数値が出力されました。
qiita_csv_output.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1