Chris Aung
Chris Aung

Reputation: 9532

Python Matplotlib runtime error upon closing the console

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.

enter image description here

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:

enter image description here

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

Upvotes: 1

Views: 2228

Answers (1)

tacaswell
tacaswell

Reputation: 87546

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.

Upvotes: 3

Related Questions