AttributeError:“设置”对象没有属性“场景”

时间:2018-11-27 07:16:13

标签: python pyqt pyqt4 qgraphicsscene

我需要将像素图添加到矩形中。当我单击“单击”按钮时,我的像素图将被一个接一个地添加到矩形中,但是我得到了这个from random import uniform a = 166.667 n = 12 assert a > 0 assert n >= 1 pieces = [] for idx in range(n-1): # Number between 1 and a # minus the current total so we don't overshoot pieces.append(uniform(0,a-sum(pieces))) pieces.append(a-sum(pieces) 。谁能指导我如何解决此错误?我尝试了很多方法,但没有得到正确的输出。

先谢谢您。

下面是我的代码:

attribute error

1 个答案:

答案 0 :(得分:1)

出现该错误是因为场景不在Settings类中,self.scene是ScanView类的成员,而self.scene与场景不同。

要解决此问题,必须将其添加为矩形的子级,并且还必须更改像素图的大小。要获得第一个矩形,必须将其存储在列表中,然后创建一个迭代器。

import sys
from pyface.qt import QtGui, QtCore

class ScanView(QtGui.QGraphicsView):
    def __init__(self,X=5, Y=5, parent=None):
        super(ScanView, self).__init__(parent)
        self._squares = []
        n_rows, n_cols = X, Y
        squareLB = 50
        width, height = (Y + 2)*squareLB, (X + 2)*squareLB
        self._scene = QtGui.QGraphicsScene(0, 0, max(708, width), height)
        p = squareLB if width > 708 else (708.0-Y*squareLB)/2.0

        for i in range(n_rows):
            for j in range(n_cols):
                it = self._scene.addRect(0, 0, squareLB, squareLB)
                it.setPos(p + j*squareLB, i*squareLB)   
                self._squares.append(it)
        self.setScene(self._scene)

class Settings(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Settings, self).__init__(parent)
        self.save = QtGui.QPushButton("save")
        self.open = QtGui.QPushButton("open")
        self.folder = QtGui.QPushButton("Folder", clicked=self.showSettings)
        central_widget = QtGui.QWidget()
        self.setCentralWidget(central_widget)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.save)
        vbox.addWidget(self.open)
        vbox.addWidget(self.folder)
        self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
        self.scrollArea.setBackgroundRole(QtGui.QPalette.Light)
        hlay = QtGui.QHBoxLayout(central_widget)
        hlay.addLayout(vbox)
        hlay.addWidget(self.scrollArea)
        self.setGeometry(200, 100, 300, 300)

    def showSettings(self):
        self.view = ScanView(5, 5)
        self.scanbtn = QtGui.QPushButton("click", clicked=self.on_clicked)
        self.newwidget = QtGui.QWidget()
        hlay = QtGui.QHBoxLayout(self.newwidget)
        hlay.addWidget(self.view)
        hlay.addWidget(self.scanbtn)
        self.scrollArea.setWidget(self.newwidget)

        self._iter_squares = iter(self.view._squares)

    def on_clicked(self):
        filename = "./img/tick.png"
        pixmap = QtGui.QPixmap(filename)
        if pixmap.isNull():
            return
        try:
            it = next(self._iter_squares)
        except StopIteration:
            pass
        else:
            pixmap = pixmap.scaled(it.rect().size().toSize())
            pixmap_it = QtGui.QGraphicsPixmapItem(pixmap, it)

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Settings()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()