通过tkinter.PhotoImage插入GIF, PGM/PPM格式的图片。
import tkinter
class Gui:
def __init__(self):
self.gui=tkinter.Tk() # create gui window
self.gui.title("Image Display") # set the title of gui
self.gui.geometry("800x600") # set the window size of gui
img = tkinter.PhotoImage(file="C:/Users/15025/Desktop/bear.gif") # read image from path
label1=tkinter.Label(self.gui,image=img) # create a label to insert this image
label1.grid() # set the label in the main window
self.gui.mainloop() # start mainloop
main = Gui()
注意: img = tkinter.PhotoImage(file="C:/Users/15025/Desktop/bear.gif") 中的关键字file不能够省略,否则程序无法正常显示图片。
对于常用的PNG,与JPG格式的图片,我们需要从python image library(pillow)(PIL)导入Image与ImageTk模块来实现,代码如下:
import tkinter
from PIL import Ima