1. 代码(假数据)
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from threading import Thread
import time, sys, os
import qdarkstyle
# 导入随机数
import random
import matplotlib
matplotlib.use(Qt5Agg)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
# 切换到当前目录
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# 自定义信号源对象类型,一定要继承自 QObject
class MySignals(QObject):
updateTempSingle = pyqtSignal(QProgressBar, float)
temp_real = []
# 实例化
global_ms = MySignals()
class Temp_Gui(QWidget):
def __init__(self):
super(Temp_Gui, self).__init__()
# 设置图标
self.setWindowTitle("dec资源分配工具")
self.setWindowIcon(QIcon(./images/head_log.jpg))
self.resize(800, 300)
# 设置样式为红色
self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
self.formLayout = QFormLayout()
self.Button_dec_file_sel = QPushButton("dec文件选择")
self.Button_dec_file_sel.setFixedSize(150, 30)
self.Button_dec_file_sel.clicked.connect(self.dec_file_sel)
self.Button_excel_print = QPushButton("excel打印输出")
self.Button_excel_print.setFixedSize(150, 30)
self.Button_excel_print.clicked.connect(self.excel_print)
self.LineEdit_dec_file_path = QLineEdit("")
self.LineEdit_dec_file_path.setReadOnly(True)
self.LineEdit_excel_file_path = QLineEdit("")
self.LineEdit_excel_file_path.setReadOnly(True)
self.comboBox_ch_sum_sel = QComboBox()
self.comboBox_ch_sum_sel.addItems(["256", "512"])
self.comboBox_ch_sum_sel.setCurrentIndex(0)
self.PlainEdit = QPlainTextEdit()
self.PlainEdit.setReadOnly(True)
self.PlainEdit.setPlainText("log print")
self.formLayout.addRow(QLabel(机台通道数目), self.comboBox_ch_sum_sel)
self.formLayout.addRow(self.Button_dec_file_sel, self.LineEdit_dec_file_path)
self.formLayout.addRow(self.Button_excel_print, self.LineEdit_excel_file_path)
self.Widget_temp_graph = QWidget()
self.Widget_temp_graph.setLayout(self.formLayout)
self.VBoxLayout = QVBoxLayout()
self.VBoxLayout.addWidget(self.Widget_temp_graph)
self.VBoxLayout.addWidget(self.PlainEdit)
self.setLayout(self.VBoxLayout)
# 自定义信号的处理函数, 传出去给MySignals,再传回来给控件
global_ms.updateTempSingle.connect(self.updataTemperature)
def updataTemperature(self, object, value):
if (value > 1000):
object.setText(str("Max") + " °C")
else:
object.setText(str(value) + " °C")
def dec_file_sel(self):
# 打开文件选择对话框
file_path = QFileDialog.getOpenFileName(self, "选择文件", "./", "*.dec *.dec.old *.py")
self.LineEdit_dec_file_path.setText(file_path[0])
def excel_print(self):
file_path = QFileDialog.getSaveFileName(self, "选择文件", "./xxx.excel", "*.excel *.py")
self.LineEdit_excel_file_path.setText(file_path[0])
def readTempTask():
def threadFunc():
while True:
time.sleep(0.22)
# 发出信号,通知主线程进行进度处理
# global_ms.updateTempSingle.emit(temp_gui.LineEdit_temp_value1, random.randint(0, 300))
# global_ms.updateTempSingle.emit(temp_gui.LineEdit_temp_value2, random.randint(0, 300))
# global_ms.updateTempSingle.emit(temp_gui.LineEdit_temp_value3, random.randint(0, 300))
# temp_real.append(random.randint(0, 300))
# plt.plot(temp_real)
# temp_gui.canvas.draw()
pass
thread = Thread(target=threadFunc, daemon=True)
thread.start()
if __name__ == __main__:
# pyqt5设置默认字体为"./font/simkai.ttf"
app = QApplication(sys.argv)
# font_path = "./font/simkai.ttf"
# QFontDatabase.addApplicationFont(font_path)
# app.setFont(QFont(font_path))
# 设置字体加粗
app.setStyleSheet("QWidget{font-weight:bold;font-size:18px;}")
temp_gui = Temp_Gui()
temp_gui.show()
readTempTask()
sys.exit(app.exec_())