图像未出现在 Tkinter 上 [英] Image does not appear on Tkinter

查看:45
本文介绍了图像未出现在 Tkinter 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Tkinter 有问题,因为我想添加一个图像作为框架的背景,但是,我尝试了很多东西,但没有任何显示.我在我的代码的开头,我会在我克服那个问题后继续前进.

l have an problem with Tkinter as l would like to add an image as background of frame ,however ,l tried many things but nothing show up. l m at the beginning of my code and l will move on after l overcome that problem.

这是我的代码:

import Tkinter
from Tkinter import *

sc=Tk()
sc.title("Matplotlib")
sc.geometry("500x500")
img=PhotoImage("mat.png")

fr1=Frame(sc,height=200,bd=5,bg="red",relief=SUNKEN);fr1.pack(side=TOP,fill=X,expand=1)
fr2=Frame(sc,height=200,bd=5,relief=SUNKEN);fr2.pack(fill=X,expand=1)
fr3=Frame(sc,height=200,bd=5,relief=SUNKEN);fr3.pack(side=BOTTOM,fill=X,expand=1)
label1=Label(fr2,image=img);label1.pack(fill=BOTH)

mainloop()

我该如何解决?或者我不想使用任何其他模块,因为我愿意使用 Tkinter 来构建我的代码

how can l solve it? or l do not want to use any other module if possible as l m willing to use Tkinter for structure of my code

推荐答案

问题是您的文件名没有被视为图像的文件名.第一个非关键字参数用作图像的内部名称.

The problem is that your filename isn't being treated as the filename of the image. The first non-keyword argument is used as the internal name of the image.

您必须指定 file 关键字参数才能将文件用作图像:

You must specify the file keyword argument for it to use the file as the image:

img=PhotoImage(file="mat.png")

此外,根据您安装的版本,tkinter 可能不支持 png 文件.如果您的系统没有,通过上述更改,您将收到类似 TclError: 无法识别图像数据 的错误.如果是这种情况,您需要将图像转换为 GIF.

Also, depending on what version you have installed, tkinter may not support png files. If your system does not, with the above change you'll get at error like TclError: couldn't recognize image data. If that is the case, you'll need to convert your image to GIF.

这篇关于图像未出现在 Tkinter 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆