首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

小弟我的基于Tkinter的串口程序,大家多提意见!

2012-03-12 
我的基于Tkinter的串口程序,大家多提意见!!!Python code#codinggbk__file__ XLiCOM.Py__author_

我的基于Tkinter的串口程序,大家多提意见!!!


Python code
#coding=gbk''''''__file__   = "XLiCOM.Py"__author__ = "XIVN1987@163.com"import sysimport ttkimport timeimport serial import threadingimport Tkinter as Tkimport Tkinter as tkisOpened = threading.Event()RBuf = ''TBuf = ''root = Tk.Tk()ComX = Tk.StringVar(root,'COM1')Baud = Tk.StringVar(root,"9600")Dbit = Tk.StringVar(root,'8')Sbit = Tk.StringVar(root,'1')Chck = Tk.StringVar(root,'None')HexD = Tk.BooleanVar(root,False)HexO = Tk.BooleanVar(root,False)Open = Tk.StringVar(root,u'打开串口')def main():    root.title("XIVN1987's COM Shell")        txt1 = Tk.Text(root,width=81,border=5)    txt1.pack(side='top',padx=3,pady=1,anchor='c')        cnv1 = tk.Canvas(root,height=26,width=580)    cnv1.pack(side='top',padx=0,pady=0,anchor='c')    cnv1.create_window( 30,15,window=ttk.Label(root,text=u'输入框:'))    cnv1.create_window(192,15,window=ttk.Entry(root,width=44))    cnv1.create_window(387,15,window=ttk.Button(root,text=u'发送',width=12))    cnv1.create_window(472,15,window=ttk.Button(root,text=u'清除',width=9))    cnv1.create_window(547,15,window=ttk.Checkbutton(root,text=u'HEX显示',variable=HexD,onvalue=True,offvalue=False))        cnv2 = tk.Canvas(root,height=26,width=580)    cnv2.pack(side='top',padx=0,pady=0,anchor='c')    cnv2.create_window( 30,15,window=ttk.Label(root,text=u'串口号:'))    cnv2.create_window(105,15,window=ttk.Combobox(root,textvariable=ComX,values=['COM1', 'COM2', 'COM3','COM4'],width=12))    cnv2.create_window(202,15,window=ttk.Label(root,text=u'波特率:'))    cnv2.create_window(277,15,window=ttk.Combobox(root,textvariable=Baud,values=['4800','9600','19200'],width=12))    cnv2.create_window(398,15,window=ttk.Button(root,textvariable=Open,width=16,command=lambda:COMOpen(cnv2)))    cnv2.create_oval(470,7,486,23,fill='black',tag='led')    cnv2.create_window(547,15,window=ttk.Checkbutton(root,text=u'HEX发送',variable=HexO,onvalue=True,offvalue=False))        cnv3 = tk.Canvas(root,height=26,width=580)    cnv3.pack(side='top',padx=0,pady=0,anchor='c')    cnv3.create_window( 30,15,window=ttk.Label(root,text=u'数据位:'))    cnv3.create_window(105,15,window=ttk.Combobox(root,textvariable=Dbit,values=['9','8','7','6','5'],width=12))    cnv3.create_window(202,15,window=ttk.Label(root,text=u'停止位:'))    cnv3.create_window(277,15,window=ttk.Combobox(root,textvariable=Sbit,values=['1','2'],width=12))    cnv3.create_window(370,15,window=ttk.Label(root,text=u'校验位:'))    cnv3.create_window(445,15,window=ttk.Combobox(root,textvariable=Chck,values=['None','Odd','Even','Mark','Space'],width=12))    cnv3.create_window(547,15,window=ttk.Button(root,text=u'扩展',width=9))    com_thread = threading.Thread(target=COMTrce)    com_thread.setDaemon(True)    com_thread.start()    root.bind("<<COMRxRdy>>",lambda e: txt1.insert("insert",RBuf))          root.mainloop()COM = serial.Serial()def COMOpen(cnv2):    if not isOpened.isSet():        try:            COM.timeout = 1            COM.xonxoff = 0                COM.port = ComX.get()            COM.parity = Chck.get()[0]            COM.baudrate = int(Baud.get())            COM.bytesize = int(Dbit.get())            COM.stopbits = int(Sbit.get())            COM.open()        except Exception:            print "COM Open Error!"        else:            isOpened.set()            Open.set(u'关闭串口')            cnv2.itemconfig('led',fill='green')    else:        COM.close()        isOpened.clear()        Open.set(u'打开串口')        cnv2.itemconfig('led',fill='black')def COMTrce():    while True:        if isOpened.isSet():            RBuf = COM.read(1)          #read one, with timout            if RBuf:                n = COM.inWaiting()                if n:                    RBuf = RBuf+COM.read(n)                    root.event_generate("<<COMRxRdy>>")                        if len(TBuf)!=0:                COM.write(TBuf)                            time.sleep(0.01)        time.sleep(0.05)if __name__=='__main__':    isOpened.clear()    main() 




[解决办法]
既然你要全局使用的,那总得保存到一个地方要用时调出来吧。
要么把那些全局变量放到一个函数里,在函数最后返回一个字典之类的对象,这个对象还是得赋值给一个全局变量才行
看看其他高手们有什么高见
[解决办法]
探讨

引用:

我觉得这全局变量没啥的,倒是那些控件的坐标较繁琐,还有用canvas只为了画一个灯也是比较奇怪...


呵呵,个人有个人的喜好吧!!!
我也是最近才发现可以把所有的部件widget放在Canvas上,立刻就喜欢上了这种方式,主要是觉得这种坐标的方式有确定性和控制性。。。

当然了,说到底,最主要的还是因为我对pack、grid这些东西……

热点排行