QGraphicsView 错误 - 尝试用 Python 显示 jpg

发布于 2024-11-04 01:41:40 字数 888 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2024-11-11 01:41:40

看起来我需要将场景移到类中,然后引用 QGraphicsView 对象的名称。我还添加了在 Windows 中使用的 ctypes。这是工作代码:

#UIGraphicTest.py

import sys,  ctypes
#from PyQt4 import QtCore
from PyQt4 import QtGui
from screentest import Ui_MainWindow

class MyForm(QtGui.QMainWindow):
  def __init__(self):
    QtGui.QWidget.__init__(self)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    #Prevent grouping with python in Windows
    myappid = 'UIGraphicTest'
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
    # Create a Scene
    self.scene = QtGui.QGraphicsScene()
    self.scene.addPixmap(QtGui.QPixmap('logo.jpg')) 
    self.ui.graphicsView.setScene(self.scene)

if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  myform = MyForm()
  myform.show()

  sys.exit(app.exec_())

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:

#UIGraphicTest.py

import sys,  ctypes
#from PyQt4 import QtCore
from PyQt4 import QtGui
from screentest import Ui_MainWindow

class MyForm(QtGui.QMainWindow):
  def __init__(self):
    QtGui.QWidget.__init__(self)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    #Prevent grouping with python in Windows
    myappid = 'UIGraphicTest'
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
    # Create a Scene
    self.scene = QtGui.QGraphicsScene()
    self.scene.addPixmap(QtGui.QPixmap('logo.jpg')) 
    self.ui.graphicsView.setScene(self.scene)

if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  myform = MyForm()
  myform.show()

  sys.exit(app.exec_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文