我使用PyQt5及其所有强大的布局和小部件编写了许多代码。但不知何故,我错过了如何正确导入代码所需的大量模块。一切正常,我只想了解使用PyQt5时的最佳实践。我读到没有确切的方法,但是任何建议都是值得赞赏的。我在这里阅读了所有的内容:https://peps.python.org/pep-0008/,但我仍然发现很难为PyQt5找到一个好的解决方案。
我到目前为止所做的工作如下:
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QTextEdit, QFileDialog
from PyQt5.QtWidgets import QLabel, QWidget, QHBoxLayout, QPushButton, QLineEdit
from PyQt5.QtWidgets import QRadioButton, QGridLayout, QFormLayout, QAction
from PyQt5.QtCore import Qt
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
这很麻烦。我也知道我不应该用
from PyQt5.QtWidgets import *
那么,最整洁的方法是什么呢?
发布于 2022-06-14 18:24:33
我通常做的--我借用了这里发布的其他代码--是在括号中对事物进行分组:
from PyQt5.QtWidgets import (QApplication, QMainWindow, QStatusBar, QTextEdit, QFileDialog,
QLabel, QWidget, QHBoxLayout, QPushButton, QLineEdit,
QRadioButton, QGridLayout, QFormLayout, QAction)
from PyQt5.QtCore import Qt
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
发布于 2022-06-14 17:21:01
您可以将使用的函数和类包装到自定义小部件或普通py-文件中,并且在主框架类中只导入这些自定义小部件。
例如,如果您有一个实现定制小部件类的customwidgetscript.py,那么它看起来如下所示:
from customwidgetscript import customwidget
在与您的customwidgetscript.py相同的mainframe.py文件夹中,您可以使用如下所示的导入:
from PyQt5.QtWidgets import QLabel, QWidget, QHBoxLayout, QPushButton, QLineEdit
https://stackoverflow.com/questions/72619528
复制相似问题