python - 需要从函数返回文件路径并将其作为参数传递给另一个函数
问题描述
所选文件的路径(存储在变量中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) 项目详情
解决方案
这是一种方法:
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
将是全局的,您可以在需要的任何地方引用它
推荐阅读
- reactjs - 存根运行承诺的函数
- python - 如何在 Python statsmodels adfuller 中为 maxlag 参数选择一个值?
- css - 删除 d3 表中的空格
- python - 如何让我的代码显示文本文件中的一定数量的字符
- json - 从 JSON 执行 Groovy 脚本
- c# - 如何在两个不相邻的区域上画一个字符串
- node.js - Web 驱动程序 - 如何覆盖 Microsoft Edge 的用户代理
- php - Laravel 数据库迁移用户可能来自其他类型
- java - 如何将带有参数的https post请求发送到api
- android - 如何使用 contentObserver 和 contentResolver 从 Messages 中读取已发送短信的内容?