gpt4 book ai didi

PyQt GridLayout 组合小部件

转载 作者:行者123 更新时间:2023-12-02 01:01:12 25 4
gpt4 key购买 nike

例如,我有一个 GUI,其中包含 label1+line_edit1label2+line_edit2按钮1按钮2按钮3。在正常意义上,代码看起来有点像这样:

class gridlayout_example(QtGui.QWidget):
def __init__(self):
self.grid_layout = QtGui.QGridLayout()

self.label1 = QtGui.QLabel("label1")
self.grid_layout.addWidget(self.label1,0,0,1,3)

self.line_edit1 = QtGui.QLineEdit()
self.grid_layout.addWidget(self.line_edit1,1,0,1,3)

self.label2 = QtGui.QLabel("label2")
self.grid_layout.addWidget(self.label1,2,0,1,3)

self.line_edit2 = QtGui.QLineEdit()
self.grid_layout.addWidget(self.line_edit2,3,0,1,3)

self.button1 = QtGui.QPushButton("button1")
self.button2 = QtGui.QPushButton("button2")
self.button3 = QtGui.QPushButton("button3")

self.grid_layout.addWidget(self.button1, 4,0,1,1)
self.grid_layout.addWidget(self.button2, 4,1,1,1)
self.grid_layout.addWidget(self.button3, 4,2,1,1)

self.setLayout(self.grid_layout)

但是有没有办法将 label1+line_edit1label2 + line_edit2 结合起来,使它变得像:

[label1                    
line edit1 ] -> (0,0,1,3)
[label2
line edit2 ] -> (1,0,1,3)
[button1][button2][button3] -> (2,x,1,1)

所以基本上 label1+line edit1 会占据网格布局的第 0 行,label2 + line edit2 占据 row1 等等...

最佳答案

创建第二个布局用作子布局,向其中添加小部件,然后使用 addLayout()代替 addWidget()

class gridlayout_example(QtGui.QWidget):
def __init__(self, parent=None):
super(gridlayout_example, self).__init__(parent)

label1 = QtGui.QLabel('label 1')
line_edit1 = QtGui.QLineEdit()
sublayout1 = QtGui.QVBoxLayout()
sublayout1.addWidget(label1)
sublayout1.addWidget(line_edit1)

label2 = QtGui.QLabel('label 2')
line_edit2 = QtGui.QLineEdit()
sublayout2 = QtGui.QVBoxLayout()
sublayout2.addWidget(label2)
sublayout2.addWidget(line_edit2)

button1 = QtGui.QPushButton("button1")
button2 = QtGui.QPushButton("button2")
button3 = QtGui.QPushButton("button3")

grid_layout = QtGui.QGridLayout(self)
grid_layout.addLayout(sublayout1, 0, 0, 1, 3)
grid_layout.addLayout(sublayout2, 1, 0, 1, 3)
grid_layout.addWidget(button1, 2, 0, 1, 1)
grid_layout.addWidget(button2, 2, 1, 1, 1)
grid_layout.addWidget(button3, 2, 2, 1, 1)

关于PyQt GridLayout 组合小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26664571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com