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
Python3
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 ):
super ().__init__()
self .setWindowTitle("Python ")
self .setGeometry( 100 , 100 , 600 , 400 )
self .UiComponents()
self .show()
def UiComponents( self ):
self .spin = QSpinBox( self )
self .spin.setGeometry( 100 , 100 , 250 , 40 )
self .spin.setRange( 0 , 999999 )
self .spin.setPrefix("Prefix ")
self .spin.setSuffix(" Suffix")
line_edit = self .spin.lineEdit()
margin = line_edit.contentsMargins()
margin.setLeft( 50 )
line_edit.setContentsMargins(margin)
App = QApplication(sys.argv)
window = Window()
window.spin.setFocus()
sys.exit(App. exec ())
|
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