我是一位有用的助手,可以为您进行文本翻译。
在编程课程中,我们需要开发一个应用程序,并且必须使用PYQT5来实现GUI。
我有一个基于QMainWindow的类。 有些代码行我不太理解。 下面是初始化:
# Snippet 1
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
# self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("Main Window")
然后,定义了以下变量/图形用户界面元素:
# Snippet 2
self.main_widget = QtWidgets.QWidget(self)
l = QtWidgets.QVBoxLayout(self.main_widget)
sc = MyStaticMplCanvas(width=5, height=4, dpi=100) # just some graph
dc = MyDynamicMplCanvas(width=5, height=4, dpi=100) # another graph
l.addWidget(sc)
l.addWidget(dc)
然后,我尝试添加一个水平盒式布局,并加入以下内容:
# Snippet 3
x = QtWidgets.QHBoxLayout(self.main_widget) # new
b1 = QtWidgets.QPushButton("Test1") # new
b2 = QtWidgets.QPushButton("Test2") # new
x.addWidget(p1) # new
x.addWidget(p2) # new
最后,生成中央小部件:
# Snippet 4
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
程序本身没有错误并且正常工作。但是,只有片段2中的两个图形显示在窗口中。如果您删除
self.main_widget
来自
l = QtWidgets.QVBoxLayout(self.main_widget)
如果括号为空,则只显示片段3中的按钮。
以下赋值语句背后的含义是什么?
self.main_widget = QtWidgets.QWidget(self)
你能将垂直和水平盒子布局合并为一个布局,并将两个按钮和两个图表作为中央小部件显示吗? 这是我的原始计划,我尝试使用addLayout()和setLayout()选项,并希望将这些布局添加到setCentralWidget(),但没有成功。
您有想法如何在基于QMainWindow的类中显示多个盒子布局的组合作为中央小部件吗?
非常感谢。 :)