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 atriggered
member or do not try to accessself.window1.triggered
?@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.
@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.@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...
@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
-
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?
- 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
-
- 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"... ?
@JonB said in How to pass a value from one class to another where each class is on different file:
- 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"... ?
Thank you for your comment! No, my code is very huge. I depict only the needed parts here... Do you have any better idea of how to pass the value from class_1() to class_2()? I tried signals/slots but I didn't achieve something. Any idea of how to pass the value with getters/setters?
- Did you import the
-
You realize to you linked to a blog post describing exactly how to implement a getter and a setter and how to use them ?
-
You realize to you linked to a blog post describing exactly how to implement a getter and a setter and how to use them ?
@SGaist said in How to pass a value from one class to another where each class is on different file:
You realize to you linked to a blog post describing exactly how to implement a getter and a setter and how to use them ?
I tried to follow the link but as you see above I face problems with my code...
-
As already written earlier, the fact that you post snipets of parts of your code distributed makes it almost impossible to help you.
As @JonB already suggested: you likely did not import class_1 properly in the file where you define class_2.
-
i do not know if there is a predefined method in PyQt but in python you can get the value from a function and call it in other class
this is a simple prototype may helps youdef idfunc(self): id =self.user_idtext() return id
-
As already written earlier, the fact that you post snipets of parts of your code distributed makes it almost impossible to help you.
As @JonB already suggested: you likely did not import class_1 properly in the file where you define class_2.
@SGaist said in How to pass a value from one class to another where each class is on different file:
As already written earlier, the fact that you post snipets of parts of your code distributed makes it almost impossible to help you.
As @JonB already suggested: you likely did not import class_1 properly in the file where you define class_2.
Yes, I do! Nothing works! Apart from signals/slots and getters/setters how can I pass the value from class_1 to class_2? class_1 and class_2 are stored in different files...
-
Well, this is the standard way to do it.
The fact that your classes are in two different files just means that you have to use proper imports to be able to use them. Nothing more.
From the looks of it, you essentially need to clean up your code base.
As suggested earlier, you should write a small dummy project that does what you want and then adapt that to your big code base.
-
Well, this is the standard way to do it.
The fact that your classes are in two different files just means that you have to use proper imports to be able to use them. Nothing more.
From the looks of it, you essentially need to clean up your code base.
As suggested earlier, you should write a small dummy project that does what you want and then adapt that to your big code base.
@SGaist said in How to pass a value from one class to another where each class is on different file:
Well, this is the standard way to do it.
The fact that your classes are in two different files just means that you have to use proper imports to be able to use them. Nothing more.
From the looks of it, you essentially need to clean up your code base.
As suggested earlier, you should write a small dummy project that does what you want and then adapt that to your big code base.
I tried this: https://www.geeksforgeeks.org/getter-and-setter-in-python/ and this: https://programmer.group/pyqt5-quick-start-pyqt5-signal-slot-mechanism.html that have source code. I will try again
-
@JonB : What's the purpose of these lines:
# in `MainWindow` class, somewhere self.window1 = class_1() self.window2 = class_2()
?
-
@JonB : What's the purpose of these lines:
# in `MainWindow` class, somewhere self.window1 = class_1() self.window2 = class_2()
?
@john_hobbyist
In the example,class_1
&class_2
are each some kind of widget/window classes, each derived fromQWidget
.class_1()
&class_2()
each create a new instance (window in this case) of the respective class. I store the references in two member variables ofself
, perhaps yourMainWindow
class.