如何设置标签之间的固定距离

How to set a fixed distance between the label

如何调整标签和布局,使标签之间的边距保持不变?我在编辑器中尝试了不同的选项,但没有任何效果。标签之间的大小是固定的。我不知道如何减少它。带上代码。

我的自动添加标签到布局的功能。谢谢你的帮助。

def auto_label(self, number, color, text):

    test_label = QLabel()
    test_label.setScaledContents(True)
    test_label.setFixedWidth(50)
    test_label.setFixedHeight(50)
    #test_label.setSizePolicy(1, 10)
    font = QtGui.QFont()
    font.setPointSize(14)
    font.setBold(True)
    font.setWeight(75)
    test_label.setFont(font)
    test_label.setAlignment(QtCore.Qt.AlignCenter)

    if color == "blue":
        test_label.setStyleSheet('background: blue')
    if color == "red":
        test_label.setStyleSheet('background: red')
    if color == "gray":
        test_label.setStyleSheet('background: gray')
    if color == "green":
        test_label.setStyleSheet('background: green')
    if color == "yellow":
        test_label.setScaledContents(True)
        test_label.setPixmap(QPixmap('images/yu.png'))
    if color == "yellow1":
        test_label.setScaledContents(True)
        test_label.setPixmap(QPixmap('images/ya.png'))
    if color == "yellow2":
        test_label.setScaledContents(True)
        test_label.setPixmap(QPixmap('images/yo.png'))
    if color == "yellow3":
        test_label.setScaledContents(True)
        test_label.setPixmap(QPixmap('images/ye.png'))
    if color == "white":
        test_label.setStyleSheet('background: white')
    #self.centralwidget.resize(width, 50)
    self.horizontalLayout_2.insertWidget(number, test_label)

我的UI代码:

def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(723, 367)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
    self.verticalLayout_2.setObjectName("verticalLayout_2")
    self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_2.setSpacing(10)
    self.horizontalLayout_2.setObjectName("horizontalLayout_2")
    self.verticalLayout_2.addLayout(self.horizontalLayout_2)
    self.gridLayout = QtWidgets.QGridLayout()
    self.gridLayout.setObjectName("gridLayout")
    self.inputLine = QtWidgets.QLineEdit(self.centralwidget)
    font = QtGui.QFont()
    font.setFamily("Times New Roman")
    font.setPointSize(50)
    self.inputLine.setFont(font)
    self.inputLine.setAutoFillBackground(True)
    self.inputLine.setInputMethodHints
        (QtCore.Qt.ImhPreferLowercase|QtCore.Qt.ImhPreferUppercase)
    self.inputLine.setText("")
    self.inputLine.setFrame(False)
    self.inputLine.setAlignment(QtCore.Qt.AlignCenter)
    self.inputLine.setClearButtonEnabled(False)
    self.inputLine.setObjectName("inputLine")
    self.gridLayout.addWidget(self.inputLine, 0, 0, 1, 1)
    self.enterButton = QtWidgets.QPushButton(self.centralwidget)
    self.enterButton.setObjectName("enterButton")
    self.gridLayout.addWidget(self.enterButton, 1, 0, 1, 1)
    self.verticalLayout_2.addLayout(self.gridLayout)
    MainWindow.setCentralWidget(self.centralwidget)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

图片有问题

您可以使用 QHboxLayout 来达到这个目的。

QHboxLayout 为其所有子项提供相同的宽度,并在添加新子项时自动调整所有子项的宽度,在这种情况下 QLabel(除非修改)