Python Forum
How to pass variables from one class to another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass variables from one class to another
#11
Please look here: source code

I have a value inside class OrthoView(QMainWindow) (line 104) and more specifically I have the value in method def __init__(self, parent=None) which I want to send it to class MyToolBar(mpl_qt.NavigationToolbar2QT) (line 46) and more specifically in method def set_message(self, s).

How can I achieve that? Thank you...
Reply
#12
We are getting close now. How about you add the code to set the value in OrthoView and add the code to use the value in MyToolBar.set_message. I know it won't run anymore, but I am still unclear about what the dependency is.

I don't think there is any class dependency here since you are talking about things that happen in __init__. MyToolBar should be able to get whatever INSTANCE VARIABLE values it needs from the OrthoView OBJECT using the instance passed when you create the toolbar.
hobbyist likes this post
Reply
#13
(Sep-23-2021, 12:15 PM)deanhystad Wrote: We are getting close now. How about you add the code to set the value in OrthoView and add the code to use the value in MyToolBar.set_message. I know it won't run anymore, but I am still unclear about what the dependency is.

I don't think there is any class dependency here since you are talking about things that happen in __init__. MyToolBar should be able to get whatever INSTANCE VARIABLE values it needs from the OrthoView OBJECT using the instance passed when you create the toolbar.

Can you please provide me example code in order to understand? Thanks...
Reply
#14
No. You provide example code of what you want to do, then I explain what you need to do to make it work. I am still unclear about what Toolbar is trying to get from OrthoView.
Reply
#15
That's very simple...suppose I have a variable

distance = 60
inside
def __init__(self, parent=None)
of
class OrthoView(QMainWindow)
. I need to pass this instance (value) in
def set_message(self, s)
of
class MyToolBar(mpl_qt.NavigationToolbar2QT)
...
Reply
#16
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.
hobbyist likes this post
Reply
#17
@deanhystad: You are awesome!! Thank you for your help and for your time!!
Reply
#18
Everything works from post #16. I also need to use this code:

class GetInterestingInfo(): # assume this class: class MyToolBar(mpl_qt.NavigationToolbar2QT):
    def get_info(self):
        self.device.read_data()
        self.process_raw_data()
        return self.processed_data
 
class UseInterestingInfo():
    def use_info(self, interesting_info):
        pass
 
 
x = GetInterestingInfo()
y = UseInterestingInfo()
 
y.use_info(x.get_info()):
but instead of class GetInterestingInfo I have put class MyToolBar(mpl_qt.NavigationToolbar2QT). Please look post #11. How do I call it in this command:

x = MyToolBar() 
I cannot call it like this, because it needs arguments inside the parentheses. I cannot also put self.plotCanvas, self. So what do I put inside?

(The idea is to send values from class MyToolBar to the class UseInterestingInfo)
Reply
#19
You are doing it again, asking questions without providing code, or enough code. Where are you trying to use this code? Why do you think you need to make MyToolBar? Doesn't your code already make a MyToolBar? Why aren't you using that one?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 518 Nov-12-2021, 09:55 PM
Last Post: snippsat
  Acess variables from class samuelbachorik 3 569 Aug-20-2021, 02:55 PM
Last Post: deanhystad
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 1,336 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Do I have to pass 85 variables to function? Milfredo 10 1,867 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 1,231 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Class variables menator01 2 900 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 958 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Pass by reference vs Pass by value leodavinci1990 1 1,138 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  Understanding Class Variables vindo 9 2,001 Jun-05-2019, 08:04 PM
Last Post: Yoriz
  What is the strategy for working with class variables? AlekseyPython 3 1,658 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020