Python 3.x QT Designer如何为QPushButton(python3)分配2个热键

Python 3.x QT Designer如何为QPushButton(python3)分配2个热键,python-3.x,keyboard-shortcuts,qt-designer,pyqt5,qpushbutton,Python 3.x,Keyboard Shortcuts,Qt Designer,Pyqt5,Qpushbutton,我创建了一个简单的GUI,并将“回车”指定给QPushButton(“开始”)。这是它的底线: self.Begin.setShortcut(_translate("Form", "Enter")) Evrything工作正常,但是如何为同一个按钮分配两种热键?我想让按钮对两个热键做出反应:输入和返回(NumPad上通常的“大输入”和“小输入”) 提前感谢。有几种方法可以做到这一点。可能最简单的方法是使用: 要获取按钮动画行为,请尝试以下操作: QShortcut(Qt.Key_Enter,

我创建了一个简单的GUI,并将“回车”指定给QPushButton(“开始”)。这是它的底线:

self.Begin.setShortcut(_translate("Form", "Enter"))
Evrything工作正常,但是如何为同一个按钮分配两种热键?我想让按钮对两个热键做出反应:输入和返回(NumPad上通常的“大输入”和“小输入”)


提前感谢。

有几种方法可以做到这一点。可能最简单的方法是使用:

要获取按钮动画行为,请尝试以下操作:

QShortcut(Qt.Key_Enter, self.Begin, self.Begin.animateClick)
QShortcut(Qt.Key_Return, self.Begin, self.Begin.animateClick)

您可以使用qshortcutone来代替QKeySequence类:创建两个当按下每个enter键时触发的QShortcut;然后,用QPushButton的
点击
槽链接这些对象的每个
激活的
信号:

# This one is for the big key
# Creation of the QShortcut, big_enter_seq is an intermediate
big_enter_seq = QKeySequence(Qt.Key_Return)
big_enter = QShortcurt(big_enter_seq, self.Begin)
# Linking
big_enter.activated.connect(self.Begin.click)
big_enter.activatedAmbiguously.connect(self.Begin.click)

# This one is for the keypad key
small_enter_seq = QKeySequence(Qt.Key_Enter)
small_enter = QShortcut(small_enter_seq, self.Begin)
small_enter.activated.connect(self.Begin.click)
small_enter.activatedAmbiguously.connect(self.Begin.click)

请注意,
模糊地激活了信号:我把你送到办公室去理解。理论上,根据Qt5文档,此代码应该可以工作,但在我的计算机上(Gnome 3.14.0的Fedora 21)无法识别numpad键。。。问题在于
Qt.Key\u Enter
,根据,它应该指的是好的键。。。告诉我它是否在你的电脑上工作

多谢各位。你的回答简短明了。请将其更正为:[code]qtwidts.QShortcut(QtCore.Qt.Key_Enter,self.Begin,self.Begin.animateClick)qtwidts.QShortcut(QtCore.Qt.Key_Return,self.Begin.animateClick)PyQt 5.4中的“code”在qtwidts中,而不是在qtguit中谢谢,我将使用下一个答案,因为它更简单,但你的书读起来很有趣。我不明白一件事:如果我们不在代码中的任何其他地方使用big_enter_seq,为什么我们需要创建big_enter_seq=QKeySequence(Qt.Key_Return)。也许这就是为什么它在你的情况下不起作用。您的第二行可能应该是这样的:big_enter=QShortcur(big_enter seq,self.Begin)是的,我很抱歉,我犯了一个错误,但现在我已经修复了它!在您的计算机上,Qt.Key\u返回是否工作,而numpad输入是否工作?不,不幸的是,它不工作。我犯了一个错误:big\u enter\u seq=QKeySequence(Qt.Key\u Return)name错误:没有定义名称“Qt”(我使用的是Qt 5.4),您必须从PyQt5.QtCore导入Qt
,在代码的开头!ekhumoro的解决方案有效吗?谢谢。1) 从PyQt5.QtCore中添加后,导入Qt并纠正аa拼写错误(QShortcurt)。您的解决方案工作得非常好(输入和返回)。(但没有Ekhumoro answer中的按钮动画)。2) 是的,Ekhumoro的解决方案很好,但我必须更正为qtwidts.QShortcut而不是他的QtGui.QShortcut-可能是因为我使用的是qt5,而他(可能)使用的是qt4。3) 我感谢你们两位的时间和帮助@埃库马罗
# This one is for the big key
# Creation of the QShortcut, big_enter_seq is an intermediate
big_enter_seq = QKeySequence(Qt.Key_Return)
big_enter = QShortcurt(big_enter_seq, self.Begin)
# Linking
big_enter.activated.connect(self.Begin.click)
big_enter.activatedAmbiguously.connect(self.Begin.click)

# This one is for the keypad key
small_enter_seq = QKeySequence(Qt.Key_Enter)
small_enter = QShortcut(small_enter_seq, self.Begin)
small_enter.activated.connect(self.Begin.click)
small_enter.activatedAmbiguously.connect(self.Begin.click)