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