AttributeError:“ builtin_function_or_method”对象没有属性“ addLayout”

时间:2018-12-05 12:29:41

标签: python python-3.x pyqt5

我在PyQT5 and Python3中有以下UIWidget类。

class UIWidget(QtWidgets.QWidget):    
    def __init__(self, parent=None):
        super(UIWidget, self).__init__(parent)        
        # Initialize tab screen
        self.tabs = QtWidgets.QTabWidget()
        self.tab1 = QtWidgets.QWidget()        

        # Add tabs
        self.tabs.addTab(self.tab1, "Face")

        self.display = PlayStreaming()
        # Create first tab
        self.createGridLayout()
        self.tab1.layout = QtWidgets.QVBoxLayout()
        self.tab1.layout.addWidget(self.display, stretch=1)
        self.tab1.layout.addWidget(self.horizontalGroupBox)
        self.tab1.setLayout(self.tab1.layout)

        # Add tabs to widget
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addWidget(self.tabs)

    def createGridLayout(self):
        hlay1 = QtWidgets.QVBoxLayout()   
        self.horizontalGroupBox = QtWidgets.QGroupBox("")
        self.horizontalGroupBox.setStyleSheet("QGroupBox{ background-color: red; border: none;}")          
        self.ScaleUpButton=QtWidgets.QPushButton('ScaleUp')
        self.ScaleUpButton.clicked.connect(self.display.scaleupSignal)
        hlay1.addWidget(self.ScaleUpButton) 
        self.ScaleDownButton=QtWidgets.QPushButton('ScaleDown')
        self.ScaleDownButton.clicked.connect(self.display.scaledownSignal)
        hlay1.addWidget(self.ScaleDownButton) 
        self.resetButton=QtWidgets.QPushButton('Reset')
        hlay1.addWidget(self.resetButton)
        self.resetButton.clicked.connect(self.display.resetallarraysSignal)
        self.RecognizeButton=QtWidgets.QPushButton('Recognize')
        self.RecognizeButton.clicked.connect(self.display.send_signal)
        hlay1.addWidget(self.RecognizeButton)             
        self.layout.addLayout(hlay1)
        self.horizontalGroupBox.setLayout(self.layout)

尝试分配Vertical layout to Horizontal layout

此行self.layout.addLayout(hlay1)的错误为

AttributeError: 'builtin_function_or_method' object has no attribute 'addLayout'

如何解决该错误?

1 个答案:

答案 0 :(得分:4)

您正尝试在self.layout中的self.layout = QtWidgets.QVBoxLayout(self)行运行之前访问__init__。因此,self.layoutinherited method from the QtWidgets.QWidget base class

您可能希望完全使用其他名称,以免与QtWidgets.QWidget上的方法发生冲突。