本文实例讲述了Python实现在tkinter中使用matplotlib绘制图形的方法。分享给大家供大家参考,具体如下:
一. 代码:
# coding=utf-8import sysimport Tkinter as Tkimport matplotlibfrom numpy import arange, sin, pifrom matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAggfrom matplotlib.backend_bases import key_press_handlerfrom matplotlib.figure import Figurematplotlib.use('TkAgg')root =Tk.Tk()root.title("VEVB武林网测试 - matplotlib in TK")#设置图形尺寸与质量f =Figure(figsize=(5,4), dpi=100)a = f.add_subplot(111)t = arange(0.0,3,0.01)s = sin(2*pi*t)#绘制图形a.plot(t, s)#把绘制的图形显示到tkinter窗口上canvas =FigureCanvasTkAgg(f, master=root)canvas.show()canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)#把matplotlib绘制图形的导航工具栏显示到tkinter窗口上toolbar =NavigationToolbar2TkAgg(canvas, root)toolbar.update()canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.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)#按钮单击事件处理函数def _quit(): #结束事件主循环,并销毁应用程序窗口 root.quit() root.destroy()button =Tk.Button(master=root, text='Quit', command=_quit)button.pack(side=Tk.BOTTOM)Tk.mainloop()
二. 运行结果:
希望本文所述对大家Python程序设计有所帮助。
新闻热点
疑难解答