没有合适的资源?快使用搜索试试~ 我知道了~
通过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 w
资源详情
资源评论
资源推荐
详解详解python tkinter 图片插入问题图片插入问题
通过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)
weixin_38647925
- 粉丝: 2
- 资源: 913
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于matlab实现用有限元法计算电磁场的Matlab工具 .rar
- 基于matlab实现有限元算法 计算电磁场问题 边界条件包括第一类边界和第二类边界.rar
- 基于matlab实现用于计算不同车重下的电动汽车动力性和经济性.rar
- 基于matlab实现遗传算法求解多车场车辆路径问题 有多组算例可以用.rar
- 浏览器.apk
- 基于matlab实现是一个matlab中的power system 中搭建的一个模型
- 基于JSP毕业设计-教学管理系统(源代码+论文).zip
- 基于JSP毕业设计-家政管理系统-毕业设计.zip
- 基于Python实现淘宝商品评论采集(含逆向)源代码
- 基于matlab实现多目标进化算法NSGAⅡ&Matlab讲解.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0