#串口開啟按鈕
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()
評論
查看更多