You are infuriating. Here is how you should have originally asked your question.
class MyToolBar(mpl_qt.NavigationToolbar2QT):
def set_message(self, s):
print(distance) # Want to get distance from OrthoView. How do I do this?
class OrthoView(QMainWindow):
def __init__(self, parent=None):
super(OrthoView, self).__init__(parent)
...
distance = 60
...
self.toolbar = MyToolBar(self.plotCanvas, self)
I would tell you that assigning distance as you do in OrthoView.__init__ creates a local variable that can only be used in that method. You need to make it an instance variable. I would also ask if this value is static or dynamic? I am going to assume it changes from time to time and that the toolbar will need to ask for an updated value.
There are multiple ways this can be done. I am already passing an instance of ortho to the toolbar. I could use that to get the value.
class MyToolBar(mpl_qt.NavigationToolbar2QT):
def __init__(self, canvas, ortho):
super().__init__(canvas, ortho)
self.ortho = ortho
def set_message(self):
print(self.ortho.distance
class OrthoView(QMainWindow):
def __init__(self, parent=None):
super(OrthoView, self).__init__(parent)
...
self.distance = 60
...
self.toolbar = MyToolBar(self.plotCanvas, self)
I would also say that this is a bad design because it forces MyToolbar to know things about OrthoView, and any changes to OrthoView may require a change to MyToolbar. I might suggest using a function to get the value and telling Toolbar about the function.
class MyToolBar(mpl_qt.NavigationToolbar2QT):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update_func = None
def update_with(self, func):
self.update_func = func
def set_message(self):
if self.update_func:
print(self.update_func())
class OrthoView(QMainWindow):
def __init__(self, parent=None):
super(OrthoView, self).__init__(parent)
...
self.distance = 60
...
self.toolbar = MyToolBar(self.plotCanvas, self)
self.toolbar.update_with(self.get_distance)
def get_distance(self):
return self.distance
If distance is just a static number that is only set when the toolbar is created, then I would just pass that info as an additional argument to the __init__
class MyToolBar(mpl_qt.NavigationToolbar2QT):
def __init__(self, canvas, ortho, distance):
super().__init__(canvas, ortho)
self.distance = distance
def set_message(self):
print(self.distance)
class OrthoView(QMainWindow):
def __init__(self, parent=None):
super(OrthoView, self).__init__(parent)
...
self.toolbar = MyToolBar(self.plotCanvas, self, 60)
Ask a good question and you get good answers. Ask a crappy question and you get what we have here. Two pages of me asking what you are trying to do and you waiting two days for an answer.