Friday, November 14, 2025
HomeLanguagesPyQt5 QSpinBox – Stopping key board input

PyQt5 QSpinBox – Stopping key board input

In this article we will see how we can make a spin box which can’t take input from the keyboard, when we create a spin box there are basically two ways to change the value of the spin box one is by using arrow buttons or other way is using key board. Stopping key board input of spin box means user will not be able to enter a value in spin box using keyboard although user can make increment or decrement using arrow buttons.

In order to stop key board input we use setReadOnly method with the line edit object Syntax : line_edit.setReadOnly(True) Argument : It takes bool as argument Action performed : It will make the line edit object read only

In order to do this we have to do the following : 1. Create a main window 2. Create a spin box 3. Get the line edit object from the spin box 4. Make the line edit part read only 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, 250, 40)
 
        # setting prefix to spin
        self.spin.setPrefix("Prefix ")
 
        # setting suffix to spin
        self.spin.setSuffix(" Suffix")
 
        # getting the line edit
        line = self.spin.lineEdit()
 
        # making the line edit part read only
        line.setReadOnly(True)
 
 
# 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