In this article we will see how we can access the prefix of the spin box, prefix is basically the text which is inserted before the value of spin box, prefix remain constant for each value of spin box i.e it is not editable. By default no prefix is set to the spin box we use setPrifix
method to set the prefix.
In order to access the prefix we will use prifix
method.
Syntax : spin_box.prifix()
Argument : It takes no argument
Return : It returns string
Implementation steps :
1. Create a spin box
2. Add prefix to the spin box
3. Create label to show the prefix
4. Access the prefix with the help of prifix
method
5. Show the prefix in label using setText
method
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 ): super ().__init__() # setting title self .setWindowTitle( "Python " ) # setting geometry self .setGeometry( 100 , 100 , 600 , 400 ) # calling method self .UiComponents() # showing all the widgets self .show() # method for widgets def UiComponents( self ): # creating spin box self .spin = QSpinBox( self ) # setting geometry to spin box self .spin.setGeometry( 100 , 100 , 150 , 40 ) # adding prefix in combo box self .spin.setPrefix( "Value of x : " ) # creating label to show the prefix label = QLabel( self ) # setting geometry of label label.setGeometry( 100 , 160 , 200 , 30 ) # getting prefix of spin box text = self .spin.prefix() # showing prefix through label label.setText( "Prefix = \" " + text + "\"" ) # create pyqt5 app App = QApplication(sys.argv) # create the instance of our Window window = Window() # start the app sys.exit(App. exec ()) |
Output :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.