PyQt5 QSpinBox – 设置边距
在本文中,我们将看到如何为旋转框设置边距,默认情况下,旋转框没有边距设置,即它们的边距值为零。虽然我们可以改变这一点,但设置微调框的边距不会在微调框中显示任何更改,因为小部件会进行覆盖以保持其结构。但是为了给旋转框设置边距,我们已经为旋转框的行编辑对象设置了边距,通过它我们可以在旋转框中显示边距。
为此,我们使用setContentsMargins方法。
Syntax : line_edit.setContentsMargins(left, top, right, bottom)
Here line_edit is the QLineEdit object of the spin box
Argument : It takes four integer as argument
Return : It returns None
下面是实现
# 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")
# getting line edit object
line_edit = self.spin.lineEdit()
# setting margin to the line edit
line_edit.setContentsMargins(50, 20, 5, 5)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
window.spin.setFocus()
# start the app
sys.exit(App.exec())
输出 :