重定向输出到gui

This commit is contained in:
halliday2023 2024-02-13 12:02:22 +08:00
parent 57e34b35b9
commit 421a530323
1 changed files with 35 additions and 10 deletions

View File

@ -7,8 +7,11 @@ import global_var
from pathlib import Path from pathlib import Path
from PySide6.QtUiTools import loadUiType from PySide6.QtUiTools import loadUiType
from PySide6.QtCore import Slot,Signal,QThread,QMutex,QWaitCondition,QMutexLocker from PySide6.QtCore import Slot,Signal,QThread,QObject,QEventLoop,QTimer
from PySide6.QtWidgets import QApplication
from PySide6.QtGui import QTextCursor
# 声明全局变量
global_var._init() global_var._init()
# 定义公共路径 # 定义公共路径
path = Path(__file__) path = Path(__file__)
@ -16,15 +19,28 @@ ui_file_patch = path.parent.parent / "ui" / "ui_main.ui"
# 从文件中加载UI定义 # 从文件中加载UI定义
formType, baseType = loadUiType(str(ui_file_patch)) formType, baseType = loadUiType(str(ui_file_patch))
# 重定向输出
class EmittingStr(QObject):
textWritten = Signal(str)
def write(self, text):
self.textWritten.emit(str(text))
loop = QEventLoop()
QTimer.singleShot(100, loop.quit)
loop.exec_()
QApplication.processEvents()
# 处理文件的线程
class FileThread(QThread): class FileThread(QThread):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent=parent) super().__init__(parent=parent)
# 覆盖原来run去调用文件造数据脚本
def run(self): def run(self):
fileDataCreating.main() fileDataCreating.main()
# GUI的主程序
class Window(formType, baseType): class Window(formType, baseType):
def __init__(self): def __init__(self):
@ -32,14 +48,16 @@ class Window(formType, baseType):
self.setupUi(self) self.setupUi(self)
self.setup_thread() self.setup_thread()
# 输出设置 # 输出设置
self.Log_Output.document().setMaximumBlockCount(100) self.Log_Output.document().setMaximumBlockCount(100)
self.Log_Output.ensureCursorVisible() self.Log_Output.ensureCursorVisible()
# 将控制台输出重定向到textBrowser中
sys.stdout = EmittingStr()
sys.stdout.textWritten.connect(self.outputWritten)
@Slot() #声明槽函数 @Slot() #声明槽函数
def start(self): def start(self):
self.Log_Output.append('点击了开始按钮') print('点击了开始按钮')
# 输入框参数校验 # 输入框参数校验
num = self.tabWidget.currentIndex() num = self.tabWidget.currentIndex()
if num == 0: if num == 0:
@ -57,7 +75,7 @@ class Window(formType, baseType):
@Slot() @Slot()
def stop(self): def stop(self):
self.Log_Output.append('点击了停止按钮') print('点击了停止按钮')
num = self.tabWidget.currentIndex() num = self.tabWidget.currentIndex()
if num == 0: if num == 0:
self.stop_run_file() self.stop_run_file()
@ -68,10 +86,10 @@ class Window(formType, baseType):
@Slot() @Slot()
def conntest(self): def conntest(self):
self.Log_Output.append('点击了测试连接按钮') print('点击了测试连接按钮')
self.check_db_input() self.check_db_input()
# TODO(MH):校验连接信息 # TODO(MH):校验连接信息
self.Log_Output.append('数据库连接成功') print('数据库连接成功')
def setup_thread(self): def setup_thread(self):
@ -89,17 +107,17 @@ class Window(formType, baseType):
def check_file_input(self): def check_file_input(self):
self.Log_Output.append('文件参数校验通过') print('文件参数校验通过')
def check_db_input(self): def check_db_input(self):
self.Log_Output.append('数据库参数校验通过,开始连接数据库测试。。。') print('数据库参数校验通过,开始连接数据库测试。。。')
def start_run_file(self): def start_run_file(self):
# 调用文件处理脚本 # 调用文件处理脚本
self.Log_Output.append('开始生成文件。。。') print('开始生成文件。。。')
@ -119,3 +137,10 @@ class Window(formType, baseType):
def stop_run_db(self): def stop_run_db(self):
pass pass
# 输出到GUI
def outputWritten(self, text):
cursor = self.Log_Output.textCursor()
cursor.movePosition(QTextCursor.End)
cursor.insertText(text)
self.Log_Output.setTextCursor(cursor)
self.Log_Output.ensureCursorVisible()