How do you level the text of the button?



  • Just started studying. PyQt and Qt Designer in particular, but faced a problem.

    I want to create a sign button. + in the middle.

    That's what I created. QPushButton and added the text +
    As is clear from the picture below, the text is down to the bottom of the button.

    How can you put it in the middle?

    QPushButton с текстом "+"б смещенным вниз от центра


    What I tried:

    vertical-align: center; - эффекта нет
    text-align: center;     - выравнивает, но только по горизонтали
    margin: 10px;           - желаемого результата тоже не дает (см картинку ниже)

    введите сюда описание изображения

    I understand that I can do my job by adding a bacon, but I want to know if there's a way to do it through the button itself.



  • It's not easy if you change the sizes very much. font♪ The text is based on the bounding rectangle. In Qt Designer, you won't do that.

    Here's a simple option:

    import sys
    from PyQt5.Qt import *
    

    class Window(QWidget):
    def init(self):
    super().init()

        layout = QHBoxLayout(self)
        layout.addWidget(QPushButton(
            objectName="BlueButton",     
            clicked=lambda: print('Hello'))
        )
    

    StyleSheet = '''
    QPushButton {
    border-image: url(plus.png);
    }
    #BlueButton {
    background-color: #2196f3;
    min-width: 100px;
    max-width: 100px;
    min-height: 100px;
    max-height: 100px;
    border-radius: 50px;
    }
    #BlueButton:hover {
    background-color: #64b5f6;
    }
    #BlueButton:pressed {
    background-color: #bbdefb;
    }
    '''

    if name == "main":
    app = QApplication(sys.argv)
    app.setStyleSheet(StyleSheet)
    w = Window()
    w.show()
    sys.exit(app.exec_())

    введите сюда описание изображения


    plus.png

    введите сюда описание изображения



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2