如何在颜色栏的顶部和底部添加按钮

时间:2018-11-23 08:23:22

标签: python python-2.7 matplotlib pyqt pyqt4

这是我的示例代码代码。我在程序中使用gridlayout添加绘图,滑块和按钮。在这里,我需要在颜色栏的顶部和底部安排向上,向下按钮,但我没有完全正确。当我使用self.glayout.addWidget(self.down,9,3,1,1)时,它正在解决整个布局。任何人都可以指导我。

self.up = QtGui.QPushButton(_("up"))
self.up.setMaximumWidth(40)
self.up.setLayoutDirection(QtCore.Qt.RightToLeft)
self.down = QtGui.QPushButton(_("down"))
self.down.setLayoutDirection(QtCore.Qt.RightToLeft)
self.down.setMaximumWidth(60)
self.newwidget = QtGui.QWidget()
self.glayout = QtGui.QGridLayout(self.newwidget)
self.canvas = MplCanvas()
self.nextBtn = QtGui.QPushButton(_("Next"))
self.previousBtn = QtGui.QPushButton(_("Previous"))
self.squareNum = QtGui.QLineEdit()
self.glayout.addWidget(self.canvas,0,0,10,10)
self.glayout.addWidget(self.up,1,4,1,1)
self.glayout.addWidget(self.down,9,4,1,1)
self.glayout.addWidget(self.sl,0,10,10,1)
self.glayout.addWidget(self.nextBtn,10,8,1,1)
self.glayout.addWidget(self.previousBtn,10,0,1,1)
self.glayout.addWidget(self.squareNum,10,4,1,1)
self.scrollArea.setWidget(self.newwidget)

我的图片:

enter image description here

例外图片:

enter image description here

1 个答案:

答案 0 :(得分:0)

如果可以将绘图和颜色栏分开,则建议对绘图的颜色栏+上/下按钮部分使用垂直布局(QVBoxLayout)。您将像这样将小部件添加到垂直布局中:

vlayout = QVBoxLayout()
vlayout.addWidget(up_button)
vlayout.addWidget(color_bar) # I don't know which widget is your color bar
vlayout.addWidget(down_button)

然后,将此布局添加到主图旁边的网格布局中:

glayout = QGridLayout()
glayout.addWidget(plot, 0, 0)
glayout.addLayout(vlayout, 0, 1)

如果相反,问题是您的图和颜色栏合并为一个且不能分开,则您可能希望将图夹在两个水平布局之间,如下所示:

vlayout = QVBoxLayout() # This is the main layout that will hold everything
top = QHBoxLayout()
bottom = QHBoxLayout()
top.addStretch(10) # This will push the button to the far right
top.addWidget(up_button, 1)
bottom.addStretch(10) # This will push the button to the far right
bottom.addWidget(down_button, 1)
vlayout.addLayout(top)
vlayout.addWidget(plot)
vlayout.addLayout(bottom)

我发现一般来说,网格布局很难使用,通常垂直和水平布局可以组合在一起以产生更好的结果,从而使布局管理器能够执行其主要功能。