gpt4 book ai didi

python - ValueError:无效的文件路径或缓冲区对象类型:

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:46 25 4
gpt4 key购买 nike

这是我拥有的一些代码的简化版本。在第一帧中,用户使用“tk.filedialog”选择了一个 csv 文件,它被绘制在 Canvas 上的同一帧上。

还有第二个框架能够绘制图表,以防跨不同的框架更容易绘制。

运行此版本的代码会导致错误:“ValueError:无效的文件路径或缓冲区对象类型:”。我不确定如何在不发生此问题的情况下使此代码正常工作,以便用户选择的文件绘制在包含“a”和“b”列的空图形上。

import csv
import pandas as pd
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
from tkinter import messagebox
import matplotlib

matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

from matplotlib.figure import Figure


fig = Figure(figsize=(5,4), dpi=100)
ax= fig.add_subplot(111)

LARGE_FONT= ("Verdana", 12)

class GUI(tk.Tk):

def __init__(self, *args, **kwargs):

tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "GUI")

container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)

self.frames = {}

for F in (Home, Graph):

frame = F(container, self)

self.frames[F] = frame

frame.grid(row=0, column=0, sticky="nsew")

self.show_frame(Home)




def show_frame(self, cont):

frame = self.frames[cont]
frame.tkraise()


class Home(tk.Frame):

def __init__(self, parent, controller):
self.controller = controller
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10, padx=10)



ftypes = [
('CSV files','*.csv')
]

def browsefunc2():
filename = tk.filedialog.askopenfilename(filetypes=ftypes)
pathlabel2.config(text=filename)

filename = filename.get()
return filename



#this line is just used to check that hard-coding in a filename works, which it does providing 'filename = tk.StringVar()' is removed
#filename = '...'


filename = tk.StringVar()

df = pd.read_csv(filename, encoding='latin-1')

browsebutton = tk.Button(self, borderwidth=0, text="Browse", command=browsefunc2, height=1, width=10)
browsebutton.pack()

pathlabel2 = tk.Label(self, borderwidth=0)
pathlabel2.pack()

canvas = FigureCanvasTkAgg(fig, self)


df.plot.scatter('a', 'b', ax=ax)

canvas.draw()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)


button2 = ttk.Button(self, text="Graph",
command=lambda: controller.show_frame(Graph))
button2.pack()

class Graph(tk.Frame):

def __init__(self, parent, controller):
self.controller = controller
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Graph", font=LARGE_FONT)
label.pack(pady=10,padx=10)

canvas = FigureCanvasTkAgg(fig, self)

#this line causes a problem as the dataframe is not recognised across frames
df.plot.scatter('a', 'b', ax=ax)

canvas.draw()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

button3 = ttk.Button(self, text="Back",
command=lambda: controller.show_frame(Home))
button3.pack()

app = GUI()
app.mainloop()

据我所知,无法将 .csv 文件上传到 StackOverflow,因此我重新创建了一个示例,但文件类型必须是 .csv。

a,b
1,10
2,32
3,23
4,5
5,4
6,66
7,7
8,19
9,31
10,44

最佳答案

我没有运行您的“简化”版本的代码,因为它绝不 Minimal, Complete, and Verifiable example .

该错误告诉您当它是 StringVar 时您假设它是路径或缓冲区。我相信错误在线:

df = pd.read_csv(filename, encoding='latin-1')

这要求 filename 是一个路径或缓冲区对象,而在上面的那一行 filename 确实是一个 StringVar 对象:

filename = tk.StringVar()

df = pd.read_csv(filename, encoding='latin-1')

为了获取 StringVar 或任何 Variable 子类类型的值,需要使用 get 方法。

filename.get()

但是,这会产生一个空字符串 '',这会引发另一个错误。

关于python - ValueError:无效的文件路径或缓冲区对象类型:<class 'tkinter.StringVar'>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49468065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com