dataCreatingGUI/bin/GUIWindows.py

148 lines
3.6 KiB
Python

# -*- coding:utf-8 -*-
import os
import sys
import fileDataCreating
import global_var
from pathlib import Path
from PySide6.QtUiTools import loadUiType
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__)
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):
super(Window, self).__init__()
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):
print('点击了开始按钮')
# 输入框参数校验
num = self.tabWidget.currentIndex()
if num == 0:
self.check_file_input()
else:
self.conntest()
# 开始运行
if num == 0:
self.start_thread()
else:
self.start_run_db()
# 按钮置灰
self.startButton.setEnabled(False)
@Slot()
def stop(self):
print('点击了停止按钮')
num = self.tabWidget.currentIndex()
if num == 0:
self.stop_run_file()
else:
self.stop_run_db()
# 按钮恢复
# self.startButton.isEnabled() 获取按钮的状态
self.startButton.setEnabled(True)
@Slot()
def conntest(self):
print('点击了测试连接按钮')
self.check_db_input()
# TODO(MH):校验连接信息
print('数据库连接成功')
def setup_thread(self):
self.thread = FileThread()
global_var.set_value('start_flag',True)
self.thread_running = global_var.get_value('start_flag')
def start_thread(self):
if self.thread_running:
self.thread.start()
else:
self.setup_thread()
self.thread.start()
def check_file_input(self):
print('文件参数校验通过')
def check_db_input(self):
print('数据库参数校验通过,开始连接数据库测试。。。')
def start_run_file(self):
# 调用文件处理脚本
print('开始生成文件。。。')
def start_run_db(self):
pass
def stop_run_file(self):
# 终止线程的事件循环
global_var.set_value('start_flag',False)
self.thread.quit()
# 标记线程停止
self.thread_running = global_var.get_value('start_flag')
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()