公司接了个活,承办一个由团委组织的五四青年节徒步活动,其中一个环节是现场报名,来的人把名字填进去后随机分组,并显示在现场的LED大屏幕上,我自告奋勇用Python来开发这个小程序。这里记录一下
【项目需求】
1、报名录入姓名和赛程,数据传到LED大屏所在的电脑上
2、赛程分半程和全程,半程的又分为ABC三个路线,每个路线下面有若干个分组,全程下面也分6个组,要求签到以后把人员随机分组
3、在大屏幕上显示出来
【分析】
1、用tkinter做一个输入界面,用一个文本框让用户输入名字,然后用一个单选框让用户选择半程还是全程,然后数据用socket传给连接现场LED屏幕的电脑
2、写一个函数处理分组工作,根据接收到的全程还是半程信息,分别进行随机分组
3、写一个服务器端程序在连接LED的电脑上运行,显示界面,并把处理结果及时更新到大屏幕。
这包括三个部分:
a) 用tkinter做一个显示界面,包括全程和半程ABC的区分,这可以用frame来实现,然后下面再细分分组,可以用label和text来实现
b) 搭一个简单的服务器用socket接收来自客户端的信息
c) 写一个随机分组函数,写一个清除函数
【项目代码】
1、客户端代码,负责录入数据,上传到服务器,命名为client.py
import tkinter as tk
from tkinter import *
root = Tk()
root.title('报名签到')
root.geometry('300x500')
title1=Label(root,fg='blue',text='填入姓名',font=('华文新魏',12))
name=Entry(root,bg='white',width=20,font=('楷体',24))
data_list=[]
def mychoice():
global data_list
val=var.get()
if len(data_list)==0:
data_list.append(val)
else:
data_list=[]
data_list.append(val)
return val
var = StringVar()
zubie_label=Label(root,fg='blue',text='选择赛程',font=('华文新魏',12))
zb1 = Radiobutton(root,text="全程",variable=var,value='q',command=mychoice)
zb2 = Radiobutton(root,text="半程",variable=var,value='b',command=mychoice)
var.set(0)
def tijiao():
global data_list
if len(data_list)==0:
print('未选择赛程')
else:
if name.get():
data_list.append(name.get())
print(data_list)
data_str=",".join(data_list)
msg=data_str
print(msg)
# 导入 socket、sys 模块
import socket
# 创建 socket 对象
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 获取本地主机名
#host = socket.gethostname()
host = '*.*.*.*' #替换成服务器IP地址
# 设置端口号
port = 9999
# 连接服务,指定主机和端口
s.connect((host, port))
s.send(msg.encode('utf-8'))
data = s.recv(1024)
print(data.decode("utf8"))
s.close()
data_list=[]
name.delete(0,END)
else:
print('未填写姓名')
btn1=Button(root,fg='blue',text='提交',command=tijiao)
title1.place(relx=0.25,rely=0.25,height=30,width=120)
name.place(relx=0.3,rely=0.35,height=30,width=100)
zubie_label.place(relx=0.3,rely=0.45,height=30,width=100)
zb1.place(relx=0.5,rely=0.55,height=30,width=100)
zb2.place(relx=0.7,rely=0.55,height=30,width=100)
btn1.place(relx=0.3,rely=0.65,height=30,width=80)
root.mainloop()
2、服务器端代码,实现接收信息,处理分组,并在大屏幕显示,命名为server.py
import random,socket
from tkinter import *
class FullScreenApp(object):
def __init__(self, master, **kwargs):
self.master=master
pad=3
self._geom='1200x600+0+0'
master.geometry("{0}x{1}+0+0".format(
master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
master.bind('<Escape>',self.toggle_geom)
def toggle_geom(self,event):
geom=self.master.winfo_geometry()
print(geom,self._geom)
self.master.geometry(self._geom)
self._geom=geom
root = Tk()
root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
def toggle_geom():
root.geometry('1200x600+0+0')
root.bind('<Escape>',toggle_geom)
root.title('报名分组情况')
#root.geometry('1200x600')
frame1 = Frame(root,bg='brown')
fenzu_Q = Label(frame1,fg='red',text='全程组')
quancheng_1 = Label(frame1,bg='white',fg='green',text='Q1队',font=('楷体',18))
qctext_1 = Text(frame1,bg='white',fg='red',font=('楷体',18))
quancheng_2 = Label(frame1,bg='white',fg='green',text='Q2队',font=('楷体',18))
qctext_2 = Text(frame1,bg='white',fg='red',font=('楷体',18))
quancheng_3 = Label(frame1,bg='white',fg='green',text='Q3队',font=('楷体',18))
qctext_3 = Text(frame1,bg='white',fg='red',font=('楷体',18))
quancheng_4 = Label(frame1,bg='white',fg='green',text='Q4队',font=('楷体',18))
qctext_4 = Text(frame1,bg='white',fg='red',font=('楷体',18))
quancheng_5 = Label(frame1,bg='white',fg='green',text='Q5队',font=('楷体',18))
qctext_5 = Text(frame1,bg='white',fg='red',font=('楷体',18))
frame2 = Frame(root,bg='green')
fenzu_A = Label(frame2,fg='red',text='半程A组')
zu_1 = Label(frame2,bg='white',fg='green',text='B1队',font=('楷体',18))
zutext_1 = Text(frame2,bg='white',fg='red',font=('楷体',18))
zu_2 = Label(frame2,bg='white',fg='green',text='B2队',font=('楷体',18))
zutext_2 = Text(frame2,bg='white',fg='red',font=('楷体',18))
zu_3 = Label(frame2,bg='white',fg='green',text='B3队',font=('楷体',18))
zutext_3 = Text(frame2,bg='white',fg='red',font=('楷体',18))
zu_4 = Label(frame2,bg='white',fg='green',text='B4队',font=('楷体',18))
zutext_4 = Text(frame2,bg='white',fg='red',font=('楷体',18))
zu_5 = Label(frame2,bg='white',fg='green',text='B5队',font=('楷体',18))
zutext_5 = Text(frame2,bg='white',fg='red',font=('楷体',18))
zu_6 = Label(frame2,bg='white',fg='green',text='B6队',font=('楷体',18))
zutext_6 = Text(frame2,bg='white',fg='red',font=('楷体',18))
frame3 = Frame(root,bg='blue')
fenzu_B = Label(frame3,fg='red',text='半程B组')
zu_7 = Label(frame3,bg='white',fg='green',text='B7队',font=('楷体',18))
zutext_7 = Text(frame3,bg='white',fg='red',font=('楷体',18))
zu_8 = Label(frame3,bg='white',fg='green',text='B8队',font=('楷体',18))
zutext_8 = Text(frame3,bg='white',fg='red',font=('楷体',18))
zu_9 = Label(frame3,bg='white',fg='green',text='B9队',font=('楷体',18))
zutext_9 = Text(frame3,bg='white',fg='red',font=('楷体',18))
zu_10 = Label(frame3,bg='white',fg='green',text='B10队',font=('楷体',18))
zutext_10 = Text(frame3,bg='white',fg='red',font=('楷体',18))
zu_11 = Label(frame3,bg='white',fg='green',text='B11队',font=('楷体',18))
zutext_11 = Text(frame3,bg='white',fg='red',font=('楷体',18))
frame4 = Frame(root,bg='orange')
fenzu_C = Label(frame4,fg='red',text='半程C组')
zu_12 = Label(frame4,bg='white',fg='green',text='B12队',font=('楷体',18))
zutext_12 = Text(frame4,bg='white',fg='red',font=('楷体',18))
zu_13 = Label(frame4,bg='white',fg='green',text='B13队',font=('楷体',18))
zutext_13 = Text(frame4,bg='white',fg='red',font=('楷体',18))
zu_14 = Label(frame4,bg='white',fg='green',text='B14队',font=('楷体',18))
zutext_14 = Text(frame4,bg='white',fg='red',font=('楷体',18))
zu_15 = Label(frame4,bg='white',fg='green',text='B15队',font=('楷体',18))
zutext_15 = Text(frame4,bg='white',fg='red',font=('楷体',18))
frame1.place(x=10,rely=0.1,relheight=0.8,relwidth=0.2)
fenzu_Q.place(relx=0.3,rely=0.05,height=30,width=80)
quancheng_1.place(relx=0.05,rely=0.2,relheight=0.12,relwidth=0.2)
quancheng_2.place(relx=0.05,rely=0.33,relheight=0.12,relwidth=0.2)
quancheng_3.place(relx=0.05,rely=0.46,relheight=0.12,relwidth=0.2)
quancheng_4.place(relx=0.05,rely=0.59,relheight=0.12,relwidth=0.2)
quancheng_5.place(relx=0.05,rely=0.72,relheight=0.12,relwidth=0.2)
qctext_1.place(relx=0.3,rely=0.2,relheight=0.12,relwidth=0.5)
qctext_2.place(relx=0.3,rely=0.33,relheight=0.12,relwidth=0.5)
qctext_3.place(relx=0.3,rely=0.46,relheight=0.12,relwidth=0.5)
qctext_4.place(relx=0.3,rely=0.59,relheight=0.12,relwidth=0.5)
qctext_5.place(relx=0.3,rely=0.72,relheight=0.12,relwidth=0.5)
frame2.place(relx=0.25,rely=0.1,relheight=0.8,relwidth=0.2)
fenzu_A.place(relx=0.3,rely=0.05,height=30,width=80)
zu_1.place(relx=0.05,rely=0.2,relheight=0.12,relwidth=0.2)
zu_2.place(relx=0.05,rely=0.33,relheight=0.12,relwidth=0.2)
zu_3.place(relx=0.05,rely=0.46,relheight=0.12,relwidth=0.2)
zu_4.place(relx=0.05,rely=0.59,relheight=0.12,relwidth=0.2)
zu_5.place(relx=0.05,rely=0.72,relheight=0.12,relwidth=0.2)
zu_6.place(relx=0.05,rely=0.85,relheight=0.12,relwidth=0.2)
zutext_1.place(relx=0.3,rely=0.2,relheight=0.12,relwidth=0.5)
zutext_2.place(relx=0.3,rely=0.33,relheight=0.12,relwidth=0.5)
zutext_3.place(relx=0.3,rely=0.46,relheight=0.12,relwidth=0.5)
zutext_4.place(relx=0.3,rely=0.59,relheight=0.12,relwidth=0.5)
zutext_5.place(relx=0.3,rely=0.72,relheight=0.12,relwidth=0.5)
zutext_6.place(relx=0.3,rely=0.85,relheight=0.12,relwidth=0.5)
frame3.place(relx=0.5,rely=0.1,relheight=0.8,relwidth=0.2)
fenzu_B.place(relx=0.3,rely=0.05,height=30,width=80)
zu_7.place(relx=0.05,rely=0.2,relheight=0.12,relwidth=0.2)
zu_8.place(relx=0.05,rely=0.33,relheight=0.12,relwidth=0.2)
zu_9.place(relx=0.05,rely=0.46,relheight=0.12,relwidth=0.2)
zu_10.place(relx=0.05,rely=0.59,relheight=0.12,relwidth=0.2)
zu_11.place(relx=0.05,rely=0.72,relheight=0.12,relwidth=0.2)
zutext_7.place(relx=0.3,rely=0.2,relheight=0.12,relwidth=0.5)
zutext_8.place(relx=0.3,rely=0.33,relheight=0.12,relwidth=0.5)
zutext_9.place(relx=0.3,rely=0.46,relheight=0.12,relwidth=0.5)
zutext_10.place(relx=0.3,rely=0.59,relheight=0.12,relwidth=0.5)
zutext_11.place(relx=0.3,rely=0.72,relheight=0.12,relwidth=0.5)
frame4.place(relx=0.75,rely=0.1,relheight=0.8,relwidth=0.2)
fenzu_C.place(relx=0.3,rely=0.05,height=30,width=80)
zu_12.place(relx=0.05,rely=0.2,relheight=0.12,relwidth=0.2)
zu_13.place(relx=0.05,rely=0.33,relheight=0.12,relwidth=0.2)
zu_14.place(relx=0.05,rely=0.46,relheight=0.12,relwidth=0.2)
zu_15.place(relx=0.05,rely=0.59,relheight=0.12,relwidth=0.2)
zutext_12.place(relx=0.3,rely=0.2,relheight=0.12,relwidth=0.5)
zutext_13.place(relx=0.3,rely=0.33,relheight=0.12,relwidth=0.5)
zutext_14.place(relx=0.3,rely=0.46,relheight=0.12,relwidth=0.5)
zutext_15.place(relx=0.3,rely=0.59,relheight=0.12,relwidth=0.5)
def clear():
qctext_1.delete('1.0',END)
qctext_2.delete('1.0',END)
qctext_3.delete('1.0',END)
qctext_4.delete('1.0',END)
qctext_5.delete('1.0',END)
zutext_1.delete('1.0',END)
zutext_2.delete('1.0',END)
zutext_3.delete('1.0',END)
zutext_4.delete('1.0',END)
zutext_5.delete('1.0',END)
zutext_6.delete('1.0',END)
zutext_7.delete('1.0',END)
zutext_8.delete('1.0',END)
zutext_9.delete('1.0',END)
zutext_10.delete('1.0',END)
zutext_11.delete('1.0',END)
zutext_12.delete('1.0',END)
zutext_13.delete('1.0',END)
zutext_14.delete('1.0',END)
zutext_15.delete('1.0',END)
quancheng_list=[[qctext_1,0],[qctext_2,0],[qctext_3,0],[qctext_4,0],[qctext_5,0]]
zutext_list=[[zutext_1,0],[zutext_2,0],[zutext_3,0],[zutext_4,0],[zutext_5,0],[zutext_6,0],[zutext_7,0],
[zutext_8,0],[zutext_9,0],[zutext_10,0],[zutext_11,0],[zutext_12,0],[zutext_13,0],[zutext_14,0],
[zutext_15,0]]
def contra(data):
if data[0] == 'q':
if len(quancheng_list)==0:
print('全程分组已满')
else:
z=random.choice(quancheng_list)
z[0].insert(INSERT, data[1]+' ')
z[1]+=1
if z[1]>5:
quancheng_list.remove(z)
elif data[0] == 'b':
if len(zutext_list)==0:
print('半程分组已满')
else:
z=random.choice(zutext_list)
z[0].insert(INSERT, data[1]+' ')
z[1]+=1
if z[1]>5:
zutext_list.remove(z)
elif data[0] == 'clear':
clear()
else:
print('提交数据有误')
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = '*.*.*.*' #替换成服务器的IP地址
port = 9999
server.bind((host, port))
server.listen()
def handle_sock():
print('服务器开始运行。。。')
sock, addr = server.accept()
while True:
print('连接地址是:',addr)
data = sock.recv(1024)
data = data.decode("utf8")
if data:
print('接收到信息:',data)
re_data = 'OK,你发送的信息已收到' #回复信息
sock.send(re_data.encode("utf8"))
data_list=data.split(",")
contra(data_list) #执行操作命令
break
sock.close()
root.after(1000, handle_sock)
root.after(1000, handle_sock)
root.mainloop()
【运行效果】
在连接LED大屏幕的电脑上启动server.py,大屏幕上界面如下:
然后在签到电脑上启动client.py,界面如下
开始使用, 比如我们在文本框中填入 欧阳锋,选择全程,点击提交,然后就会在大屏幕上看到:
继续,填郭靖,选半程,提交:
OK,实测可用