Reputation: 1399
In my project I have 2 Classes. I have a problem in accessing variables from 2nd class. The code structure is as follows.
class Ui_Dialog(object):
def setupUi(self, Dialog):
-------------------
-------------------
-------------------
-------------------
-------------------
def function1(self):
self.variable_I_wanna_share="XXXX"
-------------------
-------------------
class Ui_MainWindow(QtGui.QMainWindow,Ui_Dialog):
def setupUi(self, MainWindow):
-------------------
-------------------
-------------------
-------------------
def function2(self):
*Want to get that variable to here*
-------------------
I inherited 2nd class from 1st class But it didn't work. Any one can help me to figure this out? Note - 1st and 2nd classes are generated from pyuic4(pyqt) separately Thank you.
Upvotes: 0
Views: 3214
Reputation: 1458
As it has been suggested I think you can archive what you want using class variables.
class Ui_Dialog(object):
variable_I_wanna_share = "XXXX"
def setupUi(self, Dialog):
pass
def function1(self):
Ui_Dialog.variable_I_wanna_share="YYYY"
class Ui_MainWindow(Ui_Dialog):
def setupUi(self, MainWindow):
pass
def function2(self):
#accessing class variable
print(Ui_Dialog.variable_I_wanna_share)
dialog=Ui_Dialog()
window=Ui_MainWindow()
And here is the class var:
In [3]: window.function2()
XXXX
you can modify the class var with your function1 from any of the instances
In [4]: window.function1()
In [5]: window.function2()
YYYY
you can also access modify the class var form the instances
In [6]: window.variable_I_wanna_share
Out[6]: 'YYYY'
In [7]: dialog.variable_I_wanna_share
Out[7]: 'YYYY'
just remember that if you modify a class var referring it from a instance you will only modify it for that instance.
In [8]: dialog.variable_I_wanna_share="ZZZZ"
In [9]: window.variable_I_wanna_share
Out[9]: 'YYYY'
In [10]: dialog.variable_I_wanna_share
Out[10]: 'ZZZZ'
Upvotes: 2