J
J
Jairman2020-06-19 17:18:06
Python
Jairman, 2020-06-19 17:18:06

How to align a button in pyqt5?

I need to align the button to the bottom left. I tried to do this for the test:
self.verticalLayout.addWidget(button, alignment=QtCore.Qt.AlignRight)

But writes AttributeError: 'main' object has no attribute 'verticalLayout'

This is how I create the class: class main(QWidget):

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
StudMG, 2020-06-19
@Jairman

In order to use the self.verticalLayout attribute, you must first assign it the value of a Layout object from PyQt.
self.name is an attribute of your class, only instead of name, verticalLayout.

from PyQt5 import QtWidgets, QtCore


class WindowLayout(object):
    def __init__(self):
        button = QtWidgets.QWidget
        self.verticalLayout = QtWidgets.QVBoxLayout(parent)
        self.verticalLayout.addWidget(button, alignment=QtCore.Qt.AlignRight)

parent - the widget object to which the Layout belongs, if there is one, for example TabMenu
If not, then no need to specify

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question