data = pd.read_csv('./x1.csv', names=['0','1','2','3','4','5','6','7','8','9'])
x = data['2']
y = data['3']
z = data['4']
u = data['6']
w = data['7']
这段代码如果通过GUI实现,如何做?谢谢!
import tkinter as tk
from tkinter.filedialog import askopenfilename
import pandas as pd
import csv
root = tk.Tk()
root2 = tk.Tk()
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)
newfile = pd.read_csv(csv_file_path,encoding="gbk") # 读的时候设置格式
colist=newfile.columns
newfile = newfile[colist]#colist是一个列表,里边存放的是列名入['a','b','c']
tpframe2 = tk.Frame(root2)
tpframe2.pack(fill=tk.BOTH)
s2 = tk.Scrollbar(tpframe2, orient=tk.VERTICAL)
s2.pack(side=tk.RIGHT, fill=tk.Y)
# 显示文本内容组件
text1 = tk.Text(tpframe2, height=600, yscrollcommand=s2.set)
text1.pack(fill=tk.BOTH)
s2.config(command=text1.yview)
text1.insert(tk.INSERT,newfile)
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()
是这样吗?要设置文件格式为gbk或者utf-8之类的
# _*_ coding: utf-8 _*_
#!/usr/bin/env python3
import pandas as pd
import tkinter as tk
window = tk.Tk()
window.title('读取CSV') # 标题
window.geometry('500x500') # 窗口尺寸
text1 = tk.Text(window, width=50, height=10, bg='orange', font=('Arial', 12))
text1.pack()
def read_csv():
data = pd.read_csv('./x1.csv', names=['0','1','2','3','4','5','6','7','8','9'])
print(data['2'])
return data
def read_x():
text1.insert('insert', read_csv()['2'])
def read_y():
text1.insert('insert', read_csv()['3'])
def read_z():
text1.insert('insert', read_csv()['4'])
def read_u():
text1.insert('insert', read_csv()['6'])
def read_w():
text1.insert('insert', read_csv()['7'])
bt1 = tk.Button(window, text='打印x', width=15, height=2, command=read_x)
bt1.pack()
bt2 = tk.Button(window, text='打印y', width=15, height=2, command=read_y)
bt2.pack()
bt3 = tk.Button(window, text='打印z', width=15, height=2, command=read_z)
bt3.pack()
bt4 = tk.Button(window, text='打印u', width=15, height=2, command=read_u)
bt4.pack()
bt5 = tk.Button(window, text='打印w', width=15, height=2, command=read_w)
bt5.pack()
window.mainloop() # 显示
没明白
可以说一下界面要什么样?你这个只是一小部分逻辑,放哪都行