通过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 Image from PIL import ImageTk 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 load = Image.open("C:/Users/15025/Desktop/1.png") # open image from path img = ImageTk.PhotoImage(load) # read opened image 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()
然而在实际操作中,本人使用的是Anaconda spyder编译器,当我们在读入图像时程序出错后,再次运行程序就会导致image "pyimage1" doesn't exist错误,每次运行一次,数字就会增加1,如:image "pyimage2" doesn't exist。遇到此错误,可以直接在IPython控制台界面重启IPython内核即可,或者关闭编译器并重新打开。
看似我们已经完全实现了图片的插入,但是这种插入方法是存在隐患的,具体代码如下:
import tkinter as tk from PIL import Image from PIL import ImageTk class Gui(tk.Tk): def __init__(self): super().__init__() self.title("Figure dynamic show v1.01") # self.geometry("1000x800+400+100") self.mainGui() # self.mainloop() def mainGui(self): image = Image.open("C:/Users/15025/Desktop/1.png") photo = ImageTk.PhotoImage(image) label = tk.Label(self, image=photo) label.image = photo # in case the image is recycled label.grid() main = Gui() main.mainloop()
这里我们可以看到相比较上面的程序,我们将Gui界面的图像插入部分分离到另一个函数中,并且直接定义一个tkinter的类,这样做的好处是我们可以直接用self替代创建的主窗口界面,并且我们可以在不同的地方启动主循环,self.mainloop()和main.mainloop()任选一个即可。并且因为我们想要插入图片,所以我们可以省略指定Gui界面的尺寸,这样做的好处是会创建一个自适应图片大小的Gui界面。最重要的是我们可以看到多了一行代码label.image = photo,我们将选取的图片photo赋值给了label的属性对象image,如果没有这一行代码,图片便无法正常显示,这是因为python会自动回收不使用的对象,所以我们需要使用属性对象进行声明。 上述的程序隐患便是因为缺少了这一行代码。
至此,tkinter的图片插入可暂时告一段落。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]