如何向第三方对象的现有方法添加功能?你知道吗
我不确定这个问题是否表达正确,所以这里有一个我想要达到的目标的例子。你知道吗
以下功能用于闪烁按钮:
def clickColor(button, color):
beforeColor = button.palette().color(QPalette.Background)
button.setStyleSheet("background-color: %s" % color)
QTimer.singleShot(100, lambda: unClickColor(button, beforeColor))
def unClickColor(button, beforeColor):
button.setStyleSheet("background-color: %s" % beforeColor.name())
我希望PyQt5库的每个QPushButton在单击时都闪烁。你知道吗
我的想法是将clickColor函数添加到已单击。连接QPushButton的方法,但保持现有方法不变。你知道吗
做我想做的事的正确方法是什么?你知道吗
您可以创建一个自定义子类,然后在需要闪烁效果的任何地方使用它而不是普通的
QPushButton
。如果您使用的是Qt设计器,那么还可以使用widget promotion将添加到ui文件中的任何按钮替换为自定义类(有关详细信息,请参见this answer)。你知道吗下面是一个基本的演示脚本:
相关问题 更多 >
编程相关推荐