忙活了三个多小时,连学带做,总算是搞出来了一个具有基本功能的串口通信pc机的gui界面,tkinter在python中确实很好用,而且代码量确实也很少,不足的是tkinter不自带combox,但是幸运的是我下载的2.7版本自带了包含有combox的ttk模块,于是乎问题就顺利解决了。
下面是源代码,一些错误提示功能还没有做,目前只是简单地实现了下位机与pc的通信界面,下位机还是用的stm32f103
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#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界面的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/freedom098/article/details/48211567