Jul-04-2020, 09:25 AM
Your DraggableLine class should not know how it is to be used by the application. It should not know that there are multiple instances of draggable line and it should not be putting information in a global variable and it should definitely not know about calculating differences and writing the result in labels. The DraggableLine class should let you drag the line, provide a way to get the location of the line, and have a way to specify a function to be called when the line is moved.
Use Canvas as an example. Canvas does not know anything about DraggableLine, but it provides a way to call DraggableLine.clickonline when the canvas is picked. Your DraggableLine class should have a connect and disconnect for the line moved event. Your application would use this to call a function in the application that knows about all three plots and how to get the cursor/line position from a plot and know about the labels and all the calculations and stuff. If you want to use the DraggableLine information in multiple places you just connect multiple functions to the event.
Since DraggableLine only has one interesting event, you can simplify the connect/disconnect to maintain a list of functions.
Use Canvas as an example. Canvas does not know anything about DraggableLine, but it provides a way to call DraggableLine.clickonline when the canvas is picked. Your DraggableLine class should have a connect and disconnect for the line moved event. Your application would use this to call a function in the application that knows about all three plots and how to get the cursor/line position from a plot and know about the labels and all the calculations and stuff. If you want to use the DraggableLine information in multiple places you just connect multiple functions to the event.
Since DraggableLine only has one interesting event, you can simplify the connect/disconnect to maintain a list of functions.
class DraggableLine(): def __init__(self, plot, canvas): self.canvas = canvas self.callbacks = [] self.x = 0 self.cursor = lines.Line2D((self.x, self.x), (-2, 2), color='red', picker=5) plot.add_line(self.cursor) self.canvas.draw_idle() self.canvas.mpl_connect('pick_event', self.pick_callback) def connect(self, func): self.callbacks.append[func] return func # App will use as handle for disconnect def disconnect(self, handle): self.callbacks.remove(handle) def pick_callback(self, event): if event.artist == self.cursor: self.move = self.canvas.mpl_connect("motion_notify_event", self.move_callback) self.release = self.canvas.mpl_connect("button_press_event", self.button_callback) def move_callback(self, event): self.x = event.xdata self.cursor.set_xdata([self.x, self.x]) self.canvas.draw_idle() def button_callback(self, event): self.canvas.mpl_disconnect(self.move) self.canvas.mpl_disconnect(self.release) for cb in self.callbacks(): cb()Somewhere in you application you would write a function to calculate differences and update the results.
def update_cusor_displays(): a = pageOne.line1.x b = pageOne.line2.x c = pageOne.line3.x tex1Value = str(b-a) tex2Value = str(c-a) pageOne.line1.connect(update_cursor_displays) pageOne.line2.connect(update_cursor_displays) pageOne.line3.connect(update_cursor_displays)