如何使用tkinter GUI在python中选择函数参数和文件输出路径?
我目前有一个python文件,每天都要为不同的excel文件运行 这些步骤是:如何使用tkinter GUI在python中选择函数参数和文件输出路径?,python,python-3.x,user-interface,tkinter,Python,Python 3.x,User Interface,Tkinter,我目前有一个python文件,每天都要为不同的excel文件运行 这些步骤是: 打开.py文件 更改excel文件的目录 运行python文件 写入.xlsx 当数据框执行一些操作和其他操作时,它接收excel文件,并输出excel文件 问题是我每次都必须在代码中手动更改目录 我宁愿为我构建一个漂亮的GUI来选择我想要操作的源文件,选择输出目录,然后单击开始启动.py脚本 我当前的.py文件不是作为函数编写的,但它只是一些数据上的一系列步骤,因此我可以轻松地将其作为函数编写,如下所示: def
def data_automation(my_excel_file):
#do some stuff
pd.to_excel(output directory)
我目前有:
import tkinter.filedialog as filedialog
import tkinter as tk
master = tk.Tk()
def input():
input_path = tk.filedialog.askopenfilename()
input_entry.delete(1, tk.END) # Remove current text in entry
input_entry.insert(0, input_path) # Insert the 'path'
def output():
path = tk.filedialog.askopenfilename()
input_entry.delete(1, tk.END) # Remove current text in entry
input_entry.insert(0, path) # Insert the 'path'
top_frame = tk.Frame(master)
bottom_frame = tk.Frame(master)
line = tk.Frame(master, height=1, width=400, bg="grey80", relief='groove')
input_path = tk.Label(top_frame, text="Input File Path:")
input_entry = tk.Entry(top_frame, text="", width=40)
browse1 = tk.Button(top_frame, text="Browse", command=input)
output_path = tk.Label(bottom_frame, text="Output File Path:")
output_entry = tk.Entry(bottom_frame, text="", width=40)
browse2 = tk.Button(bottom_frame, text="Browse", command=output)
begin_button = tk.Button(bottom_frame, text='Begin!')
top_frame.pack(side=tk.TOP)
line.pack(pady=10)
bottom_frame.pack(side=tk.BOTTOM)
input_path.pack(pady=5)
input_entry.pack(pady=5)
browse1.pack(pady=5)
output_path.pack(pady=5)
output_entry.pack(pady=5)
browse2.pack(pady=5)
begin_button.pack(pady=20, fill=tk.X)
master.mainloop()
这就产生了:
所以我想做的是给开始按钮分配一个函数,我可以用command=function轻松地完成
我正在努力做的是:
data\u automation()
。差不多
def begin():
my_excel_file = input_entry.get()
output_directory = output_entry.get()
data_automation(my_excel_file, output_directory)
我已将output\u directory
参数添加到data\u automation
函数中filedialog.askdirectory()
而不是filedialog.askopenfilename()
,则可以选择目录而不是文件。顺便说一下,output()
中有一个输入错误,我想您应该将结果插入output\u entry
,而不是input\u entry
def output():
path = tk.filedialog.askdirectory()
output_entry.delete(1, tk.END) # Remove current text in entry
output_entry.insert(0, path) # Insert the 'path'