From 421a53032318b696b408d41eb8f13bbc4a64ec05 Mon Sep 17 00:00:00 2001 From: halliday2023 Date: Tue, 13 Feb 2024 12:02:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=AE=9A=E5=90=91=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E5=88=B0gui?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/GUIWindows.py | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/bin/GUIWindows.py b/bin/GUIWindows.py index 8e6fd27..5f25d00 100644 --- a/bin/GUIWindows.py +++ b/bin/GUIWindows.py @@ -7,8 +7,11 @@ import global_var from pathlib import Path 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() # 定义公共路径 path = Path(__file__) @@ -16,15 +19,28 @@ ui_file_patch = path.parent.parent / "ui" / "ui_main.ui" # 从文件中加载UI定义 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): def __init__(self, parent=None): super().__init__(parent=parent) - + # 覆盖原来run去调用文件造数据脚本 def run(self): fileDataCreating.main() +# GUI的主程序 class Window(formType, baseType): def __init__(self): @@ -32,14 +48,16 @@ class Window(formType, baseType): self.setupUi(self) self.setup_thread() - # 输出设置 self.Log_Output.document().setMaximumBlockCount(100) self.Log_Output.ensureCursorVisible() + # 将控制台输出重定向到textBrowser中 + sys.stdout = EmittingStr() + sys.stdout.textWritten.connect(self.outputWritten) @Slot() #声明槽函数 def start(self): - self.Log_Output.append('点击了开始按钮') + print('点击了开始按钮') # 输入框参数校验 num = self.tabWidget.currentIndex() if num == 0: @@ -57,7 +75,7 @@ class Window(formType, baseType): @Slot() def stop(self): - self.Log_Output.append('点击了停止按钮') + print('点击了停止按钮') num = self.tabWidget.currentIndex() if num == 0: self.stop_run_file() @@ -68,10 +86,10 @@ class Window(formType, baseType): @Slot() def conntest(self): - self.Log_Output.append('点击了测试连接按钮') + print('点击了测试连接按钮') self.check_db_input() # TODO(MH):校验连接信息 - self.Log_Output.append('数据库连接成功') + print('数据库连接成功') def setup_thread(self): @@ -89,17 +107,17 @@ class Window(formType, baseType): def check_file_input(self): - self.Log_Output.append('文件参数校验通过') + print('文件参数校验通过') def check_db_input(self): - self.Log_Output.append('数据库参数校验通过,开始连接数据库测试。。。') + print('数据库参数校验通过,开始连接数据库测试。。。') def start_run_file(self): # 调用文件处理脚本 - self.Log_Output.append('开始生成文件。。。') + print('开始生成文件。。。') @@ -119,3 +137,10 @@ class Window(formType, baseType): def stop_run_db(self): 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()