Animikh Aich
Animikh Aich

Reputation: 647

Add Padding to PushButton for PyQt5 in Python 3

I'd like to know how to add padding with respect to left, right, top and bottom edges of the PyQt window in python. I have found code for this in C++, But i haven't been able to make it work in python. Please Help me fix it.

I am using this code:

quit_button.setStyleSheet("padding:200px")

Unfortunately, does not seem to work, and it pushes the button to the top left corner of the window, and does not seem to move even when i change the padding pixels.

Note: I have tried using quit_button.setStyleSheet("padding-left:200px") and similarly padding-right, but all it does is makes the button label disappear.

End goal: I want the exit button to be on the right bottom edge of the window. But when i resize it, it should remain at the right bottom edge and not stay stagnant at it's place while the window resizes. In other words, I want it to behave like 'normal' windows applications behave.

Any help is appreciated. Thank you

Upvotes: 0

Views: 1965

Answers (1)

eyllanesc
eyllanesc

Reputation: 244359

A possible solution is to overwrite the resizeEvent() method and set the new button position

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        self.setMinimumSize(200, 200)
        self.quit_button = QPushButton("Quit", self)

    def resizeEvent(self, event):
        p = self.rect().bottomRight()-QPoint(20, 20) - self.quit_button.rect().bottomRight()
        self.quit_button.move(p)
        QWidget.resizeEvent(self, event)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_()) 

Upvotes: 1

Related Questions