我剛剛從here閱讀了關於pyqt5按鈕的教程。代碼如下。有一個關於button.clicked.connect(self.on_click)
和@pyqtSlot()
的問題。如果我從代碼中刪除@pyqtSlot()
,它仍然有效。但是,如果我從代碼中刪除button.clicked.connect(self.on_click)
,該按鈕不起作用。那麼這個代碼中的@pyqtSlot()
的功能是什麼?pyqtSlot的功能
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 button - pythonspot.com'
self.left = 10
self.top = 10
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(100,70)
button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
print('PyQt5 button click')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
請參閱[這裏](http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html#the-pyqtslot-decorator) – user3419537