python plt figure_Tkinter plt.figure()不会绘制,但Figure()会绘制

#!/usr/bin/python3importtkinterastkfromtkinterimportttkfromtkinterimportmessageboximportthreadingimportqueueimporttimefrommatplotlib.figureimportFigureimportmatplotlib.animationasanimationfrommatplotlib.backends.backend_tkaggimportFigureCanvasTkAggtry:frommatplotlib.backends.backend_tkaggimportNavigationToolbar2Tkasnav_toolexcept:frommatplotlib.backends.backend_tkaggimportNavigationToolbar2TkAggasnav_toolimportnumpyasnpclassMyThread(threading.Thread):def__init__(self,queue,which,ops,interval):threading.Thread.__init__(self)self.queue=queue

self.check=Trueself.which=which

self.ops=ops

self.interval=intervaldefstop(self):self.check=Falsedefrun(self):whileself.check:ifself.which.get()==0:lam=np.random.exponential(scale=.1,size=100).reshape(-1,1)else:lam=np.random.normal(loc=5,scale=1,size=100).reshape(-1,1)time.sleep(self.interval.get())args=(lam,self.ops[self.which.get()])self.queue.put(args)else:args=(None,"I'm stopped")self.queue.put(args)classMain(ttk.Frame):def__init__(self,parent):super().__init__()self.parent=parent

self.which=tk.IntVar()self.interval=tk.DoubleVar()self.queue=queue.Queue()self.my_thread=Noneself.init_ui()definit_ui(self):f=ttk.Frame()#create graph!self.fig=Figure()self.fig.suptitle("Hello Matplotlib",fontsize=16)self.a=self.fig.add_subplot(111)self.canvas=FigureCanvasTkAgg(self.fig,f)toolbar=nav_tool(self.canvas,f)toolbar.update()self.canvas._tkcanvas.pack(fill=tk.BOTH,expand=1)w=ttk.Frame()ttk.Button(w,text="Animate",command=self.launch_thread).pack()ttk.Button(w,text="Stop",command=self.stop_thread).pack()ttk.Button(w,text="Close",command=self.on_close).pack()self.ops=('Exponential','Normal',)self.get_radio_buttons(w,'Choice',self.ops,self.which,self.on_choice_plot).pack(side=tk.TOP,fill=tk.Y,expand=0)ttk.Label(w,text="Interval").pack()tk.Spinbox(w,bg='white',from_=1.0,to=5.0,increment=0.5,justify=tk.CENTER,width=8,wrap=False,insertwidth=1,textvariable=self.interval).pack(anchor=tk.CENTER)w.pack(side=tk.RIGHT,fill=tk.BOTH,expand=1)f.pack(side=tk.LEFT,fill=tk.BOTH,expand=1)deflaunch_thread(self):self.on_choice_plot()defstop_thread(self):ifself.my_threadisnotNone:if(threading.active_count()!=0):self.my_thread.stop()defon_choice_plot(self,evt=None):ifself.my_threadisnotNone:if(threading.active_count()!=0):self.my_thread.stop()self.my_thread=MyThread(self.queue,self.which,self.ops,self.interval)self.my_thread.start()self.periodiccall()defperiodiccall(self):self.checkqueue()ifself.my_thread.is_alive():self.after(1,self.periodiccall)else:passdefcheckqueue(self):whileself.queue.qsize():try:args=self.queue.get()self.a.clear()self.a.grid(True)ifargs[0]isnotNone:self.a.step(list(range(100)),list(args[0]))self.a.set_title(args[1],weight='bold',loc='left')else:self.a.set_title(args[1],weight='bold',loc='left')self.canvas.draw()exceptqueue.Empty:passdefget_radio_buttons(self,container,text,ops,v,callback=None):w=ttk.LabelFrame(container,text=text,)forindex,textinenumerate(ops):ttk.Radiobutton(w,text=text,variable=v,command=callback,value=index,).pack(anchor=tk.W)returnwdefon_close(self):ifself.my_threadisnotNone:if(threading.active_count()!=0):self.my_thread.stop()self.parent.on_exit()classApp(tk.Tk):"""Start here"""def__init__(self):super().__init__()self.protocol("WM_DELETE_WINDOW",self.on_exit)self.set_title()self.set_style()Main(self)defset_style(self):self.style=ttk.Style()#('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')self.style.theme_use("clam")defset_title(self):s="{0}".format('Simple App')self.title(s)defon_exit(self):"""Close all"""ifmessagebox.askokcancel("Simple App","Do you want to quit?",parent=self):self.destroy()if__name__=='__main__':app=App()app.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值