关闭控制台时出现 Python Matplotlib 运行时错误

2023-12-10

我正在尝试使用 matplotlib 在 python tkinter 上创建条形图。一切正常,除了当我关闭 tkinter(console) 窗口时,它给我这条消息。

enter image description here

在关闭控制台之前我已经关闭了 tkinter 窗口,所以我不确定它指的是哪个进程“仍在运行”。当我选择杀死它时,它给我这个错误消息:

enter image description here

仅当我嵌入 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(64 位)用于 python 2.7


你有多个 gui 事件循环正在运行(一个来自你的 TK,一个来自你的 TK)plt开始)。不导入plt or mlab. See http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html了解如何正确嵌入Tk.

你基本上需要改变这一行

fig=plt.figure()

to

fig = Figure()

并摆脱plt在您的进口中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关闭控制台时出现 Python Matplotlib 运行时错误 的相关文章

随机推荐