我正在尝试将 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++ 运行时以不寻常的方式终止。我做错了什么?