1

我正在使用带有 Eric5 和 QTDesigner 的 Python 3.2 来尝试在 QGraphicsView 场景中显示 jpg。以下代码给了我一个 无法打开的文件。原因:[Errno 22] Invalid argument:" error on the first pass and end with a 调试程序引发异常未处理的 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_())
4

1 回答 1

0

看起来我需要将我的场景移动到类,然后引用 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_())
于 2011-04-29T23:00:18.143 回答