python的tkinter插入图片_详解python tkinter 图片插入问题

本文详细介绍了如何在Python的tkinter库中插入和管理图片,包括使用tkinter.PhotoImage加载GIF和PGM/PPM格式的图片,以及使用PIL库处理PNG和JPG格式的图片。在实际操作中,可能会遇到图片无法显示的问题,解决办法是保存对图片对象的引用,以防止被Python自动回收。最后讨论了一个自适应图片大小的GUI设计,以及在不同情况下启动主循环的方法。
摘要由CSDN通过智能技术生成

通过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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值