How to use tkinter GUI to select function argument and file output path in python?

拈花ヽ惹草 提交于 2021-02-20 04:14:49

问题


I currently have a python file that I have to run for different excel files on a daily basis.

The steps are :

  1. Open .py file
  2. change directory of excel file
  3. run python file
  4. write to .xlsx

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:

tk_output

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:

  1. Given an input from the user, take that input and use the the file path as a function argument.

  2. 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!


回答1:


  1. 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.

  2. 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'
    


来源:https://stackoverflow.com/questions/57664964/how-to-use-tkinter-gui-to-select-function-argument-and-file-output-path-in-pytho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!