gpt4 book ai didi

python - PyQt:self.setScene(self.scene)AttributeError: 'Window'对象没有属性 'setScene'

转载 作者:太空宇宙 更新时间:2023-11-03 13:56:03 24 4
gpt4 key购买 nike

目的是查看窗口中的滚动条:
绘制滚动条的函数取自这里:https://www.programcreek.com/python/example/52415/PyQt4.QtGui.QGraphicsScene

该错误是什么意思以及我应该如何解决它?

import sys

from PyQt4 import QtGui
from PyQt4 import QtCore

from PyQt4.QtGui import QMainWindow, QSizePolicy, QWidget, QVBoxLayout, QAction,\
QKeySequence, QLabel, QItemSelectionModel, QMessageBox, QFileDialog, QFrame, \
QDockWidget, QProgressBar, QProgressDialog

from PyQt4.QtCore import SIGNAL, QSettings, QSize, QPoint, QVariant, QFileInfo, QTimer, pyqtSignal, QObject


class Window(QtGui.QMainWindow):

def __init__(self, parent=None):

QtGui.QGraphicsView.__init__(self, parent)
self.scene = QtGui.QGraphicsScene(self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)

self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

self._pan = False
self._draw = False
self._moved = False
self._sel = False
self.pen = None
self.penid = None
self.cmap = None
self.penwidth = 4
self._redoStack = []
self._histStates = []
self._baseRects = []

app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())

错误:

Traceback (most recent call last):
File "temp.py", line 42, in <module>
GUI = Window()
File "temp.py", line 21, in __init__
self.setScene(self.scene)
AttributeError: 'Window' object has no attribute 'setScene'

最佳答案

您正在指示 WindowQMainWindow:

class Window(QtGui.QMainWindow):

所以Window不是QGraphicsView。

因此,在显示窗口必须是 QGraphicsView 的隐含示例中,解决方案是将 QMainWindow 更改为 QGraphicsView

import sys

from PyQt4 import QtGui
from PyQt4 import QtCore

class Window(QtGui.QGraphicsView):

def __init__(self, parent=None):

QtGui.QGraphicsView.__init__(self, parent)
self.scene = QtGui.QGraphicsScene(self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
self.setScene(self.scene)

self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.viewport().setCursor(QtCore.Qt.CrossCursor)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

self._pan = False
self._draw = False
self._moved = False
self._sel = False
self.pen = None
self.penid = None
self.cmap = None
self.penwidth = 4
self._redoStack = []
self._histStates = []
self._baseRects = []

app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())

关于python - PyQt:self.setScene(self.scene)AttributeError: 'Window'对象没有属性 'setScene',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49609259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com