且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 Tkinter GUI 使用 read_csv 打开和读取 csv 文件

更新时间:2023-01-29 19:33:04

正如@Kevin 所建议的,您需要将一些功能放入按下按钮时调用的函数中.我提供了一个例子(我没有安装熊猫,所以熊猫部分被注释掉了).您还应该使用 askopenfilename 而不是 askopenfile.我还修复了您的关闭按钮,请注意我已将其更改为 root.destroy 并且我没有将 () 放在最后.

As suggested by @Kevin, you need to put some of the functionality in to a function that is called when the button is pressed. I've provided an example (I don't have pandas installed, so the pandas part is commented out). You also should be using askopenfilename not askopenfile. I've also fixed your close button, note I've changed it to root.destroy and I haven't put () at the end.

import tkinter as tk
from tkinter.filedialog import askopenfilename
#import pandas as pd


def import_csv_data():
    global v
    csv_file_path = askopenfilename()
    print(csv_file_path)
    v.set(csv_file_path)
    #df = pd.read_csv(csv_file_path)

root = tk.Tk()
tk.Label(root, text='File Path').grid(row=0, column=0)
v = tk.StringVar()
entry = tk.Entry(root, textvariable=v).grid(row=0, column=1)
tk.Button(root, text='Browse Data Set',command=import_csv_data).grid(row=1, column=0)
tk.Button(root, text='Close',command=root.destroy).grid(row=1, column=1)
root.mainloop()