【问题标题】:How to add a pushbuttons on the top and bottom of the colorbar如何在颜色栏的顶部和底部添加按钮
【发布时间】:2019-04-25 20:18:24
【问题描述】:

这是我的代码示例。我在我的程序中使用网格布局来添加我的绘图、滑块和按钮。这里我需要在颜色栏的顶部和底部安排上、下按钮,但我没有准确地得到。当我使用 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)

我的图片:

例外图片:

【问题讨论】:

标签: python python-2.7 matplotlib pyqt pyqt4


【解决方案1】:

如果您的绘图和颜色条可以分开,我建议您对绘图的颜色条 + 向上/向下按钮部分使用垂直布局 (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)

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

【讨论】:

    猜你喜欢
    • 2017-12-28
    • 2020-10-10
    • 1970-01-01
    • 2023-03-22
    • 2018-07-09
    • 1970-01-01
    • 2017-06-08
    • 2019-09-09
    • 2022-07-12
    相关资源
    最近更新 更多