How to pass a value from one class to another where each class is on different file
-
wrote on 30 May 2022, 10:31 last edited by john_hobbyist
So, the problem is that I have a GUI, the whole code is written on python and PyQt5. I have a button (BT1) where I press it and runs class_1, which exists on file_1. When the class_1 runs and stores a value in var_1 and then finishes. Let's say var_1 = 10. Then I click on another button (BT2) and then class_2 runs, which is stored in a different file (file_2). I need to pass the value from var_1 to var_2. The var_2 is a variable of class_2. I tried global variables but, I cannot pass the value from var_1 to var_2. Any idea how to achieve that? I have also tried many stackoverflow suggestions without any success.
-
So, the problem is that I have a GUI, the whole code is written on python and PyQt5. I have a button (BT1) where I press it and runs class_1, which exists on file_1. When the class_1 runs and stores a value in var_1 and then finishes. Let's say var_1 = 10. Then I click on another button (BT2) and then class_2 runs, which is stored in a different file (file_2). I need to pass the value from var_1 to var_2. The var_2 is a variable of class_2. I tried global variables but, I cannot pass the value from var_1 to var_2. Any idea how to achieve that? I have also tried many stackoverflow suggestions without any success.
wrote on 30 May 2022, 10:40 last edited by JonB@john_hobbyist
This is basic C++ or Python. An instance of one class cannot see data in an instance of another class unless you pass one instance (or data from it) to the other instance, in some shape or form. Might be as a parameter to a method of the other class, might be via a setter method in the other class, might be via a signal/slot, ... Many ways, nobody can tell you from your vague description of what you want. -
@john_hobbyist
This is basic C++ or Python. An instance of one class cannot see data in an instance of another class unless you pass one instance (or data from it) to the other instance, in some shape or form. Might be as a parameter to a method of the other class, might be via a setter method in the other class, might be via a signal/slot, ... Many ways, nobody can tell you from your vague description of what you want.wrote on 30 May 2022, 10:45 last edited by@JonB said in How to pass a value from one class to another where each class is on different file:
signal/slot
So, If I use signal/slot, where the value sent from class_1 is stored until I press BT2, and then class_2 get the value? I think I try signal/slot to see what happens...
-
@JonB said in How to pass a value from one class to another where each class is on different file:
signal/slot
So, If I use signal/slot, where the value sent from class_1 is stored until I press BT2, and then class_2 get the value? I think I try signal/slot to see what happens...
wrote on 30 May 2022, 10:51 last edited by JonB@john_hobbyist said in How to pass a value from one class to another where each class is on different file:
where the value sent from class_1 is stored until I press BT2, and then class_2 get the value
That is your problem to code! The answer is probably to store that value received from
class_1
as a member variable inclass_2
. Then the instance ofclass_2
has it in itsself.some_variable
. -
@john_hobbyist said in How to pass a value from one class to another where each class is on different file:
where the value sent from class_1 is stored until I press BT2, and then class_2 get the value
That is your problem to code! The answer is probably to store that value received from
class_1
as a member variable inclass_2
. Then the instance ofclass_2
has it in itsself.some_variable
.wrote on 30 May 2022, 11:14 last edited by@JonB said in How to pass a value from one class to another where each class is on different file:
@john_hobbyist said in How to pass a value from one class to another where each class is on different file:
where the value sent from class_1 is stored until I press BT2, and then class_2 get the value
That is your problem to code! The answer is probably to store that value received from
class_1
as a member variable inclass_2
. Then the instance ofclass_2
has it in itsself.some_variable
.How I do that???
-
@JonB said in How to pass a value from one class to another where each class is on different file:
@john_hobbyist said in How to pass a value from one class to another where each class is on different file:
where the value sent from class_1 is stored until I press BT2, and then class_2 get the value
That is your problem to code! The answer is probably to store that value received from
class_1
as a member variable inclass_2
. Then the instance ofclass_2
has it in itsself.some_variable
.How I do that???
wrote on 30 May 2022, 11:19 last edited by@john_hobbyist
This is so basic Python I don't know what to say.class_2_instance.some_variable = class_1_instance.a_variable
ultimately, but depends where you put it, e.g. this example would be from somewhere which has access toclass_2_instance
andclass_1_instance
. Would be different from somewhere else (e.g. insideclass_1
orclass_2
, but that is the principle. It's just member variables. -
@john_hobbyist
This is so basic Python I don't know what to say.class_2_instance.some_variable = class_1_instance.a_variable
ultimately, but depends where you put it, e.g. this example would be from somewhere which has access toclass_2_instance
andclass_1_instance
. Would be different from somewhere else (e.g. insideclass_1
orclass_2
, but that is the principle. It's just member variables.wrote on 30 May 2022, 11:25 last edited by john_hobbyist@JonB said in How to pass a value from one class to another where each class is on different file:
@john_hobbyist
This is so basic Python I don't know what to say.class_2_instance.some_variable = class_1_instance.a_variable
ultimately, but depends where you put it, e.g. this example would be from somewhere which has access toclass_2_instance
andclass_1_instance
. Would be different from somewhere else (e.g. insideclass_1
orclass_2
, but that is the principle. It's just member variables.Thank you! I think I have tried that, I mean you have to call the class somewhere in order to get the values, right? But what I face is not the common python code (it is PyQt5 python code)...because each class is called when I press each button...
-
@JonB said in How to pass a value from one class to another where each class is on different file:
@john_hobbyist
This is so basic Python I don't know what to say.class_2_instance.some_variable = class_1_instance.a_variable
ultimately, but depends where you put it, e.g. this example would be from somewhere which has access toclass_2_instance
andclass_1_instance
. Would be different from somewhere else (e.g. insideclass_1
orclass_2
, but that is the principle. It's just member variables.Thank you! I think I have tried that, I mean you have to call the class somewhere in order to get the values, right? But what I face is not the common python code (it is PyQt5 python code)...because each class is called when I press each button...
wrote on 30 May 2022, 13:15 last edited by JonB@john_hobbyist
My friend, your understanding of "classes" and "instances" is all over the place...! ;-)It's hard to help you because you do not explain where you create your instances of what and where you keep references around. It's not a PyQt5 issue, and yes I do understand both PyQt & Python :)
Somewhere you must create instances of your two classes. But I don't know where/how you do that....
Let's take a possible example related to your situation. Let's say your
class_1
andclass_2
are "windows" or "widgets" or "dialogs", e.g. derived fromQWidget
. And let's say you create the instances from aMainWindow
class. Then you might have something like this:# in `MainWindow` class, somewhere self.window1 = class_1() self.window2 = class_2() # connect the signal raised from `class_1` with its `var_1` parameter # to call the slot in `class_2` with that value as a parameter self.window1.button_clicked.connect(self.window2.on_window1_button_clicked) # in `class_1` class, somewhere # you must create your own "PyQt signal" for the ` self.button_clicked_signal` we are going to `emit` below # you must look up the PyQt5 syntax for that, it is *something* like (as a class variable): pyqtSignal(button_clicked_signal) # connect the Qt `QPushButton.clicked()` signal to `self.on_some_button_clicked()` self.some_button.clicked.connect(self.on_some_button_clicked) # this is a *slot* def on_some_button_clicked(self): self.button_clicked_signal.emit(self.var_1) # `button_clicked_signal` is the signal you created, we pass it `self.var_1`'s value # in `class_2` class, somewhere # this is a *slot* def on_window1_button_clicked(self, var_1): self.var_2 = var_1
The above is one of many ways you can achieve what you want. [UPDATE I changed it a bit, and added some more comments.]
-
wrote on 2 Jun 2022, 09:44 last edited by john_hobbyist 6 Feb 2022, 09:45
Why do I get this error?
Signal(button_clicked_signal) NameError: name 'button_clicked_signal' is not defined
From here:
# in `class_1` class, somewhere # you must create your own "PyQt signal" for the ` self.button_clicked_signal` we are going to `emit` below # you must look up the PyQt5 syntax for that, it is *something* like (as a class variable): pyqtSignal(button_clicked_signal)
I changed this:
pyqtSignal(button_clicked_signal)
to this:
Signal(button_clicked_signal)
-
Hi,
Because it's wrong.
The correct way to declare a signal is:
class MyWidget(QWidget): mySignal = Signal()
Between the parenthesis, put the type for the arguments if any.
-
Hi,
Because it's wrong.
The correct way to declare a signal is:
class MyWidget(QWidget): mySignal = Signal()
Between the parenthesis, put the type for the arguments if any.
wrote on 6 Jun 2022, 13:42 last edited by john_hobbyist 6 Jun 2022, 13:43@SGaist said in How to pass a value from one class to another where each class is on different file:
Hi,
Because it's wrong.
The correct way to declare a signal is:
class MyWidget(QWidget): mySignal = Signal()
Between the parenthesis, put the type for the arguments if any.
self.window1.triggered.connect(self.window2.on_window1_button_clicked) AttributeError: 'class_1' object has no attribute 'triggered'
I use menus instead of buttons. And I click them. How to fix the above error?
-
@SGaist said in How to pass a value from one class to another where each class is on different file:
Hi,
Because it's wrong.
The correct way to declare a signal is:
class MyWidget(QWidget): mySignal = Signal()
Between the parenthesis, put the type for the arguments if any.
self.window1.triggered.connect(self.window2.on_window1_button_clicked) AttributeError: 'class_1' object has no attribute 'triggered'
I use menus instead of buttons. And I click them. How to fix the above error?
wrote on 6 Jun 2022, 14:01 last edited by JonB 6 Jun 2022, 20:12@john_hobbyist
self.window1
is ofclass_1
. The error message tells you that has no method/member namedtriggered
. Since you do not show whatclass_1
, how should people answer "How to fix the above error? Either use a class which has atriggered
member or do not try to accessself.window1.triggered
? -
@john_hobbyist
self.window1
is ofclass_1
. The error message tells you that has no method/member namedtriggered
. Since you do not show whatclass_1
, how should people answer "How to fix the above error? Either use a class which has atriggered
member or do not try to accessself.window1.triggered
?wrote on 6 Jun 2022, 18:21 last edited by@JonB said in How to pass a value from one class to another where each class is on different file:
@john_hobbyist
self.window1
is ofclass_1
. The error message tells you that has no method/member namedtriggered
. Since you do not show whatclass_1
, how should people answer "How to fix the above error?? Either use a class which has a
triggeredmember or do not try to access
self.window1.triggered`?I copy paste from your previously posted code above:
# in `MainWindow` class, somewhere self.window1 = class_1() self.window2 = class_2() # connect the signal raised from `class_1` with its `var_1` parameter # to call the slot in `class_2` with that value as a parameter self.window1.button_clicked.connect(self.window2.on_window1_button_clicked) # in `class_1` class, somewhere # you must create your own "PyQt signal" for the ` self.button_clicked_signal` we are going to `emit` below # you must look up the PyQt5 syntax for that, it is *something* like (as a class variable): pyqtSignal(button_clicked_signal) # connect the Qt `QPushButton.clicked()` signal to `self.on_some_button_clicked()` self.some_button.clicked.connect(self.on_some_button_clicked) # this is a *slot* def on_some_button_clicked(self): self.button_clicked_signal.emit(self.var_1) # `button_clicked_signal` is the signal you created, we pass it `self.var_1`'s value # in `class_2` class, somewhere # this is a *slot* def on_window1_button_clicked(self, var_1): self.var_2 = var_1
What should I change here? I implement the above rationale to my code. I cannot post my code is too huge for here, but I follow your logic...
-
There's no triggered signal anywhere in sight in your code.
What can you do ?
Create minimal script with a small UI where you can actually properly experiment and implement signals and slots rather than trying to cram it in your huge code base. Once you have understood how it working, then you will be able to translate that to your application.
On a side note, please give your classes and variable meaningful names. Random things like class_1 does not make your code readable nor easy to reason about.
-
There's no triggered signal anywhere in sight in your code.
What can you do ?
Create minimal script with a small UI where you can actually properly experiment and implement signals and slots rather than trying to cram it in your huge code base. Once you have understood how it working, then you will be able to translate that to your application.
On a side note, please give your classes and variable meaningful names. Random things like class_1 does not make your code readable nor easy to reason about.
wrote on 7 Jun 2022, 09:33 last edited by@SGaist said in How to pass a value from one class to another where each class is on different file:
There's no triggered signal anywhere in sight in your code.
What can you do ?
Create minimal script with a small UI where you can actually properly experiment and implement signals and slots rather than trying to cram it in your huge code base. Once you have understood how it working, then you will be able to translate that to your application.
On a side note, please give your classes and variable meaningful names. Random things like class_1 does not make your code readable nor easy to reason about.
I have spent many days studying tutorials such as this: https://programmer.group/pyqt5-quick-start-pyqt5-signal-slot-mechanism.html, this: https://www.geeksforgeeks.org/pyqt5-qaction/ and many stackoverflow questions.... What I mean is that I have ran code and see what happens. Do I need these lines from the code above:
# in `MainWindow` class, somewhere self.window1 = class_1() self.window2 = class_2()
???
-
That's quite a strange question to ask. If you want to use objects from either of
class_1
orclass_2
in yourMainWindow
class, then yes you need them. -
That's quite a strange question to ask. If you want to use objects from either of
class_1
orclass_2
in yourMainWindow
class, then yes you need them.wrote on 7 Jun 2022, 09:43 last edited by@SGaist said in How to pass a value from one class to another where each class is on different file:
That's quite a strange question to ask. If you want to use objects from either of
class_1
orclass_2
in yourMainWindow
class, then yes you need them.So, since
triggered.connect
does not work on them, how I send the signal when the menus (that call those functions) are chosen? I am confused...
-
@SGaist said in How to pass a value from one class to another where each class is on different file:
That's quite a strange question to ask. If you want to use objects from either of
class_1
orclass_2
in yourMainWindow
class, then yes you need them.So, since
triggered.connect
does not work on them, how I send the signal when the menus (that call those functions) are chosen? I am confused...
wrote on 7 Jun 2022, 10:39 last edited by@john_hobbyist said in How to pass a value from one class to another where each class is on different file:
does not work on them, how I send the signal when the menus (that call those functions) are chosen? I am confused...
Q: What Qt object has a
triggered
signal?
A: void QAction::triggered(bool checked = false)This signal is emitted when an action is activated by the user; for example, when the user clicks a menu option, toolbar button, or presses an action's shortcut key combination, or when trigger() was called
So you need
QAction
s for yourQMenu
items:Normally, you connect each menu action's triggered() signal to its own custom slot
-
wrote on 8 Jun 2022, 11:17 last edited by john_hobbyist 6 Aug 2022, 11:42
I left signals and slots. I am trying with getters and setters... like here: https://www.geeksforgeeks.org/getter-and-setter-in-python/
I have stuck here... This is part of my code:
class class_1(): def do_this(self): ... # code here... ... self.myvar = "Ok" ... def get_var(self): return self.myvar class class_2(): def do_something(self): raj = class_1() self.var = raj.get_var() print("var = ", self.var) # here I check the value ...
But I get this error:
raj = class_1() UnboundLocalError: local variable 'class_1' referenced before assignment
Any idea what I miss here?
-
I left signals and slots. I am trying with getters and setters... like here: https://www.geeksforgeeks.org/getter-and-setter-in-python/
I have stuck here... This is part of my code:
class class_1(): def do_this(self): ... # code here... ... self.myvar = "Ok" ... def get_var(self): return self.myvar class class_2(): def do_something(self): raj = class_1() self.var = raj.get_var() print("var = ", self.var) # here I check the value ...
But I get this error:
raj = class_1() UnboundLocalError: local variable 'class_1' referenced before assignment
Any idea what I miss here?
wrote on 8 Jun 2022, 11:44 last edited by- Did you import the
class1
module into theclass_2
module? - Is your code indeed as you show?
- Does it really say it fails on the
raj = class_1()
statement? - Are you aware, even if you get this working, that
raj = class_1()
will create a new instance ofclass_1
? And that is really what you intend/need for your question, and you're not going to come back and say "but this way it doesn't access the existingclass_2
instance/widget I had"... ?
- Did you import the
1/48