在QMessageBox中显示python控制台消息 [英] Display python console messages in QMessageBox

查看:635
本文介绍了在QMessageBox中显示python控制台消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LOAD DATA INFILE command将文件导入MySQL数据库.
某些文件可能有错误,导致控制台消息,例如:

I'm importing files to a MySQL DB using the LOAD DATA INFILE command.
Some files may have an error which results in a console message like:

mysql.connector.errors.DatabaseError: 1265 (01000): Data truncated for column 'z' at row x

如何将错误消息放入QMessageBox,以便.exe用户具有指示符,可在何处检查数据集?

How can I put this error message to a QMessageBox so the user of the .exe has an indicator where to check the dataset?

try:
   cursor.execute(query)
except:
   QMessageBox.warning(self, "Failure", ...Console Output...)  

推荐答案

如果SQL库使用标准Python输出,则可以尝试用实现write方法的任何对象覆盖sys.stderrsys.stdout:

If the SQL library is using standard Python output, you could try to overwrite sys.stderr and sys.stdout with any object that implements a write method:

import sys

class TextBoxStderr:

    def __init__(self):
        self.textbox = QTextEdit()

    def write(self, errmsg):
        self.textbox.append(errmsg)  

box_stderr = TextBoxStderr()
sys.stderr = box_stderr

# ... Call Import Operation ...

# If any error was appended to the text box, show it
if box_stderr.textbox.toPlainText():
     box_stderr.textbox.show()

发送到stderr的任何文本都将附加到QTextEdit.确保在操作完成后回滚原始对象:

Any text sent to stderr will be appended to a QTextEdit. Make sure you rollback the original object after the operation is complete:

sys.sterr = sys.__stderr__

这篇关于在QMessageBox中显示python控制台消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆