pythongui界⾯制作显⽰串⼝数据_对Python简单串⼝收发GUI
界⾯的实例详解
忙活了三个多⼩时,连学带做,总算是搞出来了⼀个具有基本功能的串⼝通信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')
id(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('<>',self.Choice)
id(row = 1,column = 0,sticky = W)
#波特率选择提⽰
self.lab2 = Label(frame,text = 'Baudrate Set')
id(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('<>',self.ChoiceBaudrate)
id(row = 3,column = 0,sticky = W)
#输出框提⽰
self.lab3 = Label(frame,text = 'Message Show')
id(row = 0,column = 1,sticky = W)
#输出框
self.show = Text(frame,width = 40,height = 5,wrap = WORD)
id(row = 1,column = 1,rowspan = 4,sticky = W)
#输⼊框提⽰
self.lab4 = Label(frame,text = 'Input here,please!')
id(row = 5,column = 1,sticky = W)
#输⼊框
self.input = Entry(frame,width = 40)
id(row = 6,column = 1,rowspan = 4,sticky = W)
#输⼊按钮
self.button1 = Button(frame,text = "Input",command = self.Submit) id(row = 11,column = 1,sticky = E)
#串⼝开启按钮
self.button2 = Button(frame,text = 'Open Serial',command = self.open) id(row = 7,column = 0,sticky = W)
#串⼝关闭按钮
self.button3 = Button(frame,text = 'Close Serial',command = self.close) id(row = 10,column = 0,sticky = W)
#串⼝信息提⽰框
self.showSerial = Text(frame,width = 20,height = 2,wrap = WORD) id(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 = ()
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.ser.setBaudrate(self.baudrate)
print self.baudrate
def Submit(self):
context1 = ()
n = self.ser.write(context1)
output = ad(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)
python怎么读取串口数据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")
#ry("3000x4000")
app = GUI(root)
root.mainloop()
以上这篇对Python 简单串⼝收发GUI界⾯的实例详解就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持⼀教程⽹。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。