PyQt5 QSpinBox – 检查值是否在中心
在本文中,我们将看到如何检查值是否出现在旋转框的中心,当我们创建旋转框时,值在左侧为了使值在中心,我们必须设置它的对齐方式标志居中。
为了做到这一点,我们将使用alignment
方法。
Syntax : spin_box.alignment()
Argument : It takes no argument
Return : It returns Qt object
实施步骤——
1.创建一个旋转框
2. 借助setAlignment
方法设置其对齐方式
3.创建标签以显示结果
4. 借助alignment
方法获取对齐
5.检查对齐是否居中然后更新标签
6. 显示对齐不居中 显示不在居中
下面是实现
# 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)
# alignment
alignment = Qt.AlignCenter
# setting alignment to centre
self.spin.setAlignment(alignment)
# creating label
label = QLabel(self)
# setting geometry to the label
label.setGeometry(100, 160, 200, 30)
# getting alignment
get_alignment = self.spin.alignment()
# checking if alignment is centre
if get_alignment == Qt.AlignCenter:
text = "Center Aligned"
else:
text = "Not center aligned"
# setting text to the label
label.setText(text)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :