对Python 简单串口收发GUI界面的实例详解

yipeiwu_com5年前Python基础

忙活了三个多小时,连学带做,总算是搞出来了一个具有基本功能的串口通信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界面的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python调用C语言的实现

Python中的ctypes模块可能是Python调用C方法中最简单的一种。ctypes模块提供了和C语言兼容的数据类型和函数来加载dll文件,因此在调用时不需对源文件做任何的修改。也正...

Atom的python插件和常用插件说明

python: simplified-chinese-menu:中文汉化(英文差的) 代码高亮:Atom自带 自动补全:autocomplete-python 语法检查:linter-f...

PyQt5每天必学之日历控件QCalendarWidget

PyQt5每天必学之日历控件QCalendarWidget

QCalendarWidget 是日历控件。它允许用户以简单和直观的方式选择日期。 #!/usr/bin/python3 # -*- coding: utf-8 -*- """ P...

python之django母板页面的使用

其实就是利用{% block xxx %}   {% endblock %}的方式定义一个块,相当于占位。存放在某个html中,比如base.html 然后在需要实现...

python下PyGame的下载与安装过程及遇到问题

1.去官网下载PyGame    注意:要下载对应版本的包    官网地址:http://www.pygame.org/download.shtm...