PyQt5 QSpinBox - 停止键盘输入
在本文中,我们将看到如何制作一个无法从键盘输入的旋转框,当我们创建一个旋转框时,基本上有两种方法可以更改旋转框的值,一种是使用箭头按钮或其他方法是使用键盘。停止旋转框的键盘输入意味着用户将无法使用键盘在旋转框中输入值,尽管用户可以使用箭头按钮进行递增或递减。
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
为此,我们必须执行以下操作:
1.创建一个主窗口
2.创建一个旋转框
3.从旋转框中获取行编辑对象
4. 将行编辑部分设为只读
下面是实现
# 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 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())
输出 :