zoukankan      html  css  js  c++  java
  • pyqt如何更改任务栏关闭按钮的触发事件

    原文:https://blog.csdn.net/u010139869/article/details/79449315

    随笔稍作修改,把继承方法QDialog改为了QMainWindow

    关键部分:重写QtWidgets.QMainWindow的closeEvent方法

    # 重写closeEvent方法,实现dialog窗体关闭时执行一些代码
    from PyQt5 import QtCore, QtGui, QtWidgets
    import sys
    
    class Dialog(QtWidgets.QMainWindow):
    
        def get_thread(self, main_thread):
            self.main_thread = main_thread
    
        def closeEvent(self, event):
            """
            重写closeEvent方法,实现dialog窗体关闭时执行一些代码
            :param event: close()触发的事件
            :return: None
            """
            reply = QtWidgets.QMessageBox.question(self,
                                                   '本程序',
                                                   "是否要退出程序?",
                                                   QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
                                                   QtWidgets.QMessageBox.No)
            if reply == QtWidgets.QMessageBox.Yes:
                event.accept()
                sys.exit(0)
            else:
                event.ignore()

    完整代码

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'dialog.ui'
    #
    # Created by: PyQt5 UI code generator 5.10.1
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    import sys
    
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(640, 480)
            self.pushButton = QtWidgets.QPushButton(Dialog)
            self.pushButton.setGeometry(QtCore.QRect(370, 80, 113, 32))
            self.pushButton.setObjectName("pushButton")
            self.lineEdit = QtWidgets.QLineEdit(Dialog)
            self.lineEdit.setGeometry(QtCore.QRect(140, 80, 113, 21))
            self.lineEdit.setObjectName("lineEdit")
            self.textBrowser = QtWidgets.QTextBrowser(Dialog)
            self.textBrowser.setGeometry(QtCore.QRect(180, 200, 256, 192))
            self.textBrowser.setObjectName("textBrowser")
            self.label = QtWidgets.QLabel(Dialog)
            self.label.setGeometry(QtCore.QRect(30, 80, 111, 16))
            self.label.setObjectName("label")
            self.label_2 = QtWidgets.QLabel(Dialog)
            self.label_2.setGeometry(QtCore.QRect(30, 220, 141, 16))
            self.label_2.setObjectName("label_2")
    
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        def retranslateUi(self, Dialog):
            _translate = QtCore.QCoreApplication.translate
            Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
            self.pushButton.setText(_translate("Dialog", "PushButton"))
            self.label.setText(_translate("Dialog", "这是Line Edit ->"))
            self.label_2.setText(_translate("Dialog", "这是Text Browser ->"))
    
    
    class Dialog(QtWidgets.QMainWindow):
        """对QDialog类重写,实现一些功能"""
    
        def closeEvent(self, event):
            """
            重写closeEvent方法,实现dialog窗体关闭时执行一些代码
            :param event: close()触发的事件
            :return: None
            """
            reply = QtWidgets.QMessageBox.question(self,
                                                   '本程序',
                                                   "是否要退出程序?",
                                                   QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
                                                   QtWidgets.QMessageBox.No)
            if reply == QtWidgets.QMessageBox.Yes:
                event.accept()
            else:
                event.ignore()
    
    
    def main():
        """
        主函数,用于运行程序
        :return: None
        """
        app = QtWidgets.QApplication(sys.argv)
        dialog = Dialog()  # 注意修改为了自己重写的Dialog类
        ui = Ui_Dialog()
        ui.setupUi(dialog)
        dialog.show()  # 显示了自己重写的Dialog类
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    随便用
  • 相关阅读:
    MTLLoader 报 Handlers.get() has been removed. Use LoadingManager.getHandler() instead 错误处理
    uniCloud云开发已实现五端上线【言语录书】
    v-model 双向绑定 vue3.x
    javascript 集合操作
    链表操作
    vue3 父子组件双向数据绑定
    二叉树排序
    自制Chrome扩展插件:用于重定向js
    解析Markdown文件生成React组件文档
    微前端框架single-spa初探
  • 原文地址:https://www.cnblogs.com/pqhuang/p/11409312.html
Copyright © 2011-2022 走看看