Reputation: 11
I currently have a python file that I have to run for different excel files on a daily basis.
The steps are :
This takes in a excel file as a pandas dataframe does some manipulation and other things and spits out an excel file.
The problem is that I have to change the directory each time manually in the code.
I would rather build a nice GUI for me to pick the source file that I want to manipulate, pick the output directory and then click begin to start the .py script.
My current .py file is not written as a function but it is just a series of steps on some data so I could easy write it as a function like so:
def data_automation(my_excel_file):
#do some stuff
pd.to_excel(output directory)
I currently have this:
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()
Which generates this:
So what I want to do is assign a function to the begin button which I can do with command=function easily.
What I am struggling to do is:
Given an input from the user, take that input and use the the file path as a function argument.
Be able to select an output destintion (currently I can only select a file not a destination) and then use that destination path to write my new excel file at the end of my function.
Appreciate any input!
Upvotes: 1
Views: 7940
Reputation: 16169
Connect a function to your 'Begin' button that gets the content of both your entries and runs data_automation()
. Something like
def begin():
my_excel_file = input_entry.get()
output_directory = output_entry.get()
data_automation(my_excel_file, output_directory)
I have added the output_directory
argument to your data_automation
function.
If you use filedialog.askdirectory()
instead of filedialog.askopenfilename()
, you will be able to pick a directory instead of a file. By the way there is a typo in output()
, I think you want to insert the result in output_entry
, not input_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'
Upvotes: 1