博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tkinter 例子
阅读量:7058 次
发布时间:2019-06-28

本文共 5719 字,大约阅读时间需要 19 分钟。

#!/usr/bin/env pythonimport pylabfrom pylab import *import Tkinterfrom Tkinter import *import serialfrom numpy  import *from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAggroot = Tkinter.Tk()root.wm_title("Chocolate Oscilloscope (By Wenzhe_Qiu)")xAchse=pylab.arange(0,100,1)yAchse=pylab.array([0]*100)fig = pylab.figure(1)ax = fig.add_subplot(111)ax.grid(True)ax.set_title("Chocolate Oscilloscope Frontend (COM4)")ax.set_xlabel("Time (ms)")ax.set_ylabel("Amplitude (V)")ax.axis([0,100,-1.5,1.5])line1=ax.plot(xAchse,yAchse,'-')canvas = FigureCanvasTkAgg(fig, master=root)canvas.show()canvas.get_tk_widget().pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)toolbar = NavigationToolbar2TkAgg( canvas, root )toolbar.update()canvas._tkcanvas.pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)values=[]values = [0 for x in range(100)]run=1sampling_freq=1000sampling_time=float(1000/float(sampling_freq))NumberSamples=100  #s=serial.Serial(port='COM4')  #open port COM4s=serial.Serial(port='COM4', baudrate=115200)#Receive data by pc.putc()def waveformGenerator():  global values  _next=s.readline()  _next=float(_next)/1000  if(run==1):    if(_next<=1):     _next=3.3 * _next     values.append(_next)  root.after(0,waveformGenerator)  #def RealtimePloter():#  global values,wScale,NumberSamples,sampling_time#  NumberSamples=min(len(values),wScale.get())#  real_time=NumberSamples*sampling_time;#  interval=sampling_time#  CurrentXAxis=linspace(0,real_time,NumberSamples)#  line1[0].set_data(CurrentXAxis,pylab.array(values[-NumberSamples:]))#  ax.axis([0,real_time,-0.5,4])#  canvas.draw()#  root.after(25,RealtimePloter)def RealtimePloter():  global values,wScale,sampling_freq,interval  NumberSamples=min(len(values),wScale.get()*sampling_freq/1000)  interval=float(float (wScale.get())/float (NumberSamples))  CurrentXAxis=linspace(0,wScale.get(),NumberSamples)   line1[0].set_data(CurrentXAxis,pylab.array(values[-NumberSamples:]))  ax.axis([0,wScale.get(),-0.5,4])  canvas.draw()  root.after(25,RealtimePloter) def _quit():    print "Bye"    s.close()       # close the port    root.quit()     # stops mainloop    root.destroy()  # this is necessary on Windows to prevent                    # Fatal Python Error: PyEval_RestoreThread: NULL tstatedef _set():    global sampling_time    s.write(str(wScale_sample_freq.get())+'\n')    sampling_freq=wScale_sample_freq.get()    sampling_time=float(float(1/float(sampling_freq))*1000)    print "The current sampling frequency is " + str(wScale_sample_freq.get()) + "Hz !"    print "The sampling time interval is " + str(sampling_time) + " ms"     def _PS():    s.write("2000\n")def _STOP_IMAGE():  global run  run=~rundef _channel_change_to_15():    s.write("1010\n")    print"The current channel is 15 !"def _channel_change_to_16():    s.write("1020\n")    print"The current channel is 16 !"def _channel_change_to_17():    s.write("1030\n")    print"The current channel is 17 !"def _channel_change_to_18():    s.write("1040\n")    print"The current channel is 18 !"def _channel_change_to_19():    s.write("1050\n")    print"The current channel is 19 !"def _channel_change_to_20():    s.write("1060\n")    print"The current channel is 20 !"########## frame_widget_definition #########frame_1 = Frame(height=10, bd=1, relief=SUNKEN)frame_2 = Frame(height=10, bd=1, relief=GROOVE)frame_3 = Frame(master=frame_2, height=10, bd=1, relief=GROOVE)#relief:Border decoration. The default is FLAT. Other possible values are SUNKEN, RAISED, GROOVE, and RIDGE#frame widget reference:http://effbot.org/tkinterbook/frame.htmframe_1.pack(side=Tkinter.LEFT)frame_2.pack(side=Tkinter.RIGHT)frame_3.pack(side=Tkinter.BOTTOM)########## button_widget_definition #########button = Tkinter.Button(master=frame_3, text='  SET  ', command=_set)button_PS = Tkinter.Button(master=frame_3, text=' PAUSE/START CHANNEL ', command=_PS)button_SI = Tkinter.Button(master=frame_3, text='    STOP/RUN IMAGE   ', command=_STOP_IMAGE)button_quit = Tkinter.Button(master=frame_3, text='  QUIT  ', command=_quit)button.pack(side=Tkinter.LEFT)button_PS.pack(side=Tkinter.LEFT)button_SI.pack(side=Tkinter.LEFT)button_quit.pack(side=Tkinter.RIGHT)########## wScale_widget_definition #########wScale = Tkinter.Scale(master=frame_2,label="Time Scale (ms):", from_=1, to=1000,sliderlength=30,length=ax.get_frame().get_window_extent().width, orient=Tkinter.HORIZONTAL)wScale_sample_freq = Tkinter.Scale(master=frame_2,label="Sampling Frequency (Hz): (Click SET button for comfirmation)", from_=3, to=1000,sliderlength=30,length=ax.get_frame().get_window_extent().width, orient=Tkinter.HORIZONTAL)wScale.pack(side=Tkinter.TOP)wScale_sample_freq.pack(side=Tkinter.TOP)wScale.set(500)wScale_sample_freq.set(1000)########## radiobutton_widget_definition #########v = IntVar()v.set(1)print "The default channel is 15 !"Radiobutton(frame_1, text="Channel 15", variable=v, value=1, command=_channel_change_to_15).pack(anchor=W)Radiobutton(frame_1, text="Channel 16", variable=v, value=2, command=_channel_change_to_16).pack(anchor=W)Radiobutton(frame_1, text="Channel 17", variable=v, value=3, command=_channel_change_to_17).pack(anchor=W)Radiobutton(frame_1, text="Channel 18", variable=v, value=4, command=_channel_change_to_18).pack(anchor=W)Radiobutton(frame_1, text="Channel 19", variable=v, value=5, command=_channel_change_to_19).pack(anchor=W)Radiobutton(frame_1, text="Channel 20", variable=v, value=6, command=_channel_change_to_20).pack(anchor=W)# radiobutton widget reference:http://effbot.org/tkinterbook/radiobutton.htmroot.after(100,waveformGenerator)root.after(100,RealtimePloter)Tkinter.mainloop()

 

转载于:https://www.cnblogs.com/cosmo89929/archive/2012/12/18/2822635.html

你可能感兴趣的文章
《田志刚:第二代知识管理解密》PPT下载
查看>>
分布式系统实践技术交流活动
查看>>
LAMP一体环境快速安装
查看>>
linux定时任务Crond之调试定时任务7条生产经验12
查看>>
轻松精通awk数组企业问题案例
查看>>
K8S集群中部署jenkins
查看>>
Zabbix IPMI 调试的问题
查看>>
基于seajs加载模块的入口脚本
查看>>
How To Update Local IPS Package Repository
查看>>
韩寒的经典格言有哪些?
查看>>
OpenStack Swift 对象存储管理(六)
查看>>
SHELL编程练习-获得指定目录下的所有文件及文件夹的大小
查看>>
XML 命名空间(XML Namespaces)
查看>>
一个IT人的未来短期计划和阶段总结
查看>>
openstack issue 4
查看>>
一次真实的网购装机实战经历
查看>>
通过virt工具安装管理KVM虚拟机
查看>>
Hadoop测试常见问题和测试方法
查看>>
利用IPSec安全策略阻断内网违规外联(一)
查看>>
运维85条军规
查看>>