Open In App

PyQt5 QSpinBox – Accessing Prefix

Last Updated : 28 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 setPrefix method to set the prefix. In order to access the prefix we will use prefix method.

Syntax : spin_box.prefix() 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 prefix method 5. Show the prefix in label using setText method Below is the implementation 

Python3




# 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 :



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads