So I've created a method on a class that creates instances of the class resistor, and when a resistor is created, and image will appear on the tkinter canvas. Theres also a method that allows the image to be moved, so what happens is that when I create the first instance, I move the image, and then create the second instance, but when I do this, the first image disappears, but I believe that what happens is that is moves exactly where the second image is, but I don't know why this happens. Here's the code:
class SideBar():
def createResistor(self, value, name): # Method used to create instance of class Resistor
Res1 = Resistor(self.root, value, name)
self.resList.append(Res1)
class Resistor():
def cargarimg(self, archivo): # Se carga imagen
ruta = os.path.join('img', archivo)
imagen = PhotoImage(file = ruta)
return imagen
def show_res(self):
resImage = self.cargarimg('Res.png')
MA.MP.paintWindow.image = resImage
MA.MP.paintWindow.create_image(100, 100, image = resImage)
def move(self, e): # Method that allows image to be moved
resImage = self.cargarimg('Res.png')
MA.MP.paintWindow.image = resImage
MA.MP.paintWindow.create_image(e.x, e.y, image = resImage)
def __init__(self, root, resistance, name):
self.root = root
self.resistance = resistance
self.name = name
self.x = 50
self.y = 50
self.show_res()
MA.MP.paintWindow.bind('<B1-Motion>', self.move)
Update I've done tests by making a the coordinates x and y parameters, but the first image still disappears, this makes me believe that either for some reason the first image was deleted or it is replaced by the second image.
The easiest way to fix this is by using an lru_cache on the loading method.
from functools import lru_cache
class Resistor():
@lru_cache(None)
def cargarimg(self, archivo): # Se carga imagen
ruta = os.path.join('img', archivo)
imagen = PhotoImage(file = ruta)
return imagen
This replaces the image reference saving, so you can remove this line from all your methods.
MA.MP.paintWindow.image = resImage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With