问题描述
我正在尝试使用matplotlib在python tkinter上创建条形图.一切正常,除了当我关闭tkinter(console)窗口时,它会显示此消息.
在关闭控制台之前,我已经关闭了tkinter窗口,因此我不确定它指的是仍在运行"的进程.当我选择杀死它时,它会给我以下错误消息:
仅当我嵌入了matplotlib代码时,才会发生这种情况.当我从脚本中删除matplotlib代码时,它不再给我此消息,并且可以正常工作.我的代码如下:
为我的脚本导入
将Tkinter导入为tk导入 matplotlibmatplotlib.use('TkAgg')将numpy导入为np导入matplotlib.pyplot作为plt将matplotlib.mlab导入为mlab从 matplotlib.backends.backend_tkagg 导入 FigureCanvasTkAgg, NavigationToolbar2TkAgg从 matplotlib.backend_bases 导入 key_press_handlerfrom matplotlib.figure 导入图从日期时间导入日期时间
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)画布.show()canvas.get_tk_widget().pack(side='top', fill='both', expand=1)canvas._tkcanvas.pack(side='top', fill='both', expand=1)工具栏 = NavigationToolbar2TkAgg(canvas,frame)工具栏.update()canvas._tkcanvas.pack(side ='top',fill ='both',expand = 1)def on_key_event(事件):key_press_handler(事件,画布,工具栏)
是什么原因引起的问题?
我正在将Windows8(64位)与python 2.7.4,matplotlib 1.2.0(64位)用于python 2.7
您有多个 gui 事件循环在运行(一个来自您的 TK,一个来自 plt
开始).不要导入 plt
或 mlab
.有关如何操作方法,请参见 http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html 正确嵌入Tk
.
您基本上需要更改此行
fig = plt.figure()
到
fig = Figure()
,并删除导入中的 plt
.
I am trying to create a bar chart on python tkinter with matplotlib.Everything works fine except that when i close the tkinter(console) window, it give me this message.
I have already closed my tkinter window before closing my console so i am not sure which process it is referring to 'still running'. When i choose to kill it it give me this error message:
It only happen when i have my matplotlib code embedded. When i remove my matplotlib codes from my script, it doesn't give me this message anymore and works fine. My code is as below:
Imports for my script
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 related code
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)
What is causing the problem?
I am using windows8 (64 bits) with python 2.7.4, matplotlib 1.2.0(64bits) for python 2.7
You have multiple gui event loops running (the one from you TK, and the one plt
starts). Do not import plt
or mlab
. See http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html for how to properly embed in Tk
.
You basically need to change this line
fig=plt.figure()
to
fig = Figure()
and get rid of plt
in your imports.
这篇关于关闭控制台时出现 Python Matplotlib 运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!