我对python编程很新,这是我提出的第一个问题,所以要对我很温柔。现在我正在开发一个读取CSV文件的tkinter GUI,将它们转换为数据帧进行编辑,并将处理后的文件保存到数据路径中的result.csv中。我的问题是我想要用一个连接到一个按钮的功能OpenFile()读取CSV,然后在我的"编辑功能"中使用返回的数据帧。每个也连接到一个按钮。这似乎不适用于代码中的回调。以下是我的tkinter GUI目前的样子:
root = tk.Tk()
openbutton = tk.Button (root, text='OpenFile', command = lambda: OpenFile())
openbutton.pack()
brakebutton = tk.Button (root, text = 'EditFile', command = lambda: EditFile(z))
brakebutton.pack()
savenewbutton = tk.Button(root, text = 'SavetoNewFile', command = lambda: SaveinNewFile(out_tup_end3))
savenewbutton.pack()
saveexcbutton = tk.Button(root, text = 'AppendtoExistingFile', command = lambda: SavetoexistingFile(out_tup_end3))
saveexcbutton.pack()
root.mainloop()
我的功能基本上是这样的:
def OpenFile():
name = askopenfilename()
df = pd.read_csv(name, sep = ';')
return df
def EditFile(z):
df1 = z[[]]
.
.
.
.
return out_tup_end3
def SaveinNewFile(out_tup_end3):
out_tup_end3.to_csv("datapath", sep = ';', index = 0, mode = 'w')
def SavetoexistingFile(out_tup_end3):
with open("datapath", 'a') as f:
out_tup_end3.to_csv(f, header=False, sep = ';', index = 0)
def main():
z = OpenFile()
EditFile(z)
editedFile = EditFile(z)
SaveinNewFile(editedFile)
SavetoexistingFile(editedFile)
if __name__ == "__main__":
main()
所以我认为不正常工作的部分是回调没有将从OpenFile()读取的dataFrame移交给EditFile(z)。我怎样才能实现这一目标? 感谢您的帮助!!
答案 0 :(得分:0)
command=
执行函数但它并不关心从此函数返回的值,因此您必须使用global
变量来使用其他函数中的一个函数的结果。
您的代码可能如下所示
#!/usr/bin/env python3
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showinfo, showwarning
import pandas as pd
# --- functions ---
def open_file():
# inform function to use global variables
global df
name = askopenfilename()
if name:
df = pd.read_csv(name, sep=';')
showinfo("INFO", "DataFrame created")
def edit_file():
# inform function to use global variables
global df, out_tup_end3
if df is None:
#print("Read file first")
showwarning("WARNING", "Read file first")
else:
df1 = df[[]]
# ...
out_tup_end3 = df1
showinfo("INFO", "DataFrame edited")
def save_in_new_file():
# inform function to use global variables
global out_tup_end3
if out_tup_end3 is None:
#print("Read file first")
showwarning("WARNING", "Read file first")
else:
out_tup_end3.to_csv("datapath", sep=';', index=0, mode='w')
showinfo("INFO", "DataFrame saved")
def save_to_existing_file():
# inform function to use global variables
global out_tup_end3
if out_tup_end3 is None:
#print("Read file first")
showwarning("WARNING", "Read file first")
else:
with open("datapath", 'a') as f:
out_tup_end3.to_csv(f, header=False, sep=';', index=0)
showinfo("INFO", "DataFrame appended")
# --- main ---
if __name__ == "__main__":
# create global variables
df = None
out_tup_end3 = None
# GUI
root = tk.Tk()
b = tk.Button(root, text='Open File', command=open_file)
b.pack(fill='x')
b = tk.Button(root, text='Edit File', command=edit_file)
b.pack(fill='x')
b = tk.Button(root, text='Save to New File', command=save_in_new_file)
b.pack(fill='x')
b = tk.Button(root, text='Append to Existing File', command=save_to_existing_file)
b.pack(fill='x')
root.mainloop()
或了解如何使用课程和self.
代替global