0

I have written DataCheck function in data_check.py for data manipulation of many excel files, which has code structure as follows:

import pandas as pd

def DataCheck(filePath):
    df = pd.read_excel(filePath)
    try:
        df = df.dropna(subset=['building', 'floor', 'room'], how = 'all')
        ...
        ...
        ...
        df.to_excel(writer, 'Sheet1', index = False)
    
if __name__ == '__main__':
    status = True
    while status:
        rawPath = input(r"")
        filePath = rawPath.strip('\"')
        if filePath.strip() == "":
            status = False
        DataCheck(filePath)

For the next step I want to repetively browser excel files and pass their paths to DataCheck using tkinter, the code below named main_app.py works only for one file then need to restart main_app.py for another file.

Option 1:

import tkinter as tk
from data_check import DataCheck
from tkinter import filedialog
import os

root = tk.Tk()
root.withdraw()

def open_file(path):
    
    curr_dir = os.getcwd()
    temp_dir = filedialog.askopenfilename(parent = root, initialdir = curr_dir, title = 'select file')
    return temp_dir

if __name__ == '__main__':
    status = True
    while status:
        DataCheck(path)

Option 2:

import tkinter as tk
from data_check import DataCheck
from tkinter import filedialog
import os

root = tk.Tk()
root.withdraw()
    
curr_dir = os.getcwd()
root.update()
temp_dir = filedialog.askopenfilename(parent = root, initialdir = curr_dir, title = 'select file')

if __name__ == '__main__':
      DataCheck(path)

How could I modify and improve the code above? Sincere thanks.

ah bon
  • 9,293
  • 12
  • 65
  • 148
  • `main_app.py` will raise `NameError: name 'path' is not defined`. – acw1668 Nov 17 '20 at 09:25
  • `path ` should be `temp_dir`? – ah bon Nov 17 '20 at 09:27
  • The main problem is tkinter will close after execution of one excel file and need to reopen for another file, I try add `root.update()` as https://stackoverflow.com/questions/32217114/tkinter-askopenfilename-wont-close, but not work. – ah bon Nov 17 '20 at 09:29

1 Answers1

1

You should use a button to execute a function which selects a file using askopenfilename() and execute DataCheck() on the selected file as below:

import tkinter as tk
from tkinter import filedialog, messagebox
from data_check import DataCheck

def on_quit():
    if messagebox.askokcancel('Confirmation', 'Do you really want to quit?'):
        root.destroy()

def open_file():
    file = filedialog.askopenfilename(filetypes=(('Excel Files', '.xlsx'),))
    if file:
        DataCheck(file)

root = tk.Tk()

btn = tk.Button(root, text='Select file', command=open_file)
btn.pack()

root.protocol('WM_DELETE_WINDOW', on_quit)
root.mainloop()

Then you can repeatedly select files.


Edit: added calling askokcancel() on closing window.

acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Thanks a lot for your help, one more thing I want to ask if it's possible: how could I add `messagebox.askokcancel()` before closing the tkinter box? – ah bon Nov 17 '20 at 10:15
  • `filedialog.askopenfilename(filetypes=[("Excel files", ".xlsx .xls")])` – ah bon Nov 17 '20 at 10:18