PyQt:多个 QGridLayout
PyQt: Multiple QGridLayout
我怎样才能在一个小部件上有多个 QGridLayouts?我想左边一个网格布局,右边一个网格布局。
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class FormWidget(QWidget):
def __init__(self):
super(FormWidget, self).__init__( )
self.grid = QGridLayout(self)
self.grid2 = QGridLayout(self)
self.grid.addWidget(self.grid2)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FormWidget()
sys.exit(app.exec_())
如果您想水平放置 2 个布局,则必须使用 QHBoxLayout:
import sys
from PyQt5.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QWidget
class FormWidget(QWidget):
def __init__(self, parent=None):
super(FormWidget, self).__init__(parent)
left_grid_layout = QGridLayout()
right_grid_layout = QGridLayout()
lay = QHBoxLayout(self)
lay.addLayout(left_grid_layout)
lay.addLayout(right_grid_layout)
self.resize(640, 480)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = FormWidget()
ex.show()
sys.exit(app.exec_())
更新:
如果要设置权重,必须在拉伸中设置。
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QTextEdit, QWidget
class FormWidget(QWidget):
def __init__(self, parent=None):
super(FormWidget, self).__init__(parent)
left_grid_layout = QGridLayout()
right_grid_layout = QGridLayout()
# for testing
left_grid_layout.addWidget(QTextEdit())
right_grid_layout.addWidget(QTextEdit())
lay = QHBoxLayout(self)
lay.addLayout(left_grid_layout, stretch=1)
lay.addLayout(right_grid_layout, stretch=2)
lay.setContentsMargins(
0, # left
100, # top
0, # right
100 # bottom
)
self.resize(640, 480)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = FormWidget()
ex.show()
sys.exit(app.exec_())
我怎样才能在一个小部件上有多个 QGridLayouts?我想左边一个网格布局,右边一个网格布局。
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class FormWidget(QWidget):
def __init__(self):
super(FormWidget, self).__init__( )
self.grid = QGridLayout(self)
self.grid2 = QGridLayout(self)
self.grid.addWidget(self.grid2)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FormWidget()
sys.exit(app.exec_())
如果您想水平放置 2 个布局,则必须使用 QHBoxLayout:
import sys
from PyQt5.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QWidget
class FormWidget(QWidget):
def __init__(self, parent=None):
super(FormWidget, self).__init__(parent)
left_grid_layout = QGridLayout()
right_grid_layout = QGridLayout()
lay = QHBoxLayout(self)
lay.addLayout(left_grid_layout)
lay.addLayout(right_grid_layout)
self.resize(640, 480)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = FormWidget()
ex.show()
sys.exit(app.exec_())
更新:
如果要设置权重,必须在拉伸中设置。
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGridLayout, QHBoxLayout, QTextEdit, QWidget
class FormWidget(QWidget):
def __init__(self, parent=None):
super(FormWidget, self).__init__(parent)
left_grid_layout = QGridLayout()
right_grid_layout = QGridLayout()
# for testing
left_grid_layout.addWidget(QTextEdit())
right_grid_layout.addWidget(QTextEdit())
lay = QHBoxLayout(self)
lay.addLayout(left_grid_layout, stretch=1)
lay.addLayout(right_grid_layout, stretch=2)
lay.setContentsMargins(
0, # left
100, # top
0, # right
100 # bottom
)
self.resize(640, 480)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = FormWidget()
ex.show()
sys.exit(app.exec_())