0

I've been programming an application using tkinter and python 3. I've created a canvas and I'm tring to display on it a gif image of 5000x5000 pixel, where the canvas is 2000x2000 pixel, but the image doesn't appear when the program is being ran. This is the code:

class drawCanvas(object):

    def __init__(self, master, width=500, height=500):
        ''' build the canvas object '''

        # class attributes
        self.master = master
        self.cWidth = width
        self.cHeight = height

        # creating the canvas object
        self.canvas = tk.Canvas(self.master, width=self.cWidth, height=self.cHeight, bg="white")
        self.canvas.grid(row=0, column=2, sticky="nwes")
        self.canvas.configure(scrollregion=(0, 0, 2000, 2000))

        # creating the scrolling
        self.scroll_x = tk.Scrollbar(self.master, orient="horizontal", command=self.canvas.xview)
        self.scroll_x.grid(row=1, column=2, sticky="ew")

        self.scroll_y = tk.Scrollbar(self.master, orient="vertical", command=self.canvas.yview)
        self.scroll_y.grid(row=0, column=3, sticky="ns")

        self.canvas.configure(yscrollcommand=self.scroll_y.set, xscrollcommand=self.scroll_x.set)

        # trying to import an image
        self.canvas.create_image(500, 500, anchor="nw", image=r'C:\Users\Luca\Desktop\electronic_simulation\src\bg\try.gif')

I'm wondering if is there any solution, in case there is please tell me. Thank you for your time!

Luke__
  • 227
  • 1
  • 9
  • For one, the `image` option requires a `PhotoImage`, not a string. For another, see https://stackoverflow.com/questions/16424091 – Bryan Oakley Mar 13 '20 at 20:38

1 Answers1

2

i will try fo fix your code but even before i start, i can tell you that if you use an absolute path for your image, you must double the \ because a single \ is used for specials actions or use / instead. so your path must be

C:\\Users\\Luca\\Desktop\\electronic_simulation\\src\\bg\\try.gif

or

C:/Users/Luca/Desktop/electronic_simulation/src/bg/try.gif

edit: i think i solved your issue:

class drawCanvas(object):

    def __init__(self, master, width=500, height=500):
        ''' build the canvas object '''
        global img

        # class attributes
        self.master = master
        self.cWidth = width
        self.cHeight = height

        # creating the canvas object
        self.canvas = tk.Canvas(self.master, width=self.cWidth, height=self.cHeight, bg="white")
        self.canvas.grid(row=0, column=2, sticky="nwes")
        self.canvas.configure(scrollregion=(0, 0, 2000, 2000))

        # creating the scrolling
        self.scroll_x = tk.Scrollbar(self.master, orient="horizontal", command=self.canvas.xview)
        self.scroll_x.grid(row=1, column=2, sticky="ew")

        self.scroll_y = tk.Scrollbar(self.master, orient="vertical", command=self.canvas.yview)
        self.scroll_y.grid(row=0, column=3, sticky="ns")

        self.canvas.configure(yscrollcommand=self.scroll_y.set, xscrollcommand=self.scroll_x.set)

        # trying to import an image

        self.img = tk.PhotoImage(file = "C:\\Users\\Luca\\Desktop\\electronic_simulation\\src\\bg\\try.gif") 
        self.canvas.create_image(0, 0, anchor="nw", image=self.img)

new edit: as i don't know the context of your script, i created a script that work for me and that you can copy-paste and run directly:

import tkinter as tk
class drawCanvas(object):

    def __init__(self, master, width=500, height=500):
        ''' build the canvas object '''
        global img

        # class attributes
        self.master = master
        self.cWidth = width
        self.cHeight = height

        # creating the canvas object
        self.canvas = tk.Canvas(self.master, width=self.cWidth, height=self.cHeight, bg="white")
        self.canvas.grid(row=0, column=2, sticky="nwes")
        self.canvas.configure(scrollregion=(0, 0, 2000, 2000))

        # creating the scrolling
        self.scroll_x = tk.Scrollbar(self.master, orient="horizontal", command=self.canvas.xview)
        self.scroll_x.grid(row=1, column=2, sticky="ew")

        self.scroll_y = tk.Scrollbar(self.master, orient="vertical", command=self.canvas.yview)
        self.scroll_y.grid(row=0, column=3, sticky="ns")

        self.canvas.configure(yscrollcommand=self.scroll_y.set, xscrollcommand=self.scroll_x.set)

        # trying to import an image

        self.img = tk.PhotoImage(file = r"C:\Users\Luca\Desktop\electronic_simulation\src\bg\try.gif")
        self.canvas.create_image(0, 0, anchor="nw", image=self.img)

    def ml(self):
        self.master.mainloop()

test = drawCanvas(tk.Tk())
test.ml()
rémi couturier
  • 425
  • 3
  • 11
  • `self.img = PhotoImage(file=...)` should be `self.img = Image.open(...)`. And the `PIL` module you said is actually `Pillow` module, a clone of the old `PIL` module. `Pillow` module is not part of standard Python distribution. – acw1668 Mar 14 '20 at 00:06
  • You can simply use `self.img = ImageTk.PhotoImage(file=...)` to replace the two `self.img = ...` statements. – acw1668 Mar 14 '20 at 00:10
  • Yes I agree with your comments and I fixed my code @acw1688 – rémi couturier Mar 14 '20 at 06:58
  • thank you for your time and for your answer. I used the signle backslash because I typed r' --- ' this should be the syntax to insert a path, but I might be wrong. Anyway I tried and it still displays only the background color of the canvas, white. If you are wondering, the image was a picture of a giant OK drown by hand. – Luke__ Mar 14 '20 at 09:34
  • I've also tried to insert the image using the PIL module but it still doesn't work. When I run the program either using PIL or PhotoImage, there is no error message... – Luke__ Mar 14 '20 at 09:42
  • did you try to navigate in the bottom-left corner of the canvas because when i tested your script, my image didn't appear because it was at 500,500 coordinates (bottom-left) and yes you are right your path work, i didn't know about the "r" thing. – rémi couturier Mar 14 '20 at 10:29
  • and i edited my answer with a full script you can just copy-paste and run – rémi couturier Mar 14 '20 at 10:40