导入pyqt5.qtwidgets模块的最佳实践
我使用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 *
,那么最整洁的方法是什么?
I have written many codes using PyQt5 and all its powerful layouts and widgets.
But somehow I am missing how to properly import the great amount of modules that I need for my codes.
Everything works fine, I just want to understand the best practice when working with PyQt5.
I read that there is not an exact way, but any suggestion is appreciated.
I read all about here: https://peps.python.org/pep-0008/ but still I find difficult to find a good solution for PyQt5.
What I do up to now is as follows:
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
which is quite cumbersome.
I know also that I should not use
from PyQt5.QtWidgets import *
so what is the most neat way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常会做的事情,并且我从这里发布的其他代码借来的是将括号中的事物分组:
What I usually do and I've borrowed it from other code posted here is to group things in parentheses:
您可以将二手功能和类包装到自定义的小部件或普通的py文件中,而在主帧类中,只有导入这些自定义小部件。
例如,如果您具有实现CustomWidget类的CustomWidgetScript.py,则看起来像:
在与MainFrame.py的同一文件夹中,您可以将其类似地导入到同一文件夹中:
You could wrap the used functions and classes into custom widgets or plain py-files and in your main frame class only import those custom widgets.
For example if you have a customwidgetscript.py that implements a customwidget class it would look something like:
In that customwidgetscript.py in the same folder as your mainframe.py you could have your imports like this: