Python tkinter使用变量和来自GUI的用户输入来运行代码

时间:2018-07-25 20:14:24

标签: python pandas file tkinter

INSERT INTO toplist( user_id, name, lastname, points )
SELECT O.user_id, name, lastname, SUM( points ) AS points
FROM userdata AS C, predictions AS O
WHERE O.lid =2020
AND C.user_id = O.user_id 
GROUP BY O.user_id

我正在尝试提取放入文本框的信息并运行以下代码,我想指定要程序读取的csv文件,并从该文件中找到我指定的值。我试图在熊猫阅读器中的文本框中进行硬编码,但是在将我在GUI中键入的字符串信息外在化时遇到了麻烦,并且使该字符串出现,因此它可以运行我的代码。我不确定我是否正确表达了我的问题。而当我运行它时,我得到了错误:

from tkinter import *
import pandas as pd

def retrieve_input():
    file = textBox.get("1.0","end-1c")
    basefileread = pd.read_csv(str(textBox) + '.csv', encoding='latin-1')
    basefilevalue = basefileread.loc[basefileread['Customer'].str.contains('Lam DepT', na=False),'Jun-18\nQty']


master = Tk()
master.geometry('200x100')

textBox = Text(master, height=2, width=10)
textBox.pack()

button1 = Button(master,text="Get Value", command=lambda: retrieve_input())
button1.pack()

master.mainloop()

print(int(textBox)+10)

1 个答案:

答案 0 :(得分:1)

问题出在retriece input()函数之内
您将文本检索到变量file中,但是使用str(textBox)来读取文件

解决方案应该像这样

def retrieve_input():
    file = textBox.get("1.0","end-1c")
    basefileread = pd.read_csv(file +'.csv', encoding='latin-1')
    basefilevalue = basefileread.loc[basefileread['Customer'].str.contains('Lam DepT', na=False),'Jun-18\nQty'