PyQt5中使用matplotlib作图阻塞主程序

解决PyQt5中使用matplotlib作图阻塞主程序问题

问题:我程序中有一个按钮执行之后会分析一个数据文件,并将里面的数据做成几十张图保存本地,这样,只要我点击这个按钮,后台就开始执行matplotlib的进程,只要这个进程没结束我的主程序就一直被堵塞,这样程序没有一点流畅性。后来通过在主线程中打开一个子线程来执行作图的部分操作,还可以通过信号与槽函数,向主进程反馈子线程的进度。

  • 子线程 AnaThread.py
import os
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from PyQt5.QtCore import QThread, pyqtSignal


class AnaThread(QThread):
    signal_str = pyqtSignal(str)
    '''创建一个信号'''
    def __init__(self):
        super(AnaThread, self).__init__()
        self.filepath = ''
        self.sql = ''
        self.stack = ''

    def get_info(self, sql, filepath, stack):
        '''从主程序获得参数'''
        self.filepath = filepath
        self.sql = sql
        self.stack = stack
    
    def matplot(self):
        '''作图主体程序'''

    def run(self):
        '''内置函数,线程启动时自动执行'''
        self.matplot()
        self.signal_str.emit("提交信号:successed!")

  • 主程序 Main.py
import sys
import time
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
# from PyQt5.QtWidgets import *
# from PyQt5.QtGui import *
from PyQt5.QtCore import *
from myui2 import Ui_MainWindow
from AnaThread import AnaThread


class Example(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Example, self).__init__(parent)
        self.setupUi(self)
        '''子线程实例化'''
        self.thread = AnaThread()
        self.pushButton.clicked.connect(self.buttonClicked)
        '''信号接收'''
        self.thread.signal_str.connect(self.signal_receive)
  
    def buttonClicked(self):
        stack = self.comboBox.currentText()
        filepath = fr'{self.plainTextEdit.toPlainText()}'
        sql = ''
        '''向子线程传递参数'''
        self.thread.get_info(sql, filepath, stack)
        '''运行子线程'''
        self.thread.start()

    def signal_receive(self, signal_str):
        '''槽函数'''
        self.plainTextEdit_2.appendPlainText(signal_str)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

这样子线程不会影响主进程的执行,如果子线程运行时间很长,还可以在子线程运行的任意阶段emit signal 给主进程,让我能清楚地知道子线程的进度。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于使用PyQt5Matplotlib绘制条形图,你可以按照以下步骤: 1. 安装必要的库,如pyqt5matplotlib:可以使用pip install pyqt5和pip install matplotlib来安装。 2. 在PyQt5创建应用程序窗口,例如: ``` import sys from PyQt5.QtWidgets import QApplication, QMainWindow app = QApplication(sys.argv) window = QMainWindow() window.show() sys.exit(app.exec_()) ``` 3. 在窗口添加一个matplotlib画布,例如: ``` import matplotlib.pyplot as plt from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas class MyMainWindow(QMainWindow): def __init__(self): super().__init__() ... # Create the matplotlib figure and canvas widgets self.fig = plt.Figure() self.canvas = FigureCanvas(self.fig) self.setCentralWidget(self.canvas) ... ``` 4. 绘制条形图: ``` class MyMainWindow(QMainWindow): def __init__(self): super().__init__() ... # Define data for the bar chart labels = ['Apples', 'Oranges', 'Bananas'] values = [5, 3, 4] # Create the matplotlib figure and canvas widgets self.fig = plt.Figure() self.canvas = FigureCanvas(self.fig) self.setCentralWidget(self.canvas) # Add the bar chart to the Figure ax = self.fig.add_subplot(111) ax.bar(labels, values) ... ``` 5. 最后,记得在窗口调整和显示matplotlib画布: ``` class MyMainWindow(QMainWindow): def __init__(self): super().__init__() ... # Create the matplotlib figure and canvas widgets self.fig = plt.Figure() self.canvas = FigureCanvas(self.fig) self.setCentralWidget(self.canvas) # Add the bar chart to the Figure ax = self.fig.add_subplot(111) ax.bar(labels, values) # Adjust the matplotlib figure layout self.fig.tight_layout() # Show the window self.show() ``` 希望这能帮助你完成任务!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值