PyQt4 中的事件與信號(hào)
在這一節(jié)中,將會(huì)接觸到程序中事件和信號(hào)的 we will explore events and singnals occuring in applications. 事件
事件是gui程序中最重要的部分。事件是由用或系統(tǒng)發(fā)出的。當(dāng)我們調(diào)用程序的
事件源 是發(fā)生狀態(tài)改變改變的對(duì)象。它產(chǎn)生事件。事件對(duì)象將事件源狀態(tài)變化封裝起來(lái)。事件目標(biāo)是需要告知的對(duì)象。事件源對(duì)象將事件傳送給事件目標(biāo)處理。
當(dāng)我們調(diào)用 新 API
PyQt4.5 引入信號(hào)與槽的一種新的API 風(fēng)格。 QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'), self.onClicked) 上邊是老式的API button.clicked.connect(self.onClicked) 這是新式的更符合python標(biāo)準(zhǔn)。 信號(hào)&槽
這是一個(gè)展示PyQt4中的信號(hào)與槽。 #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we connect a signal of a QtGui.QSlider to a slot of a QtGui.QLCDNumber. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui, QtCore class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): lcd = QtGui.QLCDNumber(self) sld = QtGui.QSlider(QtCore.Qt.Horizontal, self) vbox = QtGui.QVBoxLayout() vbox.addWidget(lcd) vbox.addWidget(sld) self.setLayout(vbox) sld.valueChanged.connect(lcd.display) self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Signal & slot') self.show() def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
In our example, we display a sld.valueChanged.connect(lcd.display)
Here we connect a The sender is an object that sends a signal. The receiver is the object, that receives the signal. The slot is the method, that reacts to the signal. Figure: Signal & slot
重載事件處理器PyQt4 事件處理通常要重載事件處理器。 #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we reimplement an event handler. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui, QtCore 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') self.show() def keyPressEvent(self, e): if e.key() == QtCore.Qt.Key_Escape: self.close() def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
在我們的例子里, 我們重載了 def keyPressEvent(self, e): if e.key() == QtCore.Qt.Key_Escape: self.close() 如果按了esc鍵,程序結(jié)束。 事件發(fā)送者
有時(shí)候需要知道信號(hào)的發(fā)送者,為了方便使用PyQt4 有一個(gè) #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we determine the event sender object. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui, QtCore class Example(QtGui.QMainWindow): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): btn1 = QtGui.QPushButton("Button 1", self) btn1.move(30, 50) btn2 = QtGui.QPushButton("Button 2", self) btn2.move(150, 50) btn1.clicked.connect(self.buttonClicked) btn2.clicked.connect(self.buttonClicked) self.statusBar() self.setGeometry(300, 300, 290, 150) self.setWindowTitle('Event sender') self.show() def buttonClicked(self): sender = self.sender() self.statusBar().showMessage(sender.text() + ' was pressed') def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
在我們例子里有兩個(gè)按鈕。在 buttonClicked()方法里我們判斷哪個(gè)按鈕被按下使用了 btn1.clicked.connect(self.buttonClicked) btn2.clicked.connect(self.buttonClicked) 兩個(gè)按鈕都被連接到了相同的槽函數(shù)。 def buttonClicked(self): sender = self.sender() self.statusBar().showMessage(sender.text() + ' was pressed') 我們使用 Figure: Event sender
發(fā)出信號(hào)
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we show how to emit a custom signal. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui, QtCore class Communicate(QtCore.QObject): closeApp = QtCore.pyqtSignal() class Example(QtGui.QMainWindow): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.c = Communicate() self.c.closeApp.connect(self.close) self.setGeometry(300, 300, 290, 150) self.setWindowTitle('Emit signal') self.show() def mousePressEvent(self, event): self.c.closeApp.emit() def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main() 我們創(chuàng)建了一個(gè)名叫 class Communicate(QtCore.QObject): closeApp = QtCore.pyqtSignal()
創(chuàng)建了一個(gè) self.c = Communicate() self.c.closeApp.connect(self.close) 一個(gè)Communicate類的實(shí)例被創(chuàng)建。我們 def mousePressEvent(self, event): self.c.closeApp.emit()
當(dāng)在窗口上點(diǎn)擊鼠票時(shí),
|
|
來(lái)自: 學(xué)海無(wú)涯GL > 《Python》