dataCreatingGUI/bin/GUIWindows.py

168 lines
4.4 KiB
Python
Raw Normal View History

2024-02-11 11:38:52 +00:00
# -*- coding:utf-8 -*-
import sys
2024-02-13 09:05:46 +00:00
import logging
2024-02-13 09:25:03 +00:00
from file import fileDataCreating
from common import global_var
2024-02-12 09:24:19 +00:00
2024-02-11 11:38:52 +00:00
from pathlib import Path
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import Signal,QThread,QObject,QEventLoop,QTimer
2024-02-13 04:02:22 +00:00
from PySide6.QtWidgets import QApplication
from PySide6.QtGui import QTextCursor
2024-02-13 04:02:22 +00:00
# 声明全局变量
global_var._init()
2024-02-11 11:38:52 +00:00
2024-02-13 09:05:46 +00:00
# 控制台重定向到GUI
2024-02-13 04:02:22 +00:00
class EmittingStr(QObject):
2024-02-13 07:47:53 +00:00
# 定义信号
textWritten = Signal(str)
# 将控制台的内容输出到QTextBrowser
2024-02-13 04:02:22 +00:00
def write(self, text):
2024-02-13 07:47:53 +00:00
# 调用 emit方法 发信号时,传入参数 必须是这里指定的 参数类型
2024-02-13 04:02:22 +00:00
self.textWritten.emit(str(text))
loop = QEventLoop()
QTimer.singleShot(100, loop.quit)
loop.exec_()
QApplication.processEvents()
2024-02-13 09:05:46 +00:00
# logging重定向到print
class PrintHandler(logging.Handler):
def emit(self, record):
pass
2024-02-13 04:02:22 +00:00
# 处理文件的线程
class FileThread(QThread):
2024-02-12 09:24:19 +00:00
def __init__(self, parent=None):
super().__init__(parent=parent)
2024-02-13 04:02:22 +00:00
# 覆盖原来run去调用文件造数据脚本
2024-02-12 09:24:19 +00:00
def run(self):
fileDataCreating.main()
2024-02-11 11:38:52 +00:00
2024-02-13 04:02:22 +00:00
# GUI的主程序
class Window:
2024-02-11 11:38:52 +00:00
def __init__(self):
#载入ui
path = Path(__file__)
ui_file_patch = path.parent.parent / "ui" / "ui_main.ui"
self.ui = QUiLoader().load(str(ui_file_patch))
#指定按钮和槽函数
self.ui.startButton.clicked.connect(self.start)
self.ui.stopButton.clicked.connect(self.stop)
self.ui.conn_Button.clicked.connect(self.conntest)
2024-02-12 09:24:19 +00:00
self.setup_thread()
2024-02-13 07:47:53 +00:00
# QTextBrowser输出设置限制条数为100
self.ui.Log_Output.document().setMaximumBlockCount(100)
self.ui.Log_Output.ensureCursorVisible()
2024-02-13 07:47:53 +00:00
# 将控制台输出重定向到QTextBrowser
2024-02-13 04:02:22 +00:00
sys.stdout = EmittingStr()
sys.stdout.textWritten.connect(self.outputWritten)
2024-02-13 09:05:46 +00:00
# 创建并配置logger
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
# 创建并配置处理程序
handler = PrintHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
self.logger.addHandler(handler)
2024-02-13 07:47:53 +00:00
# 输出到GUI信号处理函数
def outputWritten(self, text):
cursor = self.ui.Log_Output.textCursor()
2024-02-13 07:47:53 +00:00
cursor.movePosition(QTextCursor.End)
cursor.insertText(text)
self.ui.Log_Output.setTextCursor(cursor)
self.ui.Log_Output.ensureCursorVisible()
2024-02-13 07:47:53 +00:00
def start(self):
2024-02-13 09:05:46 +00:00
self.logger.info("点击了开始按钮")
2024-02-11 14:06:23 +00:00
# 输入框参数校验
num = self.ui.tabWidget.currentIndex()
2024-02-11 14:06:23 +00:00
if num == 0:
self.check_file_input()
else:
self.conntest()
# 开始运行
if num == 0:
2024-02-12 09:24:19 +00:00
self.start_thread()
2024-02-11 14:06:23 +00:00
else:
self.start_run_db()
2024-02-12 14:28:36 +00:00
# 按钮置灰
self.ui.startButton.setEnabled(False)
2024-02-12 14:28:36 +00:00
def stop(self):
2024-02-13 09:05:46 +00:00
self.logger.info("点击了停止按钮")
num = self.ui.tabWidget.currentIndex()
2024-02-11 14:06:23 +00:00
if num == 0:
self.stop_run_file()
else:
self.stop_run_db()
2024-02-12 14:28:36 +00:00
# 按钮恢复
2024-02-13 04:06:59 +00:00
# self.startButton.isEnabled() 获取按钮的状态
self.ui.startButton.setEnabled(True)
def conntest(self):
2024-02-13 09:05:46 +00:00
self.logger.info("点击了测试连接按钮")
2024-02-11 14:06:23 +00:00
self.check_db_input()
# TODO(MH):校验连接信息
2024-02-13 09:05:46 +00:00
self.logger.info("数据库连接成功")
2024-02-11 14:06:23 +00:00
2024-02-12 09:24:19 +00:00
def setup_thread(self):
self.thread = FileThread()
global_var.set_value('start_flag',True)
self.thread_running = global_var.get_value('start_flag')
2024-02-12 09:24:19 +00:00
def start_thread(self):
if self.thread_running:
self.thread.start()
else:
self.setup_thread()
self.thread.start()
2024-02-12 09:24:19 +00:00
2024-02-11 14:06:23 +00:00
def check_file_input(self):
2024-02-13 09:05:46 +00:00
self.logger.info("文件参数校验通过")
2024-02-11 14:06:23 +00:00
def check_db_input(self):
2024-02-13 09:05:46 +00:00
self.logger.info("数据库参数校验通过,开始连接数据库测试。。。")
2024-02-11 14:06:23 +00:00
def start_run_file(self):
2024-02-12 09:24:19 +00:00
# 调用文件处理脚本
2024-02-13 09:05:46 +00:00
self.logger.info("开始生成文件。。。")
2024-02-12 09:24:19 +00:00
2024-02-11 14:06:23 +00:00
def start_run_db(self):
pass
def stop_run_file(self):
2024-02-12 14:28:36 +00:00
# 终止线程的事件循环
global_var.set_value('start_flag',False)
2024-02-12 14:28:36 +00:00
self.thread.quit()
# 标记线程停止
self.thread_running = global_var.get_value('start_flag')
2024-02-12 09:24:19 +00:00
2024-02-11 14:06:23 +00:00
2024-02-11 11:38:52 +00:00
2024-02-11 14:06:23 +00:00
def stop_run_db(self):
pass
2024-02-11 11:38:52 +00:00