我最近才开始编程,尤其是 Python (PyQt)。我有我的主要QMainWindow
课程。但我想将它与 UI 小部件分开,以便所有 Windows 的东西(菜单、工具栏、常用按钮)都在 中QMainWindow
,但所有程序/UI 特定的小部件(按钮、组合框、图像、复选框等)都在单独的QWidget
类中。但我不确定我这样做是否正确。
- 我的布局有问题 - 一些不可见的东西覆盖了菜单,因此它们不能被鼠标点击,我想我没有正确地将我的 UI 小部件添加到主窗口
这是我的做法:
class MyMainWindow(QMainWindow):
def __init__(self, parent = None):
super(MyMainWindow, self).__init__(parent)
self.main_widget = QWidget(self)
...
self.form_widget = FormWidget(self)
#This is my UI widget
self.main_layout = QVBoxLayout(self.main_widget)
self.main_layout.sizeConstraint = QLayout.SetDefaultConstraint
self.main_layout.addWidget(self.form_widget.main_widget)
#form_widget has its own main_widget where I put all other widgets onto
self.main_widget.setLayout(self.main_layout)
self.setCentralWidget(self.main_widget)
- 我见过其他 Python 程序,其中应用程序被分解为许多小代码文件(据我了解,主类中的所有内容都是不可读或难以管理的)。
你对将代码分解成小块有什么建议?怎么做比较好?或者对于 UI,它可以都在一个大的地方?我应该把 UI 代码/类分解成单独的文件吗?
谢谢你。
[解决了]
我发现了我的错误 - 我从 UI 小部件类中删除了 main_widget(现在所有 UI 小部件都直接放在 UI 类小部件本身上)并且只这样做:
self.main_layout.addWidget(self.form_widget)
菜单不再有问题