使用 QShortcut 时如何更改按钮的动画?
How can I change animation of button when using QShortcut?
以前,我习惯用button.clicked.connect(function)
触发一个按钮,用setStyleSheet()
设置qss
(例如:
QPushButton:pressed{background:#EAEAEA;}
)。当通过单击鼠标按下按钮并按预期工作时,按钮将改变背景颜色。但是,目前我正在使用 QShortcut
来触发按钮(例如:QtCore.Qt.Key_Return
),但按钮似乎不受 qss
.
的影响
我用过的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
Form.setStyleSheet(""
"#Form {\n"
"background-color: rgb(255, 255, 255);\n"
"}\n"
"QPushButton:hover {\n"
"color: white;\n"
"background-color: rgb(255, 85, 127);\n"
"border-radius: 6px;\n"
"font-size:16px;\n"
"}\n"
"\n"
" QPushButton:pressed{\n"
"background:#EAEAEA;\n"
"color: black;\n"
"font-size:16px;\n"
"}")
self.frame = QtWidgets.QFrame(Form)
self.frame.setGeometry(QtCore.QRect(20, 20, 351, 241))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.pushButton = QtWidgets.QPushButton(self.frame)
self.pushButton.setGeometry(QtCore.QRect(130, 100, 93, 28))
self.pushButton.setObjectName("pushButton")
# old (be affected by qss (QPushButton:pressed) when clicking mouse)
# self.pushButton.clicked.connect(self.send_message)
# new (not to be affected by qss (QPushButton:pressed) when using Enter key)
send_message_shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return), Form)
send_message_shortcut.activated.connect(self.send_message)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
def send_message(self):
# do something here
print("send message")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
您没有看到 "animation",因为根本没有按下按钮,但您可以使用 QAbstractButton.animateClick
:
触发它
send_message_shortcut.activated.connect(self.pushButton.animateClick)
这会设置按下的按钮(如 setDown(True)
),然后在默认的 100 毫秒间隔后恢复它并分别发送 released
和 clicked
信号。如果您想要不同的时间间隔,请使用 lambda(或用您自己的方法执行)并在 ms.
中设置参数
以前,我习惯用button.clicked.connect(function)
触发一个按钮,用setStyleSheet()
设置qss
(例如:
QPushButton:pressed{background:#EAEAEA;}
)。当通过单击鼠标按下按钮并按预期工作时,按钮将改变背景颜色。但是,目前我正在使用 QShortcut
来触发按钮(例如:QtCore.Qt.Key_Return
),但按钮似乎不受 qss
.
我用过的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
Form.setStyleSheet(""
"#Form {\n"
"background-color: rgb(255, 255, 255);\n"
"}\n"
"QPushButton:hover {\n"
"color: white;\n"
"background-color: rgb(255, 85, 127);\n"
"border-radius: 6px;\n"
"font-size:16px;\n"
"}\n"
"\n"
" QPushButton:pressed{\n"
"background:#EAEAEA;\n"
"color: black;\n"
"font-size:16px;\n"
"}")
self.frame = QtWidgets.QFrame(Form)
self.frame.setGeometry(QtCore.QRect(20, 20, 351, 241))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.pushButton = QtWidgets.QPushButton(self.frame)
self.pushButton.setGeometry(QtCore.QRect(130, 100, 93, 28))
self.pushButton.setObjectName("pushButton")
# old (be affected by qss (QPushButton:pressed) when clicking mouse)
# self.pushButton.clicked.connect(self.send_message)
# new (not to be affected by qss (QPushButton:pressed) when using Enter key)
send_message_shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return), Form)
send_message_shortcut.activated.connect(self.send_message)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
def send_message(self):
# do something here
print("send message")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
您没有看到 "animation",因为根本没有按下按钮,但您可以使用 QAbstractButton.animateClick
:
send_message_shortcut.activated.connect(self.pushButton.animateClick)
这会设置按下的按钮(如 setDown(True)
),然后在默认的 100 毫秒间隔后恢复它并分别发送 released
和 clicked
信号。如果您想要不同的时间间隔,请使用 lambda(或用您自己的方法执行)并在 ms.