Python Forum
Display image in tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display image in tkinter
#1
Hi all, I have a problem with displaying my image in tkinter. I´m creating a class, which creates a canvas and put an image from my file there. This is my code so far:

class Try:

    ... creating a canvas ...

    def method(self):
        player_icon = tkinter.PhotoImage(file="C://Users//name//Documents//PYTHON//folder//img_player.png")
        self.canvas.create_image(50, 50, image=player_icon)
It doesn´t reach any error, it seems like it found that image but simply doesn´t show it. What am I doing wrong?

Thanks in advance.
Reply
#2
here's a sample:
icon source: https://www.opensecurityarchitecture.org...on-library

from tkinter import *

def show_image():
    root = Tk()

    canvas = Canvas(root,  width=400, height=400)
    canvas.pack()

    img = PhotoImage(file="osa_lightning.png")
    canvas.create_image(10, 10, anchor=NW, image=img)

    mainloop()

if __name__ == '__main__':
    show_image()
   
Reply
#3
You possibly need an ImageTk.PhotoImage. see http://effbot.org/tkinterbook/photoimage.htm Also, you have to import Image and ImageTk to have access to it's functions.
Reply
#4
player_icon --> self.player_icon
Reply
#5
Thank you guys, I did it but now I noticed one another problem. I want to display my picture on canvas, where currently are some rectangles, lines and other graphics. I want to have this picture on the top of all graphics there, but it doesn´t appear so. It is probably behind, because I cannot see it. Can you help me with this?
Reply
#6
Quote: It is probably behind, because I cannot see it
I would try
item = canvas.create_image(10, 10, anchor=NW, image=img)
canvas.tag_raise(item)
Reply
#7
By default, the "display list" of the objects on the canvas follows their creation sequence, from the bottom (the first created one) to the top (the last one). You can change it through the object's ID or tag.
Reply
#8
Ok so this seems working, but now I want to implement this image displaying feature into my for cycle. It should look like this:

for i in range(self.height // self.field_size):
            for j in range(self.width // self.field_size):
                self.field_icon = tkinter.PhotoImage(file="GroundGravel_Grass.png")
                if self.layout[field] == "0":
                    self.canvas.create_image(j*50, i*50, anchor=NW, image=self.field_icon)
                elif self.layout[field] == "1":
                    self.canvas.create_rectangle(j*50, i*50, j*50+self.field_size, i*50+self.field_size, fill="blue", outline="yellow")
                elif self.layout[field] == "2":
                    self.canvas.create_rectangle(j*50, i*50, j*50+self.field_size, i*50+self.field_size, fill=self.sand, outline="yellow")
Basically, this program reads a file with number 0, 1 and 2 and according to specific numbers it should put an image to the correct position (according to "i" and "j") and so create a square grid (should work as a playfield later). You can see that in case of numbers 1 and 2 I have just colors yet, I stopped there, because even number 0 didn´t work with a picture of a grass. It draws just a last square, image here:

[Image: screen.png]

The whole black area should be covered with such a green grass tiles, but when I replace image of a grass by basic green color tile, it works. Can you help me?
Reply
#9
Move line 3 before the for loops. By overwriting self.field_icon in the loop you create many PhotoImage instances and destroy them at the next iteration. It seems to me that you need only one PhotoImage instance.
Reply
#10
Woohoo.. :D works now, thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 397 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 3,972 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,300 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,556 Jul-22-2022, 10:26 AM
Last Post: menator01
  [PyQt] Cannot Display Image after Selecting Image bintangkecil 4 2,497 Jun-12-2022, 08:18 AM
Last Post: Axel_Erfurt
  looking for scripts that do simple image display Skaperen 10 4,128 Sep-13-2021, 05:35 PM
Last Post: FullOfHelp
  How to redraw an existing image to a different image TkInter zazas321 6 5,744 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,517 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 3,954 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 17,548 Feb-06-2021, 03:35 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020