为什么我应该在 PyQt 中导入不需要的 QtGui 和 QtCore?
Why should I import QtGui and QtCore in PyQt when it isn't necessary?
我从网上复制了一个 PyQt 示例到一个文件中,然后在 PyCharm 中打开它。下面是代码:
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()
问题是代码 运行 即使不添加以下导入语句也很好:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from math import *
我在很多视频和书籍中看到过这个例子。如果没有上述语句代码也能正常工作,那么示例作者为什么要编写这些语句。
从PyQt4到PyQt5,很多东西从QtGui
、QtCore
搬到了QtWidgets
。要在 PyQt5 中编写一个简单的应用程序,您可能只需要 QtWidgets
.
我的猜测是代码最初是为 PyQt4 编写的,"adapted" 为 PyQt5 编写的,没有删除无用的导入。
正确的导入方式是 import PyQt5.QtWidgets as QtWidgets
(参见 Should wildcard import be avoided ?)。
代码则变为:
class Calculator(QtWidgets.MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.browser = QtWidgets.QTextBrowser()
self.expression = QtWidgets.QLineEdit()
...
我从网上复制了一个 PyQt 示例到一个文件中,然后在 PyCharm 中打开它。下面是代码:
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()
问题是代码 运行 即使不添加以下导入语句也很好:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from math import *
我在很多视频和书籍中看到过这个例子。如果没有上述语句代码也能正常工作,那么示例作者为什么要编写这些语句。
从PyQt4到PyQt5,很多东西从QtGui
、QtCore
搬到了QtWidgets
。要在 PyQt5 中编写一个简单的应用程序,您可能只需要 QtWidgets
.
我的猜测是代码最初是为 PyQt4 编写的,"adapted" 为 PyQt5 编写的,没有删除无用的导入。
正确的导入方式是 import PyQt5.QtWidgets as QtWidgets
(参见 Should wildcard import be avoided ?)。
代码则变为:
class Calculator(QtWidgets.MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.browser = QtWidgets.QTextBrowser()
self.expression = QtWidgets.QLineEdit()
...