检测PyQt4中的键盘事件

时间:2016-11-22 12:00:49

标签: python python-3.x user-interface pyqt4 keyboard-events

我正在制作一个带有一些UI的项目,我想在上一个UI中捕捉键盘事件并播放一些音乐。我已经展示了以下代码:

import sys
from PyQt4 import QtGui, QtCore
from pygame import mixer

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        self.setGeometry(300,300,250,150)
        self.setWindowTitle('Event handler')

    def keyPressEvent(self, e):

        if e.key() == QtCore.Qt.Key_W:
            mixer.init()
            mixer.music.load(r'C:\Users\qodud\Desktop\computingwork\bookA_Music\bookA_Music_W.mp3')
            mixer.music.stop()    

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()`

我有代码来定义UI类:

from PyQt4 import QtCore, QtGui
from pygame import mixer

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_bookA(QtGui.QWidget):
    def setupUi(self, bookA):
        bookA.setObjectName(_fromUtf8("bookA"))
        bookA.resize(400, 300)
        self.listWidget = QtGui.QListWidget(bookA)
        self.listWidget.setGeometry(QtCore.QRect(20, 20, 260, 260))
        self.listWidget.setObjectName(_fromUtf8("listWidget"))
        item = QtGui.QListWidgetItem()
        self.listWidget.addItem(item)
        self.backButton = QtGui.QPushButton(bookA)
        self.backButton.setGeometry(QtCore.QRect(300, 220, 80, 20))
        self.backButton.setObjectName(_fromUtf8("backButton"))
        self.pauseButton = QtGui.QPushButton(bookA)
        self.pauseButton.setGeometry(QtCore.QRect(300, 250, 80, 20))
        self.pauseButton.setObjectName(_fromUtf8("pauseButton"))

        self.retranslateUi(bookA)
        QtCore.QMetaObject.connectSlotsByName(bookA)

    def retranslateUi(self, bookA):
        bookA.setWindowTitle(_translate("bookA", "bookA", None))
        self.backButton.setText(_translate("bookA", "Back", None))
        self.pauseButton.setText(_translate("bookA", "Pause", None))
        __sortingEnabled = self.listWidget.isSortingEnabled()
        self.listWidget.setSortingEnabled(False)
        item = self.listWidget.item(0)
        item.setText(_translate("Form", "저자 P.15 사랑하는 이들에게 - 정재형    \n"
"\n"
"욕심 P.66 1997 Spring - Yoshimata Ryo\n"
"\n"
"편지 P.79 River Flows In You - 이루마\n"
"\n"
"낭만 실조 P.80 Kiss the Rain - 이루마\n"
"\n"
"어떤 날 P.89 History -Yoshimata Ryo\n"
"\n"
"빈 소원 P.100 The Whole Nine Yards -Yoshimata Ryo", None))
        self.listWidget.setSortingEnabled(__sortingEnabled)

    def keyPressEvent(self,e):

        if e.key() == QtCore.Qt.Key_W :
            mixer.init()
            mixer.music.load(r'C:\Users\qodud\Desktop\computingwork\bookA_Music\bookA_Music_W.mp3')
            mixer.music.play()


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    bookA = QtGui.QWidget()
    ui = Ui_bookA()
    ui.setupUi(bookA)
    bookA.show()
    sys.exit(app.exec_())

最后一个模块导入所有UI并在UI之间交换信号:

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QMainWindow
from PyQt4.QtCore import pyqtSignature
from pygame import mixer
import sys

from first import Ui_First
from mymusicbook import Ui_mymusicBook
from booka import Ui_bookA
from bookb import Ui_bookB
from bookc import Ui_bookC

class First(QMainWindow,Ui_First,QtGui.QWidget):

    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)

        self.popDialog1=mymusicBook()

    def on_startButton_clicked(self):
        self.popDialog1.show()

    def on_exitButton_clicked(self):
        self.close()

class mymusicBook(QMainWindow,Ui_mymusicBook):

    def __init__(self, parent = None):
        QMainWindow.__init__(self,parent)
        self.setupUi(self)

        self.popDialog2=bookA()
        self.popDialog3=bookB()
        self.popDialog4=bookC()

    def on_book_A_Button_clicked(self):
        self.popDialog2.show()


    def on_book_B_Button_clicked(self):
        self.popDialog3.show()

    def on_book_C_Button_clicked(self):
        self.popDialog4.show()

class bookA(QMainWindow,Ui_bookA):

    def __init__(self, parent = None):
        QMainWindow.__init__(self,parent)
        self.setupUi(self)

    def on_backButton_clicked(self):
        self.close()

    def keyPressEvent(self,e):

        if self.e.key() == QtCore.Qt.Key_W :
            mixer.init()
            mixer.music.load(r'C:\Users\qodud\Desktop\computingwork\bookA_Music\bookA_Music_W.mp3')
            mixer.music.stop()

class bookB(QMainWindow,Ui_bookB):

    def __init__(self, parent = None):
        QMainWindow.__init__(self,parent)
        self.setupUi(self)

    def on_backButon_clicked(self):
        self.close()

class bookC(QMainWindow,Ui_bookC):

    def __init__(self,parent=None):
        QMainWindow.__init__(self,parent)
        self.setupUi(self)

    def on_backButton_clicked(self):
        self.close()

我以为我必须在最后一段代码中处理事件,但它没有用。我在哪里定义方法?

1 个答案:

答案 0 :(得分:0)

  1. 将处理事件的窗口小部件是当前“聚焦”窗口小部件。
  2. 您可以随时随地处理KeyPressEvents。
  3. 你也可以制作像QKeyEvent这样的东西。忽略覆盖的方法然后它的父亲会照顾它,依此类推。
  4. 一个很好的选择是在您要捕获事件的对象上安装EventFilter并在过滤器管理器中处理它。