153 lines
5.2 KiB
Python
153 lines
5.2 KiB
Python
# #!/opt/homebrew/bin/python3
|
||
# # -*- coding:utf-8 -*-
|
||
|
||
import sys
|
||
import traceback
|
||
import logging
|
||
import os
|
||
from PyQt6 import *
|
||
from PyQt6.QtCore import *
|
||
from PyQt6.QtGui import *
|
||
from PyQt6.QtWidgets import *
|
||
from PyQt6.QtWebEngineWidgets import QWebEngineView
|
||
from mainWindow import MainWindow
|
||
from dog import dog
|
||
from dog import suDog
|
||
import xlwings
|
||
import psutil
|
||
import matplotlib
|
||
import asyncio
|
||
from bleak import BleakScanner
|
||
import nidaqmx
|
||
import pyvisa
|
||
import time
|
||
from PyQt6.QtQml import qmlRegisterType
|
||
from CustomPlotItem import CustomPlotItem
|
||
from shutil import copyfile
|
||
import datetime
|
||
import threading
|
||
|
||
|
||
# # 自定义消息处理器
|
||
# def myMsgHandler(msg_type, context, msg):
|
||
# try:
|
||
# # 线程安全的锁
|
||
# lock = threading.Lock()
|
||
# lock.acquire()
|
||
|
||
# # 获取当前时间
|
||
# current_time = datetime.datetime.now().strftime("%H")
|
||
|
||
# # 根据消息类型设置日志级别
|
||
# if msg_type == QtMsgType.QtDebugMsg:
|
||
# log_level = "Debug"
|
||
# elif msg_type == QtMsgType.QtWarningMsg:
|
||
# log_level = "Warning"
|
||
# elif msg_type == QtMsgType.QtCriticalMsg:
|
||
# log_level = "Critical"
|
||
# elif msg_type == QtMsgType.QtFatalMsg:
|
||
# log_level = "Fatal"
|
||
# else:
|
||
# log_level = "Unknown"
|
||
|
||
# # 构建日志消息
|
||
# log_message = f"{current_time} [{log_level}]: {msg}"
|
||
|
||
# # 写入到日志文件
|
||
# log_filename = f"{LOGDIR}{current_time}_log.txt"
|
||
# with open(log_filename, "a", encoding="utf-8") as file:
|
||
# file.write(log_message + "\n")
|
||
|
||
# # 释放锁
|
||
# lock.release()
|
||
# except:
|
||
# pass
|
||
|
||
# # 安装自定义的消息处理器
|
||
# qInstallMessageHandler(myMsgHandler)
|
||
|
||
# 自定义的异常钩子
|
||
def my_exception_hook(exctype, value, traceback_obj):
|
||
LOGDIR = os.path.join(os.path.dirname(__file__), 'logFile', datetime.datetime.now().strftime("%Y-%m-%d"))
|
||
if not os.path.exists(LOGDIR):
|
||
os.makedirs(LOGDIR)
|
||
with open(os.path.join(LOGDIR,"error_log.txt"), "a") as f:
|
||
# 写入异常信息
|
||
f.write(f"Unhandled exception occurred at {datetime.datetime.now()}\n")
|
||
if exctype:
|
||
f.write(f"Exception type: {exctype.__name__}\n")
|
||
else:
|
||
f.write("Exception type: None\n")
|
||
|
||
if value:
|
||
f.write(f"Exception value: {value}\n")
|
||
else:
|
||
f.write("Exception value: None\n")
|
||
|
||
# 写入堆栈跟踪信息
|
||
for line in traceback.format_exception(exctype, value, traceback_obj):
|
||
f.write(line)
|
||
|
||
# 如果需要,可以添加额外的文件名和行号信息
|
||
if traceback_obj:
|
||
f.write(f"Exception occurred in file '{traceback_obj.tb_frame.f_code.co_filename}' at line {traceback_obj.tb_lineno}\n")
|
||
|
||
# 设置自定义的异常钩子
|
||
sys.excepthook = my_exception_hook
|
||
|
||
class ThemeGuard(QObject):
|
||
def eventFilter(self, obj, event):
|
||
if event.type() == QEvent.Type.PaletteChange:
|
||
enforce_theme() # 拦截主题变更事件
|
||
return False
|
||
|
||
def enforce_theme():
|
||
# 1. 强制设定调色板
|
||
palette = app.palette()
|
||
palette.setColor(QPalette.ColorRole.Window, QColor(240, 240, 240)) # 主背景色
|
||
palette.setColor(QPalette.ColorRole.WindowText, QColor(30, 30, 30)) # 文本色
|
||
palette.setColor(QPalette.ColorRole.Base, QColor(255, 255, 255)) # 输入框背景
|
||
palette.setColor(QPalette.ColorRole.Text, QColor(0, 0, 0)) # 输入文本
|
||
app.setPalette(palette)
|
||
|
||
if __name__ == "__main__":
|
||
app = QApplication(sys.argv)
|
||
enforce_theme()
|
||
# app.installEventFilter(ThemeGuard())
|
||
# 不需要手动设置高DPI,自动启用缩放
|
||
QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
|
||
screen = app.primaryScreen()
|
||
dogErrorIndex = 0
|
||
for i in range(3):
|
||
state = dog.isDogAlive()
|
||
if state != 1:
|
||
if suDog.isDogAlive() != 1:
|
||
dogErrorIndex += 1
|
||
print("dogErrorIndex:", dogErrorIndex)
|
||
else:
|
||
dogErrorIndex = 0
|
||
else:
|
||
dogErrorIndex = 0
|
||
print("dogErrorIndex:", dogErrorIndex)
|
||
|
||
if dogErrorIndex >= 3:
|
||
reply = QMessageBox.critical(None, "Error", "请插入加密狗", QMessageBox.StandardButton.Ok)
|
||
if reply == QMessageBox.StandardButton.Ok:
|
||
sys.exit(1)
|
||
if not os.path.exists("data.db"):
|
||
try:
|
||
copyfile("default_data.db", "data.db")
|
||
except IOError as e:
|
||
print("Unable to copy file. %s" % e)
|
||
except:
|
||
print("Unexpected error:", sys.exc_info())
|
||
nidaqmx.annotations.getOptionalRelease()
|
||
app.setStyle("Fusion")
|
||
# app.style().metaObject().className = lambda: "FusionStyle" # 欺骗系统主题检测
|
||
# app.paletteChanged = lambda *args: app.setPalette(app.palette()) # 冻结调色板
|
||
app.setWindowIcon(QIcon(os.path.join(os.path.dirname(__file__), 'logo','logo.ico')))
|
||
qmlRegisterType(CustomPlotItem, "CustomPlot", 1, 0, "CustomPlot")
|
||
widget = MainWindow()
|
||
# widget.show()
|
||
sys.exit(app.exec())
|