简体   繁体   English

如何在 eel python 中获取用户输入的图像?

[英]How to take image input from user in eel python?

I am using eel library to create GUI.我正在使用 eel 库来创建 GUI。 I can't get the path using <input type="file"> tag because of security reasons.由于安全原因,我无法使用<input type="file">标签获取路径。 So please tell me a way to get the image and display it in eel interface.所以请告诉我一种获取图像并在鳗鱼界面中显示的方法。 I show several questions related to this but I didn't find any full answer.我展示了几个与此相关的问题,但我没有找到任何完整的答案。

<body>
    <button type="button" onclick="getPathToFile()">Select File</button>
    <img id="myImg" src="" width="107" height="98">
</body>

solution:解决方案:

You have to use tkinter's file dialog to get files您必须使用 tkinter 的文件对话框来获取文件

Explanation:解释:

Browsers include a lot of security measures to protect user's data.浏览器包含许多保护用户数据的安全措施。 Since you are on a local server ( eel ) there are some things you may have to do with python like getting files on the system.由于您在本地服务器( eel )上,因此您可能需要与 python 做一些事情,例如在系统上获取文件。

example:例子:

# ==> 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

to call it:称之为:

// ==> JAVASCRIPT

async function example(){
    let file = await eel.getFile()()
}

Go further: Go 进一步:

  1. For more info: filedialog docs更多信息: filedialog 文档
  2. How an app made with eel ( auto-py-to-exe ) did it: GitHub使用 eel ( auto-py-to-exe ) 制作的应用程序是如何做到的: GitHub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM