![](/img/trans.png)
[英]In Python, how can I open a file and read it on one line, and still be able to close the file afterwards?
[英]How to open an image in Python and close afterwards?
Python 版本:3.7.9
我试过将 Pillow/PIL 与 show() 和 close() 一起使用,但是这只会关闭命令提示符并使文件查看器仍然打开。
我试过 subprocess Popen 但这只是给我错误“系统找不到指定的文件”,即使文件是正确的。 所以我现在被困住了。 我认为这对 Python 来说是一个简单的过程。
我只是想打开一个图像,然后在用户点击一个按钮后,能够关闭它。
对建议/图书馆开放。 谢谢!
只需调用此函数,就会弹出一个带有图像的新窗口。 在窗口关闭之前,您的代码不会被执行。
import tkinter
from PIL import ImageTk, Image
def image_preview (path):
root = tkinter.Tk ()
root.title ("Preview")#Modify this to change the title
img = ImageTk.PhotoImage(Image.open(path))
panel = tkinter.Label (root, image = img)
panel.pack ()
root.mainloop ()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.