简体   繁体   中英

How to open an image in Python and close afterwards?

Python Versions: 3.7.9

I've tried using Pillow/PIL with show() and close() however that just closes the command prompt and leaves the file viewer still open.

I've tried subprocess Popen but that just gives me the error 'The system cannot find the file specified' even though the file is correct. So I'm just stuck right now. I figured this would've been a simple process for Python.

I'm just trying to open an image and then after the user clicks a button, be able to close it.

Open to suggestions/libraries. Thanks!

Just call this function and a new window will pop up with the image. Your code wont be executed until the window is closed.

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 ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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