首页 > 解决方案 > 需要从函数返回文件路径并将其作为参数传递给另一个函数

问题描述

所选文件的路径(存储在变量中source_file)必须从返回browseFiles()并作为参数传递给 objdetect()

def browseFiles():
   source_file = filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetypes =[('All Files', '.*')],parent=window)
   label_file_explorer.configure(text=""+source_file)

Python 3.8 (Tkinter) 项目详情

标签: pythonpython-3.xtkinter

解决方案


这是一种方法:

def browseFiles():
   source_file = filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetypes =[('All Files', '.*')],parent=window)
   label_file_explorer.configure(text=""+source_file)
   return source_file


def objdetect()
    file = browseFiles()

基本上只是使用return语句返回一个值,然后将该函数分配给一个变量,它将被调用并将返回的值分配给变量

如果你只是想拥有一个全局值,你可以这样做:

def browseFiles():
   global source_file
   source_file = filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetypes =[('All Files', '.*')],parent=window)
   label_file_explorer.configure(text=""+source_file)

现在变量source_file将是全局的,您可以在需要的任何地方引用它


推荐阅读