赞
踩
import sys from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton class MainWindow(QMainWindow): nums = 1 def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.resize(400, 250)#窗口大小 self.setWindowTitle('按钮信号与槽')#标题 self.pushButton = QPushButton('按钮', self)#创建按钮 self.pushButton.setGeometry(150,100,60,40)#x,y,w,h self.pushButton.resize(100,50)#重新设置大小 self.pushButton.clicked.connect(self.onPushButtonClicked)#点击事件,连接到onPushButton槽函数 def onPushButtonClicked(self): print("按钮被点击",self.nums) self.nums+=1 if __name__ == '__main__': app = QApplication(sys.argv) main = MainWindow() main.show() sys.exit(app.exec_())
点击按钮,控制台输出“按钮被点击”
信息。
更多内容可参考:
[Python自学] PyQT5-信号与槽
[python]PyQt5-设置按钮(按钮信号与槽)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。