Open In App
Related Articles

PyQt5 QSpinBox – Setting Left Margin

Improve Article
Save Article
Like Article

In this article we will see how we can set or change the left margin of the spin box, by default there is no margin set to the spin box i.e their margin values are zero. Although we can change that, setting margin to the spin box will not display any change in the spin box due to overriding take place by the widget to maintain its structure. But in order to set margin to the spin box we have set margin to the spin box’s line edit object by which we can display the margin in spin box. In order to do this we use setLeft method.

Syntax : margin.setLeft(n) Here margin is the QMargin object of the QLineEdit object which belongs to spin box 

Argument : It take integer as argument 

Return : It returns None

Implementation steps : 
1. Create a spin box 
2. Get the line edit object of the spin box 
3. Get the margin object from the spin box 
4. Set the left margin to the spin box 
5. Add this margin object back to the line edit object 

Below is the implementation 


# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
    def __init__(self):
        # setting title
        self.setWindowTitle("Python ")
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
        # calling method
        # showing all the widgets
        # method for widgets
    def UiComponents(self):
        # creating spin box
        self.spin = QSpinBox(self)
        # setting geometry to spin box
        self.spin.setGeometry(100, 100, 250, 40)
        # setting range to the spin box
        self.spin.setRange(0, 999999)
        # setting prefix to spin
        self.spin.setPrefix("Prefix ")
        # setting suffix to spin
        self.spin.setSuffix(" Suffix")
        # getting line edit object
        line_edit = self.spin.lineEdit()
        # getting margins
        margin = line_edit.contentsMargins()
        # setting left margin
        # setting this margin back to line edit
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 10 Feb, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials