PyQt5 QSpinBox – 访问前缀
在本文中,我们将了解如何访问微调框的前缀,前缀基本上是插入在微调框值之前的文本,前缀对于微调框的每个值都保持不变,即不可编辑。默认情况下,旋转框没有设置前缀,我们使用 setPrefix 方法设置前缀。为了访问前缀,我们将使用前缀方法。
Syntax : spin_box.prefix() Argument : It takes no argument Return : It returns string
实现步骤: 1. 创建旋转框 2. 为旋转框添加前缀 3. 创建标签以显示前缀 4. 借助前缀方法访问前缀 5. 使用 setText 方法在标签中显示前缀 下面是实现
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())
输出 :