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()
答案 0 :(得分:1)
只需点击您引用的同一链接中的answer即可。
我在相关章节中添加了解释,以帮助您理解。您可以详细了解Image
和ImageTk
here。
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()