python学习 之 pyqt5前后端分离试验(dec资源分配)

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_())


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值