ömer sarı
ömer sarı

Reputation: 699

Image does not appear on 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.

here is my code:

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()

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

Upvotes: 0

Views: 240

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

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.

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

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

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.

Upvotes: 3

Related Questions