您好,登錄后才能下訂單哦!
忙活了三個多小時,連學帶做,總算是搞出來了一個具有基本功能的串口通信PC機的GUI界面,Tkinter在python中確實很好用,而且代碼量確實也很少,不足的是Tkinter不自帶combox,但是幸運的是我下載的2.7版本自帶了包含有combox的ttk模塊,于是乎問題就順利解決了。
下面是源代碼,一些錯誤提示功能還沒有做,目前只是簡單地實現了下位機與PC的通信界面,下位機還是用的STM32F103
#encoding=utf-8 __author__ = 'freedom' from Tkinter import * from serial import * import ttk class GUI(Frame): def __init__(self,master): frame = Frame(master) frame.pack() #串口設置相關變量 self.port = 0 self.baudrate = 9600 #串口號提示 self.lab1 = Label(frame,text = 'Serial Number') self.lab1.grid(row = 0,column = 0,sticky = W) #串口號選擇下拉菜單 self.boxValue = StringVar() self.boxChoice = ttk.Combobox(frame,textvariable = self.boxValue,state = 'readonly') self.boxChoice['value'] = ('COM1','COM2','COM3','COM4') self.boxChoice.current(0) self.boxChoice.bind('<<ComboboxSelected>>',self.Choice) self.boxChoice.grid(row = 1,column = 0,sticky = W) #波特率選擇提示 self.lab2 = Label(frame,text = 'Baudrate Set') self.lab2.grid(row = 2,column = 0,sticky = W) #波特率選擇下拉菜單 self.boxValueBaudrate = IntVar() self.BaudrateChoice = ttk.Combobox(frame,textvariable = self.boxValueBaudrate,state = 'readonly') self.BaudrateChoice['value'] = (9600,115200) self.BaudrateChoice.current(0) self.BaudrateChoice.bind('<<ComboboxSelected>>',self.ChoiceBaudrate) self.BaudrateChoice.grid(row = 3,column = 0,sticky = W) #輸出框提示 self.lab3 = Label(frame,text = 'Message Show') self.lab3.grid(row = 0,column = 1,sticky = W) #輸出框 self.show = Text(frame,width = 40,height = 5,wrap = WORD) self.show.grid(row = 1,column = 1,rowspan = 4,sticky = W) #輸入框提示 self.lab4 = Label(frame,text = 'Input here,please!') self.lab4.grid(row = 5,column = 1,sticky = W) #輸入框 self.input = Entry(frame,width = 40) self.input.grid(row = 6,column = 1,rowspan = 4,sticky = W) #輸入按鈕 self.button1 = Button(frame,text = "Input",command = self.Submit) self.button1.grid(row = 11,column = 1,sticky = E) #串口開啟按鈕 self.button2 = Button(frame,text = 'Open Serial',command = self.open) self.button2.grid(row = 7,column = 0,sticky = W) #串口關閉按鈕 self.button3 = Button(frame,text = 'Close Serial',command = self.close) self.button3.grid(row = 10,column = 0,sticky = W) #串口信息提示框 self.showSerial = Text(frame,width = 20,height = 2,wrap = WORD) self.showSerial.grid(row = 12,column = 0,sticky = W) #串口初始化配置 self.ser = Serial() self.ser.setPort(self.port) #self.ser.setBaudrate(self.baudrate) #self.ser.open() #print self.ser.isOpen() #print self.ser def Choice(self,event): context = self.boxValue.get() list = ["COM1",'COM2','COM3','COM4'] if context in list: self.port = list.index(context) self.ser.setPort(self.port) print self.port def ChoiceBaudrate(self,event): self.baudrate = self.boxValueBaudrate.get() self.ser.setBaudrate(self.baudrate) print self.baudrate def Submit(self): context1 = self.input.get() n = self.ser.write(context1) output = self.ser.read(n) print output self.show.delete(0.0,END) self.show.insert(0.0,output) def open(self): self.ser.open() if self.ser.isOpen() == True: self.showSerial.delete(0.0,END) self.showSerial.insert(0.0,"Serial has been opend!") def close(self): self.ser.close() if self.ser.isOpen() == False: self.showSerial.delete(0.0,END) self.showSerial.insert(0.0,"Serial has been closed!") root = Tk() root.title("Serial GUI") #root.geometry("3000x4000") app = GUI(root) root.mainloop()
以上這篇對Python 簡單串口收發GUI界面的實例詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。