尝试使用TKINTER将Seaborn图绘制到GUI

时间:2019-01-16 15:47:39

标签: python-3.x user-interface tkinter seaborn

因此,我试图在用户用数字值输入CSV文件的文件路径后,让GUI显示统计图。在下面的代码中,我已经指定了我一直在使用的测试文件的值的名称,但是我计划也将其更改为用户输入。我的问题是我无法使其正常工作。当我在下面运行代码时,出现此错误:

ValueError: Invalid file path or buffer object type: <class 'tkinter.StringVar'>

我的尝试

import pandas as pd
import seaborn as sns
import tkinter
from tkinter import ttk
window = Tk()
window.geometry("600x600")
window.title("Visual data")
label_one = ttk.Label(window, text = "Enter your filepath")
label_one.grid(row = 0, column = 0)
label_two = ttk.Label(window, text = "Your file path is")
label_two.grid(row = 1, column = 1)
user_path = tkinter.StringVar()
user_entry = ttk.Entry(window, width = 26, textvariable = user_path)
user_entry.grid(row = 0, column = 1)
y_axis = "age"
x_axis = "errors_time1"
def barplot_function(dataset, name1, name2):
    sns.set(color_codes=True)
    dataset = pd.read_csv(dataset)
    dataset.head()
    plot = sns.barplot(x=name1, y=name2, data=dataset)
    outbox.insert(plot)
def action():
    label_two.configure(text = "Your file path is " + user_path.get())
    barplot_function(user_path, y_axis, y_axis)
button1 = ttk.Button(window, text = "submit", command = action)
button1.grid(row = 0, column = 2)

outbox = Canvas(window, width = 200, height = 200, bg = "light grey")
outbox.grid(row = 3, column = 0, sticky = W)
window.mainloop()

这是我第一次尝试使用GUI,因此非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试

 ...
    barplot_function(user_path.get(), y_axis, y_axis)
    ...

user_path中检索StringVar的值。