我在使用matplotlib编写tkinter应用程序时遇到问题,该应用程序更新了dinamicaly。我创建了一个绘图并在tkinter窗口中使用它。然后使用tk.after()方法每50ms更新一次图,这在我到目前为止尝试的其他应用程序中工作正常。但在我的应用程序中,一切似乎都在工作,直到一段时间后(一分钟左右)我才收到错误:
RecursionError:调用Python对象时超出最大递归深度
代码是:
import matplotlib
#matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import sys
import time
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
import numpy as np
import matplotlib.pyplot as plt
import math
class MainWindow():
#----------------
def __init__(self, root):
self.index=0
self.root=root
self.fig, self.ax = plt.subplots()
self.line, = self.ax.plot(np.random.randn(100))
#plt.show(block=False)
# a tk.DrawingArea
self.canvas = FigureCanvasTkAgg(self.fig, master=root)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.toolbar = NavigationToolbar2TkAgg(self.canvas, root)
self.toolbar.update()
self.canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.canvas.mpl_connect('key_press_event', self.on_key_event)
self.display = Tk.Label(root, text="") # we need this Label as a variable!
self.display.pack()
self.button1 = Tk.Button(master=root, text='Quit', command=self._quit)
self.button1.pack(side=Tk.BOTTOM)
global w
w=2*math.pi
self.button2 = Tk.Button(master=root, text='Increase frecuency', command=self.button2_event)
self.button2.pack(side=Tk.BOTTOM)
#A simple clock
global miliseconds
global t
t=time.time()
self.update_clock()
print('going to the next stop')
self.root.mainloop()
def on_key_event(event):
print('you pressed %s' % event.key)
key_press_handler(event, self.canvas, toolbar)
def _quit(self):
self.root.quit() # stops mainloop
self.root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
def button2_event(self):
global t
global w
w+=2*3.1416
def update_clock(self):
global t
mili=str(math.floor(1000*(t-time.time())))+' ms'
t=time.time()
now = time.strftime("%H:%M:%S"+'-'+mili)
self.display.configure(text=now)
N=100
y=np.sin(4*math.pi*(np.array(range(N))/N)+t*w)
x=range(N)
self.line.set_ydata(y)
self.line.set_xdata(x)
self.ax.relim()
self.ax.autoscale()
#☺fig.canvas.update()
#fig.canvas.flush_events()
self.canvas.show()
self.canvas.flush_events()
self.root.after(50, self.update_clock()) #<------ERROR HERE!------
root = Tk.Tk()
root.wm_title("Embedding in TK")
MainWindow(root)
答案 0 :(得分:3)
将const fileNames = await fs.readdirAsync(desktopPath);
const files = await Promise.map(fileNames, fileName => Promise.props({
path: `${desktopPath}/${fileName}`,
name: fileName,
ext: fileName.split('.').pop(),
isDirectory: fs.statAsync(`${desktopPath}/${fileName}`);
})));
console.log(files);
return files;
更改为self.root.after(50, self.update_clock())
,after
之后(delay_ms,callback = None,* args)
注册在给定时间后调用的警报回调。