Python 为什么不是';我的图像不能用Tkinter显示吗?
,我试图显示一个图像,但当我运行它时(几乎与答案中的一样),窗口不会显示图像Python 为什么不是';我的图像不能用Tkinter显示吗?,python,python-3.x,image,tkinter,jpeg,Python,Python 3.x,Image,Tkinter,Jpeg,,我试图显示一个图像,但当我运行它时(几乎与答案中的一样),窗口不会显示图像 from PIL import Image import tkinter window = tkinter.Tk() window.title("Join") window.geometry("300x300") window.configure(background='grey') imageFile = "/Users/glennsha/Desktop/APCS_Create/Rank_icons/champio
from PIL import Image
import tkinter
window = tkinter.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
imageFile = "/Users/glennsha/Desktop/APCS_Create/Rank_icons/champion1.jpg"
window.im1 = Image.open(imageFile)
input()
window.mainloop()
只需在您引用的同一链接中单击。
我在相关章节中添加了解释,以帮助您理解。您可以阅读有关Image
和ImageTk
的更多信息
您复制了问题格式,但没有使用该问题答案中的
img=ImageTk.PhotoImage(Image.open(path))
。尝试将此部分添加到代码中,以及将实际图像小部件添加到框架中。那么,您从一个工作示例中删除了代码,但现在它不工作了?你是否考虑过可能删除了一些重要的部分?你应该编辑你的文件路径并离开OPS文件路径。没有理由同时显示这两个。@Mike SMT感谢您的提醒。用于测试。忘了移除。
from PIL import Image, ImageTk # I have added the import of ImageTk
import tkinter
window = tkinter.Tk()
window.title("Join")
window.geometry("300x300")
window.configure(background='grey')
imageFile = "/Users/glennsha/Desktop/APCS_Create/Rank_icons/champion1.jpg"
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
im1 = ImageTk.PhotoImage(Image.open(imageFile))
#Next, you need to put your image into a widget before it can be visible.
# Your reference answer used a Label widget. We will use the same here.
# This Label widget is a child of "window" which is the Tk() window.
panel = tkinter.Label(window, image = im1)
#Next you need to put the widget into the Tk() window before the widget can be made visible.
# Here, the Pack geometry manager is used to put/locate the widget containing
# the images into the Tk() Window.
panel.pack(side = "bottom", fill = "both", expand = "yes")
window.mainloop()