Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用tkinter循环excel文件的路径,并将其传递给Python中的数据操作函数_Python 3.x_For Loop_Tkinter_While Loop - Fatal编程技术网

Python 3.x 使用tkinter循环excel文件的路径,并将其传递给Python中的数据操作函数

Python 3.x 使用tkinter循环excel文件的路径,并将其传递给Python中的数据操作函数,python-3.x,for-loop,tkinter,while-loop,Python 3.x,For Loop,Tkinter,While Loop,我在data_check.py中编写了DataCheck函数,用于对许多excel文件进行数据操作,其代码结构如下: import pandas as pd def DataCheck(filePath): df = pd.read_excel(filePath) try: df = df.dropna(subset=['building', 'floor', 'room'], how = 'all') ... ...

我在data_check.py中编写了DataCheck函数,用于对许多excel文件进行数据操作,其代码结构如下:

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)
在下一步中,我想使用tkinter重复浏览excel文件并将其路径传递给DataCheck,下面名为main_app.py的代码仅适用于一个文件,然后需要重新启动另一个文件的main_app.py

备选案文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)
备选案文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)

如何修改和改进上述代码?衷心感谢。

您应该使用一个按钮来执行一个函数,该函数使用askopenfilename选择一个文件,并对所选文件执行数据检查,如下所示:

将tkinter作为tk导入 从tkinter导入文件对话框的messagebox 从数据检查导入数据检查 def on_退出: 如果messagebox.askokcancel“确认”,“是否确实要退出?”: 毁灭 def open_文件: file=filedialog.askopenfilenamefiletypes='Excel Files','.xlsx', 如果文件: 数据检查文件 root=tk.tk btn=tk.Buttonroot,text='Select file',command=open\u file btn.pack root.protocol'WM_DELETE_WINDOW',在退出时 root.mainloop 然后可以重复选择文件


编辑:在关闭窗口时添加了调用askokcancel。

main\u app.py将引发name错误:未定义名称“path”。path应为temp\u dir?主要问题是tkinter将在执行一个excel文件后关闭,需要重新打开另一个文件,我尝试添加root.update as,但无法工作。非常感谢您的帮助,我还想问一件事,如果可能的话:在关闭tkinter框之前如何添加messagebox.askokcancel?filedialog.askopenfilenamefiletypes=[Excel文件,.xlsx.xls]