2

I copied a PyQt example from the Web into a file and opened it up in PyCharm. Below is the code:

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from math import *


class Calculator(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.browser = QTextBrowser()
        self.expression = QLineEdit()
        self.expression.setPlaceholderText("Type an expression and press Enter.")
        self.expression.selectAll()

        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.expression)
        self.someWidget = QWidget()
        self.someWidget.setLayout(layout)
        self.setCentralWidget(self.someWidget)
        self.expression.setFocus()

        self.expression.returnPressed.connect(self.updateUi)
        self.setWindowTitle("Calculator")

    def updateUi(self):
        try:
            text = self.expression.text()
            self.browser.append("%s = %s", (text, eval(text)))
        except:
            self.browser.append("<font color=red>Invalid Expression</font>")


def main():
    import sys
    app = QApplication(sys.argv)
    window = Calculator()
    window.show()
    sys.exit(app.exec_())


main()

The problem is that the code run well even without adding the following import statements:

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from math import *

I have seen this example being used in many videos and books. If the code works good without the above statements, then why did the author of the example write the statements.

Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
  • If they're not needed, then you shouldn't; unused imports just clutter up your code and namespace, particularly unused *wildcard* imports. – jonrsharpe Nov 20 '15 at 08:48
  • 3
    From PyQt4 to PyQt5, a lot of things moved from QtGui, QtCore to QtWidgets. My guess is that the code was originally written for PyQt4, and useless imports were not removed. The proper way to import would be `import PyQt5.QtWidgets as QtWidgets`. – Mel Nov 20 '15 at 09:09
  • 1
    That explains everything! Thanks for answering my question. – Vishal Subramanyam Nov 20 '15 at 09:11
  • @tmoreau You may want to post your comment as answer. OP will accept if it solves the problem. – Santosh Kumar Nov 22 '15 at 07:21

1 Answers1

3

From PyQt4 to PyQt5, a lot of things moved from QtGui, QtCore to QtWidgets. To write a simple application in PyQt5, you're likely to need only QtWidgets.

My guess is that the code was originally written for PyQt4, and "adapted" to PyQt5, without removing useless imports.

The proper way to import would be import PyQt5.QtWidgets as QtWidgets ( see Should wildcard import be avoided ?).

The code then becomes:

class Calculator(QtWidgets.MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.browser = QtWidgets.QTextBrowser()
        self.expression = QtWidgets.QLineEdit()
        ...
Community
  • 1
  • 1
Mel
  • 5,837
  • 10
  • 37
  • 42