Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 618
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 2
Kenil Vasani
Asked: December 14, 20202020-12-14T21:07:25+00:00 2020-12-14T21:07:25+00:00In: Python

AttributeError: module ‘PyQt5.QtGui’ has no attribute ‘QWidget’

  • 2

So, I’m trying to make a UI for a python code I have, but keep stumbling up on problems…

Right now, all the code does is make a window, with 2 texteditor boxes, and one button. When the button is pressed, it prints ‘Test’.

I used QTDesigner to make the UI, then PyQt5 to convert it to python, and edited the code.

My code:

   # -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'widget.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Widget(QtGui.QWidget):
    def setupUi(self, Widget):
        Widget.setObjectName("Widget")
        Widget.resize(681, 382)
        self.horizontalLayout = QtWidgets.QHBoxLayout(Widget)
        self.horizontalLayout.setContentsMargins(11, 11, 11, 11)
        self.horizontalLayout.setSpacing(6)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.in_box = QtWidgets.QTextEdit(Widget)
        self.in_box.setMaximumSize(QtCore.QSize(200, 350))
        self.in_box.setObjectName("in_box")
        self.horizontalLayout.addWidget(self.in_box)
        self.convert = QtWidgets.QPushButton(Widget)
        self.convert.setObjectName("convert")
        self.horizontalLayout.addWidget(self.convert)
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setContentsMargins(11, 11, 11, 11)
        self.gridLayout.setSpacing(6)
        self.gridLayout.setObjectName("gridLayout")
        self.out_box = QtWidgets.QTextEdit(Widget)
        self.out_box.setMaximumSize(QtCore.QSize(200, 350))
        self.out_box.setObjectName("out_box")
        self.gridLayout.addWidget(self.out_box, 6, 0, 1, 1)
        self.horizontalLayout.addLayout(self.gridLayout)

        self.retranslateUi(Widget)
        QtCore.QMetaObject.connectSlotsByName(Widget)

    def retranslateUi(self, Widget):
        _translate = QtCore.QCoreApplication.translate
        Widget.setWindowTitle(_translate("Widget", "Converter"))
        self.convert.setText(_translate("Widget", "Convert"))
    self.convert.clicked.connect(self.convertclick)

    def convertclick(self):
        print ('Test')



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Widget = QtWidgets.QWidget()
    ui = Ui_Widget()
    ui.setupUi(Widget)
    Widget.show()
    sys.exit(app.exec_())

This is the error I get:

AttributeError: module 'PyQt5.QtGui' has no attribute 'QWidget'

Even when I change class Ui_Widget(QtGui.QWidget):
to class Ui_Widget(QtGui.QtWidgets):
I get AttributeError: module 'PyQt5.QtGui' has no attribute 'QtWidgets'

Anyone who knows what the problem may be?

Cheers.

pyqt5pythonqt-designer
  • 1 1 Answer
  • 10 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      Your error is from here:

      Ui_Widget(QtGui.QWidget)
      

      It basically tells you what the problem is.

      It seems you are mixing some QT4 and QT5 here as your import is in QT5-style, but QtGui.QWidget looks like QT4-style.

      Replace the line with:

      Ui_Widget(QtWidgets.QWidget)
      

      which should be compatible according to the docs

      Remark: I don’t know what you are really doing, but when you mention this: Even when I change class Ui_Widget(QtGui.QWidget): to class Ui_Widget(QtGui.QtWidgets): I get AttributeError: module 'PyQt5.QtGui' has no attribute 'QtWidgets' That’s correct. You already imported QtWidgets, and not from PyQt5.QtGui. Just use Ui_Widget(QtWidgets) there.

      In short: all these errors seem to be related to refactoring in regards to the modules between QT4 and QT5. The docs should help.

      • 2
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      create-react-app is not working since version 4.0.1

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com