📅  最后修改于: 2023-12-03 14:45:48.237000             🧑  作者: Mango
PyQt5是Python编程语言中的GUI工具包之一,它允许程序员使用Python创建不同类型的GUI应用程序。其中之一是QCommandLinkButton,它是QPushButton的子类,可以创建具有标签和描述的命令链接按钮。
在本文中,我们将了解如何为PyQt5中的QCommandLinkButton组合的Checked和Hover状态设置背景颜色。
对于选中的QCommandLinkButton,我们可以将其背景颜色更改为所需的颜色。可以使用setStyleSheet()方法和QCommandLinkButton::checked伪状态来设置选中的状态下的背景颜色。
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QCommandLinkButton, QWidget
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 300, 150)
self.setWindowTitle('QCommandLinkButton')
self.setWindowIcon(QIcon('web.png'))
button = QCommandLinkButton('Command Link Button', self)
button.setGeometry(50, 50, 200, 40)
button.setStyleSheet("""
QCommandLinkButton:checked {
background-color: red;
}
""")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
上面的代码将创建一个QCommandLinkButton,若其被选中,其背景颜色将变为红色。
对于悬停的QCommandLinkButton,我们可以使用setStyleSheet()方法和QCommandLinkButton::hover伪状态来设置悬停状态下的背景颜色。
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QCommandLinkButton, QWidget
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 300, 150)
self.setWindowTitle('QCommandLinkButton')
self.setWindowIcon(QIcon('web.png'))
button = QCommandLinkButton('Command Link Button', self)
button.setGeometry(50, 50, 200, 40)
button.setStyleSheet("""
QCommandLinkButton:hover {
background-color: green;
}
""")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
上面的代码将创建一个QCommandLinkButton,当其被悬停时,其背景颜色将变为绿色。
我们可以使用QCommandLinkButton::checked和QCommandLinkButton::hover伪状态同时设置背景颜色。
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QCommandLinkButton, QWidget
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 300, 150)
self.setWindowTitle('QCommandLinkButton')
self.setWindowIcon(QIcon('web.png'))
button = QCommandLinkButton('Command Link Button', self)
button.setGeometry(50, 50, 200, 40)
button.setStyleSheet("""
QCommandLinkButton:checked {
background-color: red;
}
QCommandLinkButton:hover {
background-color: green;
}
""")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
上面的代码将创建一个QCommandLinkButton,当其被选中时,背景颜色将变为红色;当其被悬停时,背景颜色将变为绿色。
这是如何为PyQt5中的QCommandLinkButton组合的Checked和Hover状态设置背景颜色的简短指南。