Display python console messages in QMessageBox

你。 提交于 2019-12-12 02:00:05

问题


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

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...)  

回答1:


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()

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__


来源:https://stackoverflow.com/questions/24017143/display-python-console-messages-in-qmessagebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!