Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python tkinter中嵌入matplotlib(运行时错误)_Python_Matplotlib_Tkinter - Fatal编程技术网

在python tkinter中嵌入matplotlib(运行时错误)

在python tkinter中嵌入matplotlib(运行时错误),python,matplotlib,tkinter,Python,Matplotlib,Tkinter,我正在尝试将matplotlib条形图嵌入到python tkinter中。这是我的代码: import matplotlib matplotlib.use('TkAgg') import numpy as np import matplotlib.pyplot as plt import matplotlib.mlab as mlab from Tkinter import* from matplotlib.backends.backend_tkagg import FigureCanvasT

我正在尝试将matplotlib条形图嵌入到python tkinter中。这是我的代码:

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from Tkinter import*
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

def trend_quit():
   trend_root.quit()
   trend_root.destroy()
fig=plt.figure()
ax=fig.add_subplot(211)

systems=['system a','system b','system c','system d','system e']
N=len(systems)
ind=np.arange(N)
width=0.25
list_a=(1,3,4,3,6)
x=np.arange(0,5,1)
ax.set_xlabel('System')
ax.set_ylabel('Number of faults')
ax.set_ylim(0, 6)
ax.set_xticks(ind+width)
ax.set_xticklabels(systems,rotation='vertical')
rects1=ax.bar(ind+0.11,list_a,width,color='b')
rects2=ax.plot(x,list_a,color='r')
ax.grid(True)

trend_root=Tk()
trend_root.overrideredirect(True)
trend_root.wm_geometry("%dx%d+%d+%d" % (1200, 600, 50, 100))
trend_root.config(bg='white',relief=SOLID,bd=1)

trend_frame1=Frame(trend_root,bg='black',width=1200,relief=GROOVE,height=50)
trend_frame1.place(x=0,y=0)

trend_frame2=Frame(trend_root,bg='black',width=1000,relief=GROOVE,height=700)
trend_frame2.place(x=20,y=60)

trend_label1=Label(trend_frame1,text="Trends",font=('calibri',(18)),bg='black',fg='white')
trend_label1.place(x=10,y=5)

trend_button1=Button(trend_frame1,text='EXIT',font=('calibri',(12)),bg='red',command=trend_quit)
trend_button1.place(x=1100,y=10)

canvas = FigureCanvasTkAgg(fig, master=trend_frame2)
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,trend_frame2 )
toolbar.update()
canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)

def on_key_event(event):
    print('you pressed %s'%event.key)
    key_press_handler(event, canvas, toolbar)

canvas.mpl_connect('key_press_event', on_key_event)
trend_root.mainloop()

一切正常!但是当我退出我使用的退出按钮时,它给了我一个错误的信息,说“C++运行时错误”——这个应用程序请求C++运行时间以异常的方式终止。我做错了什么?

我设法解决了这个问题。我不知道问题的确切原因,但在我卸载pythonwin后,它似乎工作正常,没有给我任何错误消息。感谢A.Rodas提供的关于Python的链接。

我无法重现这个问题,您的代码对我来说很好。然而,我发现这可能是相关的。黑暗中的一枪:你是从闲置者那里运行这个吗?我不是在使用闲置者,只是出于好奇。。它不能从空闲状态运行吗?总之,你提供的链接给了我一些线索。。我现在正在用蟒蛇。。因此,我将再次尝试使用其他IDE。非常感谢你的帮助!有时候我也会犯这个错误。难道它只适用于机智的线程吗?你开过线吗?