📜  PyQt5 - CheckBox 的圆形指示器

📅  最后修改于: 2022-05-13 01:55:21.220000             🧑  作者: Mango

PyQt5 - CheckBox 的圆形指示器

在本文中,我们将看到如何创建复选框的圆形指示器。默认情况下,复选框有方形指示符,尽管在样式表的帮助下我们可以将其更改为圆形。下面是普通指标与复选框圆形指标的表示。

为此,我们必须执行以下操作:

  1. 当我们尝试更改形状边框时,将边框设置为指示器会消失,因此为了处理它,我们将为其提供自定义边框。
  2. 设置指标的大小,它应该是方形的。
  3. 将边框半径设置为指示器长度和边框厚度的一半的总和,即
    radius = width/2 + thickness

样式表代码 –

QCheckBox::indicator
{
border : 2px solid black;
width : 20px;
height : 20px;
border-radius : 22px;
}

下面是实现——

# 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 the check-box
        checkbox = QCheckBox('Geek ?', self)
  
        # setting tristate check box
        checkbox.setTristate(True)
  
        # setting geometry of check box
        checkbox.setGeometry(200, 150, 100, 40)
  
        # adding border to the check box and padding
        # adding border to indicator and setting size
        # setting border radius
        checkbox.setStyleSheet("QCheckBox"
                               "{"
                               "border : 2px solid clack;"
                               "padding : 5px;"
                               "}"
                               "QCheckBox::indicator"
                               "{"
                               "border : 2px solid black;"
                               "width : 20px;"
                               "height : 20px;"
                               "border-radius :12px;"
                               "}")
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :