Friday, November 14, 2025
HomeLanguagesPyQt5 QSpinBox – Accessing Prefix

PyQt5 QSpinBox – Accessing Prefix

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 :

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS