Open In App

PyQt5 QSpinBox – Stopping key board input

Last Updated : 07 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 :



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads