Python:如何使Pillow和Tkinter兼容?

时间:2018-09-05 01:30:27

标签: python tkinter python-imaging-library tk

我正在尝试运行一些同时使用tkinter和Pillow的Python代码。当我在IDE(Pycharm)中运行代码时,程序将打开,并且没有任何问题。但是,当我尝试以Python文件形式单独运行代码时,会快速看到Command提示符,该提示符立即关闭。我正在运行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()

1 个答案:

答案 0 :(得分:0)

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

input('Press any key to exit')

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