PyQt5 QSpinBox - 清理文本
在本文中,我们将看到如何清理旋转框的文本,清理文本意味着删除任何数字之前的空格和零,因为我们知道 001 类似于 1,因此清理文本将从文本中删除不需要的零.
为了做到这一点,我们使用 cleanText 方法。
Syntax : spin_box.cleanText()
Argument : It takes no argument
Action Performed : It cleans the text of spin box
注意:不包括前缀、后缀或前导或尾随空格。
Implementation steps :
1. Create a main window
2. Create a spin box set its range prefix and suffix
3. Create a push button
4. Add action to the push button
5. Inside the action method call the cleanText method
下面是实现
# 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 range to the spin box
self.spin.setRange(0, 999999)
# setting prefix to spin
self.spin.setPrefix("Prefix ")
# setting suffix to spin
self.spin.setSuffix(" Suffix")
# creating a push button
push = QPushButton("Clean", self)
# setting geometry to the push button
push.setGeometry(100, 200, 100, 40)
# adding action to the push button
push.clicked.connect(self.push_action)
# method called by the push button
def push_action(self):
# cleaning the text
self.spin.cleanText()
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :