【问题标题】:from PyQt4.QtGui: NameError: name 'QtGui' is not defined来自 PyQt4.QtGui: NameError: name 'QtGui' is not defined
【发布时间】:2018-09-11 12:00:53
【问题描述】:

我遵循了具体的导入方式,而不是写 *.
from PyQt4.QtGui

我还缺少什么?

import sys

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_())

【问题讨论】:

  • 您将其导入为PyQt4.QtGui
  • 那么,应该写什么呢? @Aran-Fey
  • 导入没问题,但是你必须使用PyQt4.QtGui而不是QtGui来引用它。
  • @Aran-Fey 我试过class Window(PyQt4.QtGui.QMainWindow):。结果是NameError: name 'PyQt4' is not defined
  • 只需添加from PyQt4 import QtGui 到您的代码中,不要更改任何其他内容

标签: python qt4


【解决方案1】:

我添加了following lines to the code 并解决了错误:

from PyQt4 import QtGui
from PyQt4 import QtCore

现在看起来像这样:

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

【讨论】:

    猜你喜欢
    • 2016-05-16
    • 2022-12-02
    • 2013-04-09
    • 1970-01-01
    • 2020-11-07
    • 2017-01-09
    • 2017-06-14
    • 2023-03-11
    相关资源
    最近更新 更多