PyQt5做一个BugReport

该博客内容涉及使用PyQt5库创建一个错误报告窗口,读取并显示程序中的错误信息。程序会从'error.txt'文件中读取错误,并在窗口中展示,同时提供一个图标和错误描述。如果在运行时遇到新的异常,会追加到错误文件中。
摘要由CSDN通过智能技术生成

我们来理一下思路,首先导入几个PyQt5必要的库。

然后读取我们程序记录下来的错误,路径可以自己改。

然后把错误告诉给大家。

那个lb2是一个图标文件,路径可以自己改。

最后,调用一下窗口就好了。

try:
    pass
except:
    pass
else:
    try:
        #INFO
        INFO="""
BugReport.pyw
        """
        HTML='http://haote.home.cn/'
        #Library
        import os
        import datetime
        import sys
        import subprocess
        import pygame
        import webbrowser
        from PyQt5.QtGui import QIcon
        from PyQt5.Qt import *
        from PyQt5.QtCore import *
        from PyQt5.QtCore import Qt
        from PyQt5.QtWidgets import *
        #Variables
        NULL=''
        #ReadErrors
        with open('error.txt','r',encoding='utf-8') as readError:
            error=readError.read()
        #MainWindow
        class Main(QMainWindow):
            def __init__(self):
                super().__init__()
                self.ui()
                self.flag=True
            def ui(self):
                #InitWindow
                self.resize(400,200)
                self.setWindowTitle('写作助手')
                self.setFixedSize(self.width(),self.height())
                self.setWindowIcon(QIcon('res\App_Icon\Application_Icon.ico'))
                #UI
                label=QLabel(self)
                label.setText('程序中发生了一个错误(异常)。如下:')
                label.setStyleSheet('font-size:16px;')
                label.resize(280,20)
                label.move(70,30)
                label.show()
                pic=QPixmap('res/App_Icon/error.png')
                lb2 = QLabel(self)
                lb2.setPixmap(pic)
                lb2.resize(48,48)
                lb2.setScaledContents(True)
                lb2.move(25,23)
                lb2.show()
                textedit=QTextEdit(self)
                textedit.setPlainText(error)
                textedit.setReadOnly(True)
                textedit.resize(230,100)
                textedit.move(85,70)
                textedit.setLineWrapMode(False)
                textedit.show()
    except Exception as exception:
        with open('error.txt','a',encoding='utf-8') as exception_write:
            exception_write.write('{0} {1}:{2}{3}'.format(os.getlogin(),datetime.datetime.now(),str(exception),'\n'))
    #Applications
    if __name__ == '__main__':
        applications = QApplication(sys.argv)
        root=Main()
        root.show()
        sys.exit(applications.exec_())

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值