Python Tkinter askopenfilename路径

Python Tkinter askopenfilename路径,python,tkinter,Python,Tkinter,我正在尝试构建一个Tkinter UI,其中包含一个按钮,用户可以在该按钮中选择一个主文件,并根据该文件中的信息,脚本将获得一些有关辅助文件的信息。在选择主文件时,我尝试使用主文件的文件路径来获取次文件的文件路径。但是,由于我两次调用函数“open_main_file()”,因此会出现两个对话框,用户必须在其中两次而不是一次选择主文件。我怎样才能避免这种情况 我的代码如下所示: import pandas as pd from tkinter import * from os.path impo

我正在尝试构建一个Tkinter UI,其中包含一个按钮,用户可以在该按钮中选择一个主文件,并根据该文件中的信息,脚本将获得一些有关辅助文件的信息。在选择主文件时,我尝试使用主文件的文件路径来获取次文件的文件路径。但是,由于我两次调用函数“open_main_file()”,因此会出现两个对话框,用户必须在其中两次而不是一次选择主文件。我怎样才能避免这种情况

我的代码如下所示:

import pandas as pd
from tkinter import *
from os.path import dirname
from tkinter import filedialog

root = Tk()

def open_main_file():
    return filedialog.askopenfilename()

def parse_main_file():
    return pd.read_parquet(open_main_file(), engine='pyarrow')

def get_some_ids():
    return parse_main_file()['some_ids'].iloc[0]

def get_list_of_other_files():
    other_files_path = dirname(dirname(dirname(open_main_file())))
    list_of_other_files = []
    for f in get_some_ids():
        list_of_other_files.append(glob.glob(other_files_path + '/identifier=' + str(f) + '/*'))
    return map(''.join, list_of_other_files)

myButton = Button(root, text='Open File...', command=get_list_of_other_files).pack()
root.mainloop()
import pandas as pd
from tkinter import *
from os.path import dirname
from tkinter import filedialog

root = Tk()

def open_main_file(): # This function is not necessary
    return filedialog.askopenfilename()

def parse_main_file(main_file_path):
    return pd.read_parquet(main_file_path, engine='pyarrow')

def get_some_ids(main_file_path):
    return parse_main_file(main_file_path)['some_ids'].iloc[0]

def get_list_of_other_files():
    main_file_path = filedialog.askopenfilename()
    other_files_path = dirname(dirname(dirname(main_file_path)))
    list_of_other_files = []
    for f in get_some_ids(main_file_path):
        list_of_other_files.append(glob.glob(other_files_path + '/identifier=' + str(f) + '/*'))
    return map(''.join, list_of_constituent_files)

myButton = Button(root, text='Open File...', command=get_list_of_other_files).pack()
root.mainloop()

尽量不要在其他函数中使用函数。在本例中,您在另一个函数中调用一次函数,在主代码中调用一次函数。这就是为什么会出现两次文件对话框。使用返回的项并将其交给下一个函数。您可以这样编写代码:

import pandas as pd
from tkinter import *
from os.path import dirname
from tkinter import filedialog

root = Tk()

def open_main_file():
    return filedialog.askopenfilename()

def parse_main_file():
    return pd.read_parquet(open_main_file(), engine='pyarrow')

def get_some_ids():
    return parse_main_file()['some_ids'].iloc[0]

def get_list_of_other_files():
    other_files_path = dirname(dirname(dirname(open_main_file())))
    list_of_other_files = []
    for f in get_some_ids():
        list_of_other_files.append(glob.glob(other_files_path + '/identifier=' + str(f) + '/*'))
    return map(''.join, list_of_other_files)

myButton = Button(root, text='Open File...', command=get_list_of_other_files).pack()
root.mainloop()
import pandas as pd
from tkinter import *
from os.path import dirname
from tkinter import filedialog

root = Tk()

def open_main_file(): # This function is not necessary
    return filedialog.askopenfilename()

def parse_main_file(main_file_path):
    return pd.read_parquet(main_file_path, engine='pyarrow')

def get_some_ids(main_file_path):
    return parse_main_file(main_file_path)['some_ids'].iloc[0]

def get_list_of_other_files():
    main_file_path = filedialog.askopenfilename()
    other_files_path = dirname(dirname(dirname(main_file_path)))
    list_of_other_files = []
    for f in get_some_ids(main_file_path):
        list_of_other_files.append(glob.glob(other_files_path + '/identifier=' + str(f) + '/*'))
    return map(''.join, list_of_constituent_files)

myButton = Button(root, text='Open File...', command=get_list_of_other_files).pack()
root.mainloop()

@这是不正确的。如果您想使用Filedialog和其他类似ttk Separate的工具,您需要导入它们。当我这样做时,我会得到“NameError:name'Filedialog'未定义”,好像它没有得到imported@Atlas435哦,对不起,你说得对。我的badJust a提示:
myButton=Button(root,text='Open File…',command=get\u list\u of其他文件)。pack()
myButton
设置为
None
,因为方法
pack
不返回任何内容。我建议删除该方法,然后使用
myButton.pack()
调用该方法,以避免将来出现错误。因此,请尝试与变量名保持一致,并将
myButton
更改为
myu按钮
,这样才有效。我理解你在回答中的观点,但你能引导我找到一个链接,在那里我能理解为什么它是这样工作的吗?在函数中的参数如何工作方面,我想我缺少了一些基本的东西,经过数小时的文件和结构检查后,我看不清楚。@bloo检查此链接:@FarZadAsG谢谢。Atlas435在您的示例代码中,错误是“def get_list_of_other_files():file=open_main_file()”,而FarZadAsG的错误是“def get_list of_other_files():main_file_path=filedialog.askopenfilename()”,这正是我使用函数作为变量值时犯的错误。我想我现在知道问题是什么了@在上面的工作示例中,FarZadAsG甚至不需要def open_main_file()。只需将filedialog.askopenfilename()分配给right函数下的变量即可。@bloo是的,您是对的。这是没有必要的。我只是想保留代码的整个结构。正如您所看到的,我甚至没有在代码后面使用该函数。然后键入:
main\u file\u path=filedialog.askopenfilename()