如何从类外部访问另一个类中创建的类的变量

时间:2015-04-27 16:56:30

标签: python class pyqt

我有一个类,我需要访问它的属性。问题在于此类的实例在另一个类中实例化。

这是我希望访问其变量的类(还有更多内容,但是这会显示如何创建类):

class Button(QtGui.QPushButton):
    def __init__(self, iconPathUnassigned, iconX, iconY, posX, posY, posA, posB, objName, labEdit, iconPathAssigned, iconPathPlaying, parent=None):
        super(Button, self).__init__(parent=parent) 
        self.setAcceptDrops(True)
        self.iconPathAssigned = iconPathAssigned

然后在另一个类中实例化该类:

class Ui_Form(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(591, 591)
        self.btn_a1 = Button("Python/LP_Proj/LP_Circle_Off.png", 49, 49, 30, 38, 41, 41, "btn_a1", self.lab_a1, "Python/LP_Proj/LP_Circle_Green.png", "Python/LP_Proj/LP_Circle_Red.png", self)

Ui_Form按以下方式创建:

if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    ex = Ui_Form()
    ex.show()
    sys.exit(app.exec_())

所以,说我想在行iconPathAssigned之后打印btn_a1 ex.show()变量,我该怎么做?我尝试了各种各样的方法:

print Ui_Form.btn_a1.iconPathAssigned
print Form.btn_a1.iconPathAssigned
print btn_a1.iconPathAssigned
print QtGui.QWidget(Ui_Form.btn_a1.iconPathAssigned)

1 个答案:

答案 0 :(得分:3)

ex

因为ex = Ui_Form()是您的表单实例(正如您从finish()行所知道的那样)