python两个线程一边显示图形,一边计算【实测成功】

仅作为记录,大佬请跳过。

直接上代码

import threading
import time
import numpy as np
from matplotlib.pyplot import *
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui

# def print_111():
win = pg.GraphicsLayoutWidget(show=True)
win.setWindowTitle('pyqtgraph example: Scrolling Plots')

p2 = win.addPlot()
data1 = np.random.normal(size=300)

curve2 = p2.plot(data1)
ptr1 = 0

def update1():
    global data1, ptr1
    data1[:-1] = data1[1:]  # shift data in the array one sample left

    data1[-1] = np.random.normal()

    ptr1 += 1
    curve2.setData(data1)
    curve2.setPos(ptr1, 0)

timer = pg.QtCore.QTimer()
timer.timeout.connect(update1)
timer.start(50)
    # import sys
    # if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
    #     QtGui.QApplication.instance().exec_()

# def print_111():
#     import sys
#
#     if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
#         QtGui.QApplication.instance().exec_()

def print_222():
    while 1:
        print('222')
        time.sleep(1)


threads = []

# t1 = threading.Thread(target=print_111)
# threads.append(t1)
t2 = threading.Thread(target=print_222)
threads.append(t2)

if __name__=='__main__':
    import sys


    for t in threads:
        t.start()
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()
    for t in threads:
        t.join()

print ("退出线程")

print('sec74')

展示:

在这里插入图片描述

参考

传送门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值