NameError:name' QtGui'没有定义

时间:2015-08-29 21:59:23

标签: python pyqt4 python-import nameerror

当我尝试运行此代码时,它显示错误:NameError: name 'QtGui' is not defined。我的应用程序出了什么问题?

代码:

import sys
from tkinter import *
from PyQt4 import *
from PyQt4.QtGui import *
from PyQt4.QtCore import * 

class WindowHello(QtGui, QWidget, QtCore):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(650, 450, 450, 380)
        self.label = QtGui.QLabel("<center>Hello!<center>")
        self.box = QtGui.QVBoxLayout()
        self.box.addWidget(self.label)
        self.setLayout(self.box)

app = QtGui.QApplication(sys.argv)

op = WindowHello()
op.setWindowTitle('LangTIME')
op.setWindowIcon(QtGui.QIcon('Minilogowin.png'))
op.show()

sys.exit(app.exec_())

我在示例中完成了所有操作,但仍显示错误。

1 个答案:

答案 0 :(得分:0)

尝试我提供的以下代码,您尝试实例 QtGui QtCore ,其中包含所有类型的小部件/ lib,因此您无法将它们全部实例化,您需要具体而言,请使用例如: QWidget QDialog QMainWindow

import sys
#from tkinter import *
#from PyQt4 import *
from PyQt4.QtGui import *
from PyQt4.QtCore import * 

class WindowHello(QWidget):
    def __init__(self, parent = None):
        QWidget.__init__(self, parent)

        self.setGeometry(650, 450, 450, 380)
        self.label = QLabel("<center>Hello!<center>")
        self.box = QVBoxLayout()
        self.box.addWidget(self.label)
        self.setLayout(self.box)



app = QApplication(sys.argv)

op = WindowHello()
op.setWindowTitle('LangTIME')
#op.setWindowIcon(QtGui.QIcon('Minilogowin.png'))
op.show()

sys.exit(app.exec_())