如何将 GroupBox 放入 GridLayout 中? PyQt4
How to put a GroupBox inside a GridLayout? PyQt4
我想将一个包含很多复选框和按钮小部件的组框放入 PyQt4 的 GridLayout 中。另外,我想在 groupBox 之外有一个按钮(但在相同的布局中),一旦选中了复选框,就可以通过单击它来 运行 一些功能。
另外,我想在组框中添加一个滚动条(也许 QScrollArea 可以?),因为我将来可能会在其中添加更多按钮。
需要注意的一件事是此 layout/window 中有多个选项卡(我不确定该术语)。所有 groupbox/button/etc 都应该在选项卡 1 中。
我已经阅读了几篇具有相同情况的文章,但其中 none 提供了正确的答案...如果有人知道如何解决该问题,请告诉我。赞赏!
编辑:我的代码如下。如您所见,我在底部有一个组框。我想要做的是将组框放在顶部,所有 checkboxs/buttons,除了组框外底部的 运行 按钮。而这些都应该只发生在 Tab 1 中。
import PyQt4
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os, sys
class Page1 (QTabWidget):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab1, "Tab1")
self.addTab(self.tab2, "Tab2")
self.addTab(self.tab3, "Tab3")
self.tab1_initUI()
self.tab2_initUI()
self.tab3_initUI()
def tab1_initUI(self):
btn1a = QPushButton('A1')
btn1a.resize(btn1a.sizeHint())
btn1b = QPushButton('A2')
btn1b.resize(btn1b.sizeHint())
btn2a = QPushButton('B1')
btn2a.resize(btn2a.sizeHint())
btn2b = QPushButton('B2')
btn2b.resize(btn2b.sizeHint())
btn3a = QPushButton('C1')
btn3a.resize(btn3a.sizeHint())
btn3b = QPushButton('C2')
btn3b.resize(btn3b.sizeHint())
btn4a = QPushButton('D1', self)
btn4a.resize(btn4a.sizeHint())
btn4b =QPushButton('D2', self)
btn4b.resize(btn4b.sizeHint())
btn5a = QPushButton('E1', self)
btn5a.resize(btn5a.sizeHint())
btn5b = QPushButton('E2', self)
btn5b.resize(btn5b.sizeHint())
self.checkBox1 = QtGui.QCheckBox('1', self)
self.checkBox2 = QtGui.QCheckBox('2', self)
self.checkBox3 = QtGui.QCheckBox('3', self)
self.checkBox4 = QtGui.QCheckBox('4', self)
self.checkBox5 = QtGui.QCheckBox('5', self)
btnRun = QPushButton('Run', self)
btnRun.resize(btnRun.sizeHint())
grid = QtGui.QGridLayout()
grid.addWidget(self.checkBox1, 1, 0)
grid.addWidget(btn1a, 1, 1)
grid.addWidget(btn1b, 1, 2)
grid.addWidget(self.checkBox2, 2, 0)
grid.addWidget(btn2a, 2, 1)
grid.addWidget(btn2b, 2, 2)
grid.addWidget(self.checkBox3, 3, 0)
grid.addWidget(btn3a, 3, 1)
grid.addWidget(btn3b, 3, 2)
grid.addWidget(self.checkBox4, 4, 0)
grid.addWidget(btn4a, 4, 1)
grid.addWidget(btn4b, 4, 2)
grid.addWidget(self.checkBox5, 5, 0)
grid.addWidget(btn5a, 5, 1)
grid.addWidget(btn5b, 5, 2)
grid.addWidget(btnRun, 6, 0, 1, 3)
groupbox = QtGui.QGroupBox(self)
hbox = QtGui.QHBoxLayout()
grid2= QtGui.QGridLayout()
hbox.addLayout(grid2)
groupbox.setLayout(hbox)
scroll = QtGui.QScrollArea()
scroll.setWidget(groupbox)
scroll.setWidgetResizable(True)
grid.addWidget(groupbox,7,0,1,3)
self.tab1.setLayout(grid)
def tab2_initUI(self):
grid = QtGui.QGridLayout()
self.tab2.setLayout(grid)
def tab3_initUI(self):
grid = QtGui.QGridLayout()
self.tab3.setLayout(grid)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(300, 200, 600, 370)
self.startPage1()
def startPage1(self):
x = Page1(self)
self.setWindowTitle("Auto Benchmark")
self.setCentralWidget(x)
self.show()
def main():
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
总是建议划分任务,在这种情况下,我们将创建 class Widget
,我们将在其中放置按钮和复选框。
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
for i in range(20):
letter = chr(ord('a') + i)
checkBox = QtGui.QCheckBox('{}'.format(i+1), self)
self.layout().addWidget(checkBox, i, 0)
btna = QPushButton("{}1".format(letter), self)
btnb = QPushButton("{}2".format(letter), self)
self.layout().addWidget(btna, i, 1)
self.layout().addWidget(btnb, i, 2)
第二个是 class Tab1
,它实现了包含带有 QScrollArea
和 运行 按钮的 Widget
的小部件:
class Tab1(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
self.group = Widget(self)
scroll = QtGui.QScrollArea(self)
scroll.setWidget(self.group)
scroll.setWidgetResizable(True)
self.layout().addWidget(scroll)
self.runBtn = QPushButton("Run", self)
self.layout().addWidget(self.runBtn)
然后将按钮添加到 QTabWidget
(您不再需要 tab1_initUI()
功能)
class Page1(QTabWidget):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.tab1 = Tab1()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab1, "Tab1")
self.addTab(self.tab2, "Tab2")
# ...
截图:
完整代码在下面link
我想将一个包含很多复选框和按钮小部件的组框放入 PyQt4 的 GridLayout 中。另外,我想在 groupBox 之外有一个按钮(但在相同的布局中),一旦选中了复选框,就可以通过单击它来 运行 一些功能。
另外,我想在组框中添加一个滚动条(也许 QScrollArea 可以?),因为我将来可能会在其中添加更多按钮。
需要注意的一件事是此 layout/window 中有多个选项卡(我不确定该术语)。所有 groupbox/button/etc 都应该在选项卡 1 中。
我已经阅读了几篇具有相同情况的文章,但其中 none 提供了正确的答案...如果有人知道如何解决该问题,请告诉我。赞赏!
编辑:我的代码如下。如您所见,我在底部有一个组框。我想要做的是将组框放在顶部,所有 checkboxs/buttons,除了组框外底部的 运行 按钮。而这些都应该只发生在 Tab 1 中。
import PyQt4
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os, sys
class Page1 (QTabWidget):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab1, "Tab1")
self.addTab(self.tab2, "Tab2")
self.addTab(self.tab3, "Tab3")
self.tab1_initUI()
self.tab2_initUI()
self.tab3_initUI()
def tab1_initUI(self):
btn1a = QPushButton('A1')
btn1a.resize(btn1a.sizeHint())
btn1b = QPushButton('A2')
btn1b.resize(btn1b.sizeHint())
btn2a = QPushButton('B1')
btn2a.resize(btn2a.sizeHint())
btn2b = QPushButton('B2')
btn2b.resize(btn2b.sizeHint())
btn3a = QPushButton('C1')
btn3a.resize(btn3a.sizeHint())
btn3b = QPushButton('C2')
btn3b.resize(btn3b.sizeHint())
btn4a = QPushButton('D1', self)
btn4a.resize(btn4a.sizeHint())
btn4b =QPushButton('D2', self)
btn4b.resize(btn4b.sizeHint())
btn5a = QPushButton('E1', self)
btn5a.resize(btn5a.sizeHint())
btn5b = QPushButton('E2', self)
btn5b.resize(btn5b.sizeHint())
self.checkBox1 = QtGui.QCheckBox('1', self)
self.checkBox2 = QtGui.QCheckBox('2', self)
self.checkBox3 = QtGui.QCheckBox('3', self)
self.checkBox4 = QtGui.QCheckBox('4', self)
self.checkBox5 = QtGui.QCheckBox('5', self)
btnRun = QPushButton('Run', self)
btnRun.resize(btnRun.sizeHint())
grid = QtGui.QGridLayout()
grid.addWidget(self.checkBox1, 1, 0)
grid.addWidget(btn1a, 1, 1)
grid.addWidget(btn1b, 1, 2)
grid.addWidget(self.checkBox2, 2, 0)
grid.addWidget(btn2a, 2, 1)
grid.addWidget(btn2b, 2, 2)
grid.addWidget(self.checkBox3, 3, 0)
grid.addWidget(btn3a, 3, 1)
grid.addWidget(btn3b, 3, 2)
grid.addWidget(self.checkBox4, 4, 0)
grid.addWidget(btn4a, 4, 1)
grid.addWidget(btn4b, 4, 2)
grid.addWidget(self.checkBox5, 5, 0)
grid.addWidget(btn5a, 5, 1)
grid.addWidget(btn5b, 5, 2)
grid.addWidget(btnRun, 6, 0, 1, 3)
groupbox = QtGui.QGroupBox(self)
hbox = QtGui.QHBoxLayout()
grid2= QtGui.QGridLayout()
hbox.addLayout(grid2)
groupbox.setLayout(hbox)
scroll = QtGui.QScrollArea()
scroll.setWidget(groupbox)
scroll.setWidgetResizable(True)
grid.addWidget(groupbox,7,0,1,3)
self.tab1.setLayout(grid)
def tab2_initUI(self):
grid = QtGui.QGridLayout()
self.tab2.setLayout(grid)
def tab3_initUI(self):
grid = QtGui.QGridLayout()
self.tab3.setLayout(grid)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(300, 200, 600, 370)
self.startPage1()
def startPage1(self):
x = Page1(self)
self.setWindowTitle("Auto Benchmark")
self.setCentralWidget(x)
self.show()
def main():
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
总是建议划分任务,在这种情况下,我们将创建 class Widget
,我们将在其中放置按钮和复选框。
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
for i in range(20):
letter = chr(ord('a') + i)
checkBox = QtGui.QCheckBox('{}'.format(i+1), self)
self.layout().addWidget(checkBox, i, 0)
btna = QPushButton("{}1".format(letter), self)
btnb = QPushButton("{}2".format(letter), self)
self.layout().addWidget(btna, i, 1)
self.layout().addWidget(btnb, i, 2)
第二个是 class Tab1
,它实现了包含带有 QScrollArea
和 运行 按钮的 Widget
的小部件:
class Tab1(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
self.group = Widget(self)
scroll = QtGui.QScrollArea(self)
scroll.setWidget(self.group)
scroll.setWidgetResizable(True)
self.layout().addWidget(scroll)
self.runBtn = QPushButton("Run", self)
self.layout().addWidget(self.runBtn)
然后将按钮添加到 QTabWidget
(您不再需要 tab1_initUI()
功能)
class Page1(QTabWidget):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.tab1 = Tab1()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab1, "Tab1")
self.addTab(self.tab2, "Tab2")
# ...
截图:
完整代码在下面link