# -*- coding: utf-8 -*-
# 导入PyQt5库和相关模块
import PyQt5
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QImage, QPixmap
# 导入自定义的UI文件
from ui_Dialog import Ui_Dialog
# 定义一个自定义窗口类MyWindow,继承自QMainWindow和Ui_Dialog
class MyWindow(QMainWindow, Ui_Dialog):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# 初始化UI界面
self.setupUi(self)
# 绑定pushButton1的clicked事件到槽函数OnPushButton1上
self.pushButton1.clicked.connect(self.OnPushButton1)
# 初始化计数器num
self.num = 0
# 定义槽函数OnPushButton1
def OnPushButton1(self):
# 在textEdit中添加文本
self.textEdit.append("Hello PyQt5")
# 计数器num加1
self.num += 1
# 设置窗口标题
self.setWindowTitle("PushButton1 has been clicked {0} times".format(self.num))
# 返回空
return None
if __name__ == "__main__":
# 创建Qt应用对象
app = QApplication(sys.argv)
# 创建自定义窗口对象
mainWindow = MyWindow()
# 显示窗口
mainWindow.show()
# 进入消息循环
sys.exit(app.exec_())
11-30
4837
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)