Python:如何使Pillow和Tkinter兼容?

问题描述 投票:0回答:1

我正在尝试运行一些使用tkinter和Pillow的Python代码。当我在IDE(Pycharm)中运行代码时,程序打开并且没有问题。但是,当我尝试将代码本身作为Python文件运行时,我会快速刷新命令提示符,该提示符会立即关闭。我正在运行Python 3.7.0(最新版本),这意味着我还拥有最新版本的tkinter以及Pillow 5.2.0。如何使用其作为独立python文件的UI运行我的代码?

from tkinter import *
from PIL import Image, ImageTk

root = Tk()

mapFrame=Frame(root)
mapFrame.place(relx=0, rely=0, relheight=0.85, relwidth=1)
subMapFrame = Frame(mapFrame)
subMapFrame.place(relx=0.05,rely=0.1,relwidth=0.9, relheight=0.9)

image = Image.open("field.png")
img_copy= image.copy()
background_image = ImageTk.PhotoImage(image)
background = Label(subMapFrame, image=background_image)
background.pack(fill=BOTH, expand=YES)

root.mainloop()
python tkinter python-imaging-library tk
1个回答
0
投票

将以下代码添加到python文件的末尾。

input('Press any key to exit')

除非按某些键,否则它将阻止命令提示立即关闭。

© www.soinside.com 2019 - 2024. All rights reserved.