創建的信號槽,並且運行你的代碼,這樣的事情:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#---------
# IMPORT
#---------
import sys
from PyQt4 import QtGui, QtCore
#---------
# DEFINE
#---------
class MyWindow(QtGui.QMainWindow):
_numberClicked = 0
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.centralwidget = QtGui.QWidget(self)
self.pushButtonClick = QtGui.QPushButton(self.centralwidget)
self.pushButtonClick.setText("Click Me!")
self.pushButtonClick.clicked.connect(self.on_pushButtonClick_clicked)
self.labelClicked = QtGui.QLabel(self)
self.layoutVertical = QtGui.QVBoxLayout(self.centralwidget)
self.layoutVertical.addWidget(self.pushButtonClick)
self.layoutVertical.addWidget(self.labelClicked)
self.statusbar = QtGui.QStatusBar(self)
self.statusbar.setObjectName("statusbar")
self.setCentralWidget(self.centralwidget)
self.setStatusBar(self.statusbar)
@QtCore.pyqtSlot()
def on_pushButtonClick_clicked(self):
self._numberClicked += 1
message = "Clicked {0} time(s)".format(self._numberClicked)
self.labelClicked.setText(message)
self.statusbar.showMessage(message, 1111)
#---------
# MAIN
#---------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.resize(333, 111)
main.show()
sys.exit(app.exec_())