为什么我和特金特的照片没有显示出来?

2024-04-27 04:06:39 发布

您现在位置:Python中文网/ 问答频道 /正文

Following this answer,我正试图得到一个要显示的图像,但是当我去运行它时(几乎和答案中的一样),窗口不会显示图像。你知道吗

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

Why is it not displaying?


Tags: 答案answerfrom图像imageimportpiltitle
1条回答
网友
1楼 · 发布于 2024-04-27 04:06:39

只需在引用的同一链接中跟随answer。 我在相关章节中添加了解释,以帮助您理解。您可以阅读有关ImageImageTkhere的更多信息。你知道吗

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

相关问题 更多 >