简体   繁体   English

PyQt5 自定义按钮旁边有一个 label

[英]PyQt5 custom button with a label next to it

I want to define a custom button with label next to it, these two should be combined as a control and there should always be no space between and won't be affected by the layout or other widgets.我想定义一个自定义按钮,旁边有 label,这两个应该组合为一个控件,并且之间应该始终没有空格,并且不会受到布局或其他小部件的影响。


class ButtonCtrl(QWidget):
    """ Implemented button control with label and checkable property """
    toggled = pyqtSignal(bool)

    def __init__(self,  label='', func=None, parent=None):
        super().__init__(parent)
        col = QVBoxLayout(self)
        self.text = ['ON', 'OFF']
        self.label = QLabel(label, self)
        self.button = QPushButton('ON', self)
        col.addWidget(self.label)
        col.addWidget(self.button)
        # self.setLayout(col)
        col.setSpacing(0)
        self.setContentsMargins(0,0,0,0)
        self.adjustSize()
        # Defaultly False
        self.button.setCheckable(True)
        self.button.setChecked(False)
        self.button.setStyleSheet('''QPushButton[text='OFF']{ background-color:red; font-weight:bold; font-size: 12pt; font-family: Microsoft YaHei} QPushButton[text='ON']:checked{background-color: green; font-weight:normal}''')

        self.updateStatus(False)
        self.label.setStyleSheet('QLabel {font-size: 14pt; font-weight: bold; font-family: Microsoft YaHei}')
        # self.label.setFont(QFont('Microsoft YaHei', 14,99))
        # self.button.setFont(QFont('Microsoft YaHei', 12, 50))
        self.button.toggled.connect(self.toggled.emit)
        self.toggled.connect(self.updateStatus)
        if func:
            self.toggled.connect(func)

    def setLabel(self, label=''):
        self.label.setText(label)

    def setChecked(self, state):
        self.button.setChecked(state)

    def setStatusText(self, on='ON', off='OFF'):
        self.text = [on, off]
        self.updateStatus(self.button.isChecked())

    def status(self):
        return self.button.isChecked()

    def updateStatus(self, state):
        if state:
            self.button.setText(self.text[0])
        else:
            self.button.setText(self.text[-1])


However, the label is quite far away from the button control if it is added to the main window within a QHBoxLayout and there is too much space for this custom button.但是,如果将 label 添加到 QHBoxLayout 内的主 window 中,则与按钮控件相距甚远,并且此自定义按钮的空间太大。 In addition, if I set the alignment of the custom button as center, all of them will be aligned to center, and other alignments work as well.此外,如果我将自定义按钮的 alignment 设置为中心,它们都将对齐到中心,其他对齐方式也可以。 However, this is not exactly what I want, I hope the button and label will always be closely next to each other.但是,这并不是我想要的,我希望按钮和 label 永远紧挨着。 Any elegant solutions?任何优雅的解决方案?

在此处输入图像描述

You can add a stretch before the first widget and after the last:您可以在第一个小部件之前最后一个小部件之后添加拉伸:


class ButtonCtrl(QWidget):
    """ Implemented button control with label and checkable property """
    toggled = pyqtSignal(bool)

    def __init__(self,  label='', func=None, parent=None):
        super().__init__(parent)
        col = QVBoxLayout(self)
        col.addStretch()
        self.text = ['ON', 'OFF']
        self.label = QLabel(label, self)
        self.button = QPushButton('ON', self)
        col.addWidget(self.label)
        col.addWidget(self.button)
        col.setSpacing(0)
        col.addStretch()
        # ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM