Search code examples
pythonclassobjectmousetkinter-canvas

How to share mouse coordinates between 2 class


The goal is to reproduce, in canvas classB, the drawing (example with vertical line) made with the mouse in canvas classA.

To do this, I need to transfer the mouse coordinates from one class to another and I don't know how to do it. Thanks if you have any ideas. PS: I put a fixed value for x to explain what I want to do

from tkinter import*

def main():
    display()

def display():
   
    fen=Tk()
    fen.title("Test")
    fen.geometry("500x200+50+50")
    fen.configure(bg='green')   
    
    a = A(fen)
    a.configure(bg='white', bd=2, relief=SUNKEN)
    a.grid(row=1,column=1)    
    
    b = B(fen)        
    b.configure(bg="White", bd=2, relief=SUNKEN)
    b.grid(row=2, column=1)    
    
class A(Canvas):
    def __init__(self, fen , larg=500, haut=100):
        Canvas.__init__(self, fen)
        self.configure(width=larg, height=haut)
        self.larg, self.haut = larg, haut
        self.bind('<Motion>', self.callback)         
        
    def callback(self, event):        
        x, y = event.x, event.y         
        self.create_line(x, 0, x, 100, width=1)        
        
class B(Canvas):
    def __init__(self, fen, larg = 500, haut=100,  x=" "):
        Canvas.__init__(self, fen)
        self.configure(width=larg, height=haut)
        self.larg, self.haut = larg, haut
        
        x = 200 # variable to import from class A ??
        
        self.create_line(x, 0, x, 100, width=1)   
   
main()

Solution

  • There are surely other ways to do it, but a possible approach would be to create your own class that inherits from Tk, and use it so that A can propagate back its events, so that they can be distributed by the father, in this case mirroring the events on B.

    A crude sample implementation could be:

    from tkinter import *
    
    def main():
        Display()
    
        
    class Display(Tk):
        def __init__(self):
            super().__init__()
            self.title("Test")
            self.geometry("500x200+50+50")
            self.configure(bg='green')   
            
            self.a = A(self)
            self.a.configure(bg='white', bd=2, relief=SUNKEN)
            self.a.grid(row=1,column=1)    
            
            self.b = B(self)        
            self.b.configure(bg="White", bd=2, relief=SUNKEN)
            self.b.grid(row=2, column=1)    
            
            self.mainloop()
            
        def callback_from_a(self, x, y):
            self.b.create_line(x, 0, x, 100, width=1)
            
        
    class A(Canvas):
        def __init__(self, fen , larg=500, haut=100):
            self.parent = fen
            Canvas.__init__(self, fen)
            self.configure(width=larg, height=haut)
            self.larg, self.haut = larg, haut
            self.bind('<Motion>', self.callback)         
            
        def callback(self, event):        
            x, y = event.x, event.y         
            self.create_line(x, 0, x, 100, width=1)  
            self.parent.callback_from_a(x, y)
            
            
    class B(Canvas):
        def __init__(self, fen, larg = 500, haut=100,  x=" "):
            Canvas.__init__(self, fen)
            self.configure(width=larg, height=haut)
            self.larg, self.haut = larg, haut
            
            x = 200 # variable to import from class A ??
            
            #self.create_line(x, 0, x, 100, width=1)   
       
    main()