Python多线程

时间:2015-04-03 16:37:21

标签: python matplotlib python-multithreading

为了显示,让我们说,2个绘图数字(使用matplotlib)和1个消息框(使用wxpython)同时(无顺序)一个好主意是使用线程:

from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
from reportlab.platypus import Flowable
from mpl_toolkits.axes_grid1 import host_subplot
from threading import Thread
import numpy as np
import matplotlib.pyplot as plt
import wx

def plFig1():

    fig1 = plt.figure(1)
    fig1.canvas.set_window_title('Quality control measure. Translation. ')
    plt.title('Parameters of the linear head motions')
    plt.xlabel("Dynamic scans")
    plt.ylabel("Linear motion -X, Y, Z- (mm)")

    x, y, z, = plt.plot(((1,2,3), (2,4,6), (3,6,9)))
    plt.legend((x, y, z), ("X", "Y", "Z"), bbox_to_anchor=(0, 1), loc=2, borderaxespad=0.)

    plt.xlim(0, 4)
    plt.ylim(0*1.1, 9*1.1)

    plt.savefig('plFig1.png')

    plt.get_current_fig_manager().window.wm_geometry("1300x300+0+0")
    plt.show()

def plFig2():
    fig2 = plt.figure(2)
    fig2.canvas.set_window_title('Quality control measure. Rotation. ')

    plt.title('Parameters of the rotational head motions')
    plt.xlabel("Dynamic scans")
    plt.ylabel(u'Rotational motion -Roll, Pitch, Yaw- (°)')

    roll, pitch, yaw, = plt.plot(((-1,-2,-3), (-2,-4,-6), (-3,-6,-9)))      
    plt.legend((roll, pitch, yaw), ("Roll", "Pitch", "Yaw"), bbox_to_anchor=(0, 1), loc=2, borderaxespad=0.)

    plt.xlim(0, 4)
    plt.ylim(-9*1.1, 0*1.1)

    plt.savefig('plFig1.png')
    plt.get_current_fig_manager().window.wm_geometry("1300x300+100+400")
    plt.show()

p1 = Thread(target=plFig1)
p2 = Thread(target=plFig2)

p1.start()
p2.start()

qualPara = wx.App(False)
dlg = wx.MessageDialog(None, 'Bla bla bla', 'Head movements - Quality parameters', 
    wx.OK | wx.ICON_INFORMATION |wx.STAY_ON_TOP, pos=(-1,-1))
dlg.ShowModal()
dlg.Destroy()

qualPara.MainLoop()

##########################################
p1.join()
p2.join()

但我只观察第一个图和消息框以及此消息错误:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib64/python2.7/threading.py", line 811, in __bootstrap_inner
    self.run()
  File "/usr/lib64/python2.7/threading.py", line 764, in run
    self.__target(*self.__args, **self.__kwargs)
  File "essai30.py", line 43, in plFig2
    fig2 = plt.figure(2)
  File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 1745, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TclError: out of stack space (infinite loop?)

此外,可以使用x-close角关闭图形,然后使用OK按钮关闭消息框,但如果我先单击消息框的OK按钮,则框不会关闭(必须先关闭)关闭数字!!!)。

0 个答案:

没有答案
相关问题