1

When i try to run this code, it's showing an error: NameError: name 'QtGui' is not defined. What's wrong with my app?

Code:

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

I'm did it all like in the example, but still it is showing error.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Mr.Tigr
  • 29
  • 1
  • 1
  • 6
  • 2
    Your import is wrong.See the not accepted answer of [import PyQt4 does not include PyQt4.QtCore or QtGui](http://stackoverflow.com/questions/21323899/import-pyqt4-does-not-include-pyqt4-qtcore-or-qtgui) – Mel Aug 29 '15 at 22:17
  • @tmoreau. That answer specifically deals with the question of how to "import everything" in PyQt. But that is not what is needed here. The OP should replace all the import lines (apart from `import sys`) with `from PyQt4 import QtCore, QtGui`, and then just change the class definition to `class WindowHello(QtGui.QWidget):`. The script will then work as expected. – ekhumoro Aug 30 '15 at 14:30
  • That's why I specifically referred to the not accepted answer which states "Don't use import *, namespaces exist for a good reason.". But yes, there's maybe a better duplicate than this question. – Mel Aug 30 '15 at 15:06

1 Answers1

1

Try the following code that I provide, you try to instance QtGui, QtCore, which contain all types of widgets/lib so you cannot instance them all, you need to be specific, use instead eg: 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_())
  • 1
    It not good practice to use `import *`, and the line `from PyQt4 import *` is useless – Mel Aug 30 '15 at 10:07
  • from PyQt4 import * , yes I miss that, well ... is not my code, regarding " It not good practice to use import * " yes I know, just import what you need... –  Aug 30 '15 at 10:17