Bac*_*b32 3 python pyqt python-3.x pyqt5 qlayout
我正在尝试为QHBoxLayout. 为了详细说明,我需要为我的水平布局设置一个特定的高度。但是,我找不到这样做的正确方法。我该怎么做才能做到这一点?
hbox1 = QHBoxLayout()
Run Code Online (Sandbox Code Playgroud)
正如@ekhumuro在 QHBoxLayout 中指出的,您不能设置固定高度,您必须对将包含它的小部件执行此操作,如下所示:
import random
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setFixedHeight(300)
lay = QtWidgets.QHBoxLayout(self)
for letter in "ABCDEFG":
label = QtWidgets.QLabel(letter, alignment=QtCore.Qt.AlignCenter)
color = QtGui.QColor(*[random.randint(0, 255) for _ in range(3)])
label.setStyleSheet("background-color: {}".format(color.name()))
lay.addWidget(label)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)