我正在尝试使用matplotlib在python tkinter上创建条形图。一切正常,除了当我关闭tkinter(console)窗口时,它会向我发送此消息。
在关闭控制台之前,我已经关闭了tkinter窗口,因此我不确定它指的是“仍在运行”的进程。当我选择杀死它时,会给我以下错误消息:
仅当我嵌入了matplotlib代码时,才会发生这种情况。当我从脚本中删除matplotlib代码时,它不再给我该消息,并且可以正常工作。我的代码如下:
导入我的脚本
import Tkinter as tk
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
from datetime import datetime
Matplotlib相关代码
frame=tk.Frame(parent,width=900,height=500,bg='lightgreen',relief='groove',bd=1)
frame.place(x=10,y=10)
fig=plt.figure()
ax=fig.add_subplot(211)
canvas = FigureCanvasTkAgg(fig, master=frame)
canvas.show()
canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
canvas._tkcanvas.pack(side='top', fill='both', expand=1)
toolbar = NavigationToolbar2TkAgg(canvas,frame )
toolbar.update()
canvas._tkcanvas.pack(side='top', fill='both', expand=1)
def on_key_event(event):
key_press_handler(event, canvas, toolbar)
是什么原因引起的?
我正在将Windows8(64位)与python 2.7.4,matplotlib 1.2.0(64bits)用于python 2.7
最佳答案
您正在运行多个gui事件循环(一个来自TK,另一个plt
启动)。不要导入plt
或mlab
。有关如何正确嵌入Tk
的信息,请参见http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html。
您基本上需要更改此行
fig=plt.figure()
至
fig = Figure()
并删除进口中的
plt
。关于python - 关闭控制台后,Python Matplotlib运行时错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17252566/