2016-11-22 80 views
2

我正在製作一個有幾個用戶界面的項目,我想捕捉鍵盤事件並在最後一個UI中播放一些音樂。我已經展示了下面的代碼:在PyQt4中檢測鍵盤事件

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() 

我以爲我不得不處理上次代碼中的事件,但它沒有奏效。我必須在哪裏定義方法?

+0

我有一個類似的應用程序(音樂時,一個關鍵是strocken)。我強烈建議你去pyqt5,因爲pyqt4不再維護:http://julien.duponchelle.info/python/migration-from-pyqt4-to-pyqt5/ 我的麻煩少得多。我並沒有解決你的問題,但也許你最好花費用pyqt5解決它的問題,而不是pyqt4 ...... – Wli

+0

@Wli。 PyQt4仍然得到充分維護,並且沒有計劃停止支持它。 – ekhumoro

+0

真的嗎?我可能從錯誤的消息來源讀到...感謝您糾正我 – Wli

回答

0
  1. 將處理事件的小部件是當前「焦點」小部件。
  2. 你可以在任何你想要的地方處理你的KeyPressEvents。
  3. 您也可以在覆蓋的方法內製作類似QKeyEvent .ignore的東西,然後它的父親會照顧它,依此類推。
  4. 一個不錯的選擇是安裝一個EventFilter到你想要捕獲事件並在你的過濾管理器中處理它的對象。