QGraphicsView 错误 - 尝试用 Python 显示 jpg
我正在使用 Python 3.2 与 Eric5 和 QTDesigner 来尝试在 QGraphicsView 场景中显示 jpg。下面的代码给了我一个 无法打开该文件。原因:[Errno 22] 无效参数:“ 第一次通过时出错,然后以 被调试的程序引发了未处理的异常 AttributeError “‘MyForm’对象没有属性‘QGraphicsView’” 文件: ,行: 17
引用的 screentest.ui 文件只是表单上的一个简单的 QGraphicsView 对象框。
非常感谢任何建议。
#UIGraphicTest.py
import sys
# from PyQt4 import QtCore
from PyQt4 import QtGui
from screentest import Ui_MainWindow
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
grview = myapp.QGraphicsView()
scene = myapp.QGraphicsScene()
scene.addPixmap(QtGui.QPixmap('att-logo.jpg'))
grview.setScene(scene)
myapp.show()
sys.exit(app.exec_())
I'm using Python 3.2 with Eric5 and QTDesigner to try and display a jpg in a QGraphicsView scene. The following code gives me a
The file could not be opened. Reason: [Errno 22] Invalid argument: "
error on the first pass and then ends with a
The debugged program raised the exception unhandled AttributeError
"'MyForm' object has no attribute 'QGraphicsView'"
File: , Line: 17
The referenced screentest.ui file is just a simple QGraphicsView object box on a form.
Any suggestions are greatly appreciated.
#UIGraphicTest.py
import sys
# from PyQt4 import QtCore
from PyQt4 import QtGui
from screentest import Ui_MainWindow
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
grview = myapp.QGraphicsView()
scene = myapp.QGraphicsScene()
scene.addPixmap(QtGui.QPixmap('att-logo.jpg'))
grview.setScene(scene)
myapp.show()
sys.exit(app.exec_())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来我需要将场景移到类中,然后引用 QGraphicsView 对象的名称。我还添加了在 Windows 中使用的 ctypes。这是工作代码:
Looks like I needed to move my scene up to the class and then reference the name of the QGraphicsView object. I also added the ctypes for use in Windows. Here's the working code: