📌  相关文章
📜  PyQt5 QCommandLinkButton – 为其设置边框(1)

📅  最后修改于: 2023-12-03 15:03:57.530000             🧑  作者: Mango

PyQt5 QCommandLinkButton – 为其设置边框

QCommandLinkButton 是 PyQt5 中的一种 QPushButton。与 QPushButton 不同的是,QCommandLinkButton 通常用于生成一些需要详细说明的命令或操作,它通常包含一个主标题、一个次标题和一个命令链接。

在本文中,我们将介绍如何为 PyQt5 QCommandLinkButton 设置边框。首先,让我们展示一下如何创建一个简单的 QCommandLinkButton。

from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QCommandLinkButton

class Example(QDialog):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        vbox = QVBoxLayout()

        self.commandLinkButton = QCommandLinkButton('Command Link Button')
        
        vbox.addWidget(self.commandLinkButton)

        self.setLayout(vbox)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QCommandLinkButton')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

运行上面的代码,我们将会看到一个简单的窗口,其中包含一个 Command Link Button。

QCommandLinkButton_without_border

现在,我们将为这个按钮添加边框,我们可以使用 QPushButton 的样式表来设置边框。

self.commandLinkButton.setStyleSheet("border: 2px solid gray;")

现在,我们的 QCommandLinkButton 将具有灰色边框。

QCommandLinkButton_with_border

完整代码如下:

from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QCommandLinkButton

class Example(QDialog):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        vbox = QVBoxLayout()

        self.commandLinkButton = QCommandLinkButton('Command Link Button')
        self.commandLinkButton.setStyleSheet("border: 2px solid gray;")
        
        vbox.addWidget(self.commandLinkButton)

        self.setLayout(vbox)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QCommandLinkButton')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

现在,你已经学会了如何给 PyQt5 QCommandLinkButton 设置边框。