📌  相关文章
📜  PyQt5 QCommandLinkButton – 为按下状态设置边框

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

PyQt5 QCommandLinkButton – 为按下状态设置边框

在本文中,我们将了解如何根据按下状态为 QCommandLinkButton 设置边框。命令链接按钮是一种特殊类型的按钮,它具有按钮和单选按钮的特性。默认情况下,命令链接按钮没有额外的边框,尽管当它被按下时我们能够看到边框,我们也可以为其设置自定义边框。

基本上有两种按下状态,一种是按下状态,即按下命令链接按钮时,即单击,第二种是反按下状态,即未单击命令链接按钮时。

为此,我们将为它设置样式表代码,因为我们使用setStyleSheet方法和命令链接按钮对象,下面是样式表代码

QCommandLinkButton::pressed
{
border : 4px solid green;
}
QCommandLinkButton::!pressed
{
border : 2px solid black;
}

下面是实现

# 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, 500, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for components
    def UiComponents(self):
  
        # creating a command link button
        cl_button = QCommandLinkButton("Press", self)
  
        # setting geometry
        cl_button.setGeometry(250, 100, 200, 50)
  
        # setting style sheet
        # setting border to it for pressed and anti pressed state
        cl_button.setStyleSheet("QCommandLinkButton::pressed"
                                "{"
                                "border : 4px solid green;"
                                "}"
                                "QCommandLinkButton::! pressed"
                                "{"
                                "border : 2px solid black;"
                                "}")
  
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :