PyQt5 QSpinBox – 设置底边距

PyQt5 QSpinBox – 设置底边距

在这篇文章中,我们将看到如何设置或改变旋转盒的底边距,默认情况下,旋转盒没有设置任何边距,即底边距值为零。尽管我们可以改变它,但由于小部件为保持其结构而进行的覆盖,给旋转盒设置保证金不会在旋转盒中显示任何变化。但是,为了给旋转盒设置边距,我们必须给旋转盒的行编辑对象设置边距,通过它我们可以在旋转盒中显示边距。

为了做到这一点,我们使用setBottom方法。

语法: margin.setBottom(n)
这里margin是属于旋转盒的QLineEdit对象的QMargin对象。

参数: 它以整数为参数。

返回: 它返回无。

实现步骤:

1.创建一个自旋盒

2.获取旋转盒的行编辑对象

3.从旋钮箱中获取边距对象

4.将底边距设置为旋转框

5.把这个边距对象添加到行编辑对象中。

下面是实现方法

# 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()
  
        # getting margins
        margin = line_edit.contentsMargins()
  
        # setting bottom margin
        margin.setBottom(30)
  
        # setting this margin back to line edit
        line_edit.setContentsMargins(margin)
  
  
# 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())

输出 :

PyQt5 QSpinBox - 设置底边距

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

PyQt5 计数器控件QSPINBox