Python 提供了 Pillow Package (PIL) 来处理和加载应用程序中的图像。可以使用内置的Image.open("image location")方法加载图像。此外,我们可以使用 Label 小部件在窗口中显示图像。
#Import tkinter library from tkinter import * from PIL import Image,ImageTk #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("750x550") #Load the image img= Image.open("nhooo.jpg") #Convert To photoimage tkimage= ImageTk.PhotoImage(img) #Display the Image label=Label(win,image=tkimage) label.pack() win.mainloop()输出结果
运行上面的代码将在窗口中显示图像。
在执行代码之前,请确保您在同一项目目录中有图像,或者提供图像位置的绝对路径。