Python:如何使枕头和Tkinter兼容?

Python:如何使枕头和Tkinter兼容?,python,tkinter,python-imaging-library,tk,Python,Tkinter,Python Imaging Library,Tk,我正在尝试运行一些同时使用tkinter和Pillow的Python代码。当我在IDE(Pycharm)中运行代码时,程序会打开,没有问题。然而,当我尝试将代码作为Python文件单独运行时,我会得到一个命令提示符的快速闪烁,它会立即关闭。我运行的是Python3.7.0(最新版本),这意味着我还拥有tkinter的最新版本,以及Pillow 5.2.0。如何让代码作为独立的python文件在UI中运行 from tkinter import * from PIL import Image, I

我正在尝试运行一些同时使用tkinter和Pillow的Python代码。当我在IDE(Pycharm)中运行代码时,程序会打开,没有问题。然而,当我尝试将代码作为Python文件单独运行时,我会得到一个命令提示符的快速闪烁,它会立即关闭。我运行的是Python3.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文件的末尾

input(“按任意键退出”)


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

我尝试在root.mainloop行之前和之后添加代码行。仍然无法打开UI。@KeenanB您能在
mapFrame=Frame(root)
中定义宽度和高度吗?我尝试更改两个框架,以便将宽度和高度定义为:mapFrame=Frame(root,width=150,height=150)mapFrame.place(x=0,y=0)Submap Frame=Frame(mapFrame,width=100,height=100)Submap Frame.place(x=0,y=0)你是否检查过图像是否存在。因为如果我将图像路径更改为“不存在”,提示会立即关闭。当您双击并运行时,您是通过pc上安装的python而不是pycharm运行它。您还需要为python安装pip-install-pillow。检查的一个好方法是右键单击py文件,单击editbyide,然后运行它。