Python 在命令行界面(CLI)后台使用线程绘制不同类的数据
我试图在后台绘制一个可变的电池电压,同时命令行代码在程序执行时保持运行。 打印从用户按“打印”开始,到按“退出”结束Python 在命令行界面(CLI)后台使用线程绘制不同类的数据,python,multithreading,command-line-interface,Python,Multithreading,Command Line Interface,我试图在后台绘制一个可变的电池电压,同时命令行代码在程序执行时保持运行。 打印从用户按“打印”开始,到按“退出”结束 from threading import Thread import cmd import matplotlib.pyplot as plt import sys from drawnow import * plt.ion() count=0 class myThreadPlot (Thread): def __init__(self): Thr
from threading import Thread
import cmd
import matplotlib.pyplot as plt
import sys
from drawnow import *
plt.ion()
count=0
class myThreadPlot (Thread):
def __init__(self):
Thread.__init__(self)
self.state = test()
self.battVoltagePlot= []
self.flag = True
def run(self):
global count
while self.flag:
self.battVoltagePlot.append(self.state.batteryVoltage)
drawnow(self.plot_var,stop_on_close=True)
count+=1
if count>50: self.battVoltagePlot.pop(0) # to plot 50 values at
a time
plt.pause(1e-4)
plt.close()
def plot_var(self):
x =min(self.battVoltagePlot)
y = max(self.battVoltagePlot)
if x<0: plt.ylim(2*x,2*y)
else: plt.ylim(x/2,2*y)
plt.grid(True)
plt.ylabel('V')
plt.plot(self.battVoltagePlot, 'ro-', label='V')
class test(object):
def __init__(self):
self.batteryVoltage =30
class CLI(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = ">> "
self.intro = ("Welcome\n Press help to see the options.\n press
plot to plot battery voltage and press exit to exit ")
def do_plot(self,arg):
"calling myThreadPLot to plot a variable 'batteryVoltage' from
class 'test' in the background"
self.my = myThreadPlot()
self.my.start()
def do_exit(self, arg):
'''Exit this application'''
print("Bye")
self.my.flag = False
return True
if __name__ == '__main__':
CLI().cmdloop()
从线程导入线程
导入命令
将matplotlib.pyplot作为plt导入
导入系统
从现在开始导入*
plt.ion()
计数=0
类myThreadPlot(线程):
定义初始化(自):
线程。\uuuu初始化\uuuuu(自)
self.state=test()
self.battVoltagePlot=[]
self.flag=True
def运行(自):
全局计数
而self.flag:
self.battVoltagePlot.append(self.state.batteryVoltage)
drawnow(self.plot\u var,stop\u on\u close=True)
计数+=1
如果计数>50:self.battVoltagePlot.pop(0)#在
一段时间
plt.暂停(1e-4)
plt.close()
def plot_var(自):
x=最小值(自电池电压图)
y=最大值(自电池电压图)
如果atexit中存在xError.\u run\u exitfuncs:RuntimeError:sys.exitfunc:Traceback(最近一次调用):文件“/usr/lib/python2.7/atexit.py”,destroy\u allmanager.destroy()中的第24行,\u run\u exitfuncs funct(*targets,**kargs)文件“/home/arauf/.local/lib/python2.7/site packages/matplotlib/\u pylab\u helpers.pylab\u helpers.py”,第78行文件“/home/arauf/.local/lib/python2.7/site packages/matplotlib/backends/_backend_tk.py”,第557行,在destroy self.canvas.\u tkcanvas.after_cancel(sel.canvas.\u idle_callback)文件“/usr/lib/python2.7/lib tk/Tkinter.py”,第612行,在after cancer中,可以将该错误放入问题本身的代码块中。这样可读性更好。我已经将您的错误消息传送到主文本,您可以在主文本中格式化它。似乎有一部分不见了。请添加相关信息。已完成。我还补充了一些解释
Exception RuntimeError: RuntimeError('main thread is not in main loop',) in
<bound method PhotoImage.__del__ of <Tkinter.PhotoImage instance at
0x7f38f5412320>> ignored
Tcl_AsyncDelete: async handler deleted by the wrong thread