我正在使用eel库来创建GUI。由于安全原因,我无法使用<input type="file">标签获取路径。所以请告诉我一种获取图像并将其显示在eel界面中的方法。我展示了几个与此相关的问题,但我没有找到任何完整的答案。
<body>
<button type="button" onclick="getPathToFile()">Select File</button>
<img id="myImg" src="" width="107" height="98">
</body>发布于 2021-01-08 03:57:07
解决方案:
你必须使用tkinter的文件对话框来获取文件
解释:
浏览器包含很多安全措施来保护用户的数据。由于您使用的是本地服务器( python、eel、),因此使用python可能需要做一些事情,比如获取系统上的文件。
示例:
# ==> PYTHON
from tkinter import filedialog, Tk, messagebox
import os
@eel.expose
def getFile ():
root = Tk()
root.withdraw()
root.wm_attributes('-topmost', 1) # without that it wil go behind all others apps
file = filedialog.askopenfilename(parent=root,
initialdir=os.getcwd(),
title = "Ouvrez un fichier pickle",
filetypes = (
("images","*.png"),
("all files","*.*")
)
)
return file称其为:
// ==> JAVASCRIPT
async function example(){
let file = await eel.getFile()()
}更进一步:
更多信息:filedialog docs
,
https://stackoverflow.com/questions/65073903
复制相似问题