使用Tkinter库设计实现的中小学校疫情防控入校人员登记检测系统

Tkinter简介

Tkinter是Python标准库中用于GUI图形用户界面开发的工具包,它是基于Tcl/Tk的封装,提供了大量预定义的控件,如按钮、文本框、标签等,非常适合快速原型开发和小型应用的构建。本文将通过一个具体的案例——“中小学校疫情防控入校人员登记检测系统”来详细介绍Tkinter的基本使用和一些高级功能。

系统设计目标

本系统旨在帮助中小学校有效管理疫情期间入校人员的健康状况,包括基本信息录入、健康状况检查、异常情况预警及历史访问记录查询等功能。系统采用Tkinter进行界面设计,实现直观的用户交互。

界面布局与控件使用
  1. 主窗口配置
    使用root.geometry()设置窗口尺寸和位置,确保窗口居中显示。通过root.title()设定窗口标题,root.resizable()防止用户调整窗口大小。

  2. 菜单栏设计
    通过tk.Menu创建菜单栏,使用add_cascade添加下拉菜单,如“校内人员”和“其余人员”,并为其添加子菜单项,如“学生”、“教师”和“校外人员”。每项菜单关联不同的函数,用于动态生成相关表单。

  3. 控件布局

    • 文本标签tk.Label用于显示字段名称。
    • 输入框tk.Entry用于收集用户输入。
    • 单选按钮tk.Radiobutton用于性别、消毒洗手、佩戴口罩和接触感染人员的选择。
    • 下拉框ttk.Combobox用于年级、部门、近期去过的地方和身体异常情况的选择。
  4. 功能实现

    • 条件判断函数data_judge检查体温、接触史、旅行史和健康状态,决定是否允许进入校园。
    • 数据保存函数save将访问者信息和检测结果写入文本文件。
    • 访问历史查询函数select从文件中检索并显示特定访问者的记录。
  5. 显示区域
    一个专用的框架display_frame用于显示访问历史,包含一个标签show_label用于更新显示内容。

效果图

由于这是一个文本格式的博客,无法直接显示图像,但是你可以运行上述代码在本地环境中查看效果。界面设计清晰,布局合理,用户可以直观地填写信息并进行操作。

完整代码
import tkinter as tk
from tkinter import messagebox as msb
from tkinter import ttk
import datetime
# ---------------------------父窗口设计------------------------------
# 初始化窗口
root = tk.Tk()
# 获取屏幕宽度、高度
Screen_width = root.winfo_screenwidth()
Screen_height = root.winfo_screenheight()
# 定义窗口的宽度、高度
Root_width = 900
Root_height = 600
# 定义窗口初始位置的坐标值
x = (Screen_width - Root_width) / 2
y = (Screen_height - Root_height) / 2
# 使窗口位于屏幕中央
root.geometry("%dx%d+%d+%d" % (Root_width, Root_height, x, y))
# 为窗口添加标题
root.title("中小学校疫情防控入校人员登记检测系统")
# 窗口大小设置为不可变
root.resizable(False, False)

# ---------------------------菜单栏设计------------------------------
def others():
    fr_others = tk.Frame(root, height=150, width=310, bd=3)
    fr_others.place(x=450, y=150)
    label8 = tk.Label(fr_others, text='访 校 事 由:', font=('宋体', '12'))
    label8.place(x=10, y=20)
    entry8 = tk.Entry(fr_others)
    entry8.place(x=120, y=20)
    label9 = tk.Label(fr_others, text='访问对象姓名:', font=('宋体', '12'))
    label9.place(x=10, y=80)
    entry9 = tk.Entry(fr_others)
    entry9.place(x=120, y=80)
    return 0

def student():
    # 创建年纪选择组件框
    fr_student = tk.Frame(root,height=150, width=310,bd=3)
    fr_student.place(x=450,y=150)
    grade = tk.StringVar()
    grade_combobox = ttk.Combobox(fr_student, textvariable=grade)
    grade_combobox['value'] = ('一年级', '二年级', '三年级', '四年级', '五年级','六年级',
                               '七年级', '八年级', '九年级', '高一级','高二级', '高三级')
    grade_combobox['state'] = 'readonly'
    grade_combobox.current(0)
    grade_combobox.bind('<>')
    grade_combobox.place(x=100, y=20)
    label_combobox = tk.Label(fr_student, text='年    级:', font=('宋体', '12'))
    label_combobox.place(x=10, y=20)
    label10 = tk.Label(fr_student, text='班    级:', font=('宋体', '12'))
    label10.place(x=10, y=80)
    entry10 = tk.Entry(fr_student)
    entry10.place(x=100, y=80)
    return 0

def teacher():
    # 创建教师职务组件框
    fr_teacher = tk.Frame(root, height=150, width=310, bd=3)
    fr_teacher.place(x=450, y=150)
    techer = tk.StringVar()
    techer_combobox = ttk.Combobox(fr_teacher, textvariable=teacher)
    techer_combobox['value'] = ('办公室', '教务处', '政教处', '后勤处', '财务处','校医室', '其他级组')
    techer_combobox['state'] = 'readonly'
    techer_combobox.current(0)
    techer_combobox.bind('<>')
    techer_combobox.place(x=120, y=20)
    labe_combobox = tk.Label(fr_teacher, text='所属部门:', font=('宋体', '12'))
    labe_combobox.place(x=10, y=20)
    return 0

# 创建一个菜单栏
menubar = tk.Menu(root)
# 定义一个校内人员和校外人员的空菜单单元
in_menu = tk.Menu(menubar, tearoff=0)  # tearoff意为下拉
out_menu = tk.Menu(menubar, tearoff=0)  # tearoff意为下拉
# 为菜单添加一级菜单标签
menubar.add_cascade(label='校内人员', menu=in_menu)
menubar.add_cascade(label='其余人员', menu=out_menu)
# 为“校内人员”添加二级菜单
in_menu.add_command(label='学生',command=student)
in_menu.add_command(label='教师',command=teacher)
root.config(menu=menubar)
# 为“其余人员”添加二级菜单
out_menu.add_command(label='校外人员',command=others)

# ---------------------------窗口控件设计------------------------------
label1 = tk.Label(root,text='姓    名:',font=('宋体','12'))
label1.place(x=30,y=30)
name = tk.StringVar()
entry1 = tk.Entry(root,textvariable=name)
entry1.place(x=130,y=30)

label2 = tk.Label(root,text='性    别:',font=('宋体','12'))
label2.place(x=30,y=80)
# 创建性别单选项
male = tk.StringVar()
def get_radiobut12_value():
    print(male.get())
radiobt1 = tk.Radiobutton(root,text='男',font=('宋体','12'),value="男",variable=male,command=get_radiobut12_value)
radiobt1.place(x=140,y=80)
radiobt2 = tk.Radiobutton(root,text='女',font=('宋体','12'),value="女",variable=male,command=get_radiobut12_value)
radiobt2.place(x=210,y=80)

label3 = tk.Label(root,text='联系方式:',font=('宋体','12'))
label3.place(x=30,y=130)
entry3 = tk.Entry()
entry3.place(x=130,y=130)

label4 = tk.Label(root,text='体    温:',font=('宋体','12'))
label4.place(x=30,y=180)
temp = tk.StringVar()
entry4 = tk.Entry(root,textvariable=temp)
entry4.place(x=130,y=180)
print(temp.get())

# 创建消毒洗手单选项
label5 = tk.Label(root,text='消毒洗手:',font=('宋体','12'))
label5.place(x=30,y=230)
xiaodu = tk.StringVar()
def get_radiobut34_value():
    print(xiaodu.get())
    if xiaodu.get() == '否':
        msb.showwarning(title="提示", message="禁止进入,请您立即在校门口设置的消毒站消毒洗手!")
radiobt3 = tk.Radiobutton(root,text='是',font=('宋体','12'),value="是",variable=xiaodu,command=get_radiobut34_value)
radiobt3.place(x=140,y=230)
radiobt4 = tk.Radiobutton(root,text='否',font=('宋体','12'),value="否",variable=xiaodu,command=get_radiobut34_value)
radiobt4.place(x=210,y=230)

# 创建佩戴口罩单选项
label6 = tk.Label(root,text='佩戴口罩:',font=('宋体','12'))
label6.place(x=30,y=280)
peidai = tk.StringVar()
def get_radiobut56_value():
    print(peidai.get())
    if peidai.get() == '否':
        msb.showwarning(title="提示", message="禁止进入,请在消毒站领取并佩戴口罩!")

radiobt5 = tk.Radiobutton(root,text='是',font=('宋体','12'),value="是",variable=peidai,command=get_radiobut56_value)
radiobt5.place(x=140,y=280)
radiobt6 = tk.Radiobutton(root,text='否',font=('宋体','12'),value="否",variable=peidai,command=get_radiobut56_value)
radiobt6.place(x=210,y=280)

# 创建接触感染人员单选项
label7 = tk.Label(root,text='接触感染人员:',font=('宋体','12'))
label7.place(x=30,y=330)
ganran = tk.StringVar()
def get_radiobut78_value():
    print(ganran.get())
radiobt7 = tk.Radiobutton(root,text='是',font=('宋体','12'),value="是",variable=ganran,command=get_radiobut78_value)
radiobt7.place(x=140,y=330)
radiobt8 = tk.Radiobutton(root,text='否',font=('宋体','12'),value="否",variable=ganran,command=get_radiobut78_value)
radiobt8.place(x=210,y=330)

#创建近期去过那里组件框
place = tk.StringVar()
place_combobox = ttk.Combobox(root,textvariable=place)
place_combobox['value'] = ('无','北京','新疆',"湖北","武汉","广西","国外")
place_combobox['state'] = 'readonly'
place_combobox.current(0)
place_combobox.bind('<>')
place_combobox.place(x=600,y=30)
label_combobox = tk.Label(root,text='近期去过哪里:',font=('宋体','12'))
label_combobox.place(x=450,y=30)

#创建身体异常组件框
body = tk.StringVar()
body_combobox = ttk.Combobox(root,textvariable=body)
body_combobox['value'] = ('正常','发热,乏力','干咳,咽痛','鼻塞,流涕','腹泻')
body_combobox['state'] = 'readonly'
body_combobox.current(0)
body_combobox.bind('<>')
body_combobox.place(x=600,y=100)
label_combobox = tk.Label(root,text='身体异常情况:',font=('宋体',12))
label_combobox.place(x=450,y=100)


# -------------------------------来访人员入校记录区-----------------------------------
display_frame = tk.Frame(root,height=240, width=900,relief=tk.GROOVE,bd=3)
display_frame.pack(side=tk.BOTTOM)
lab2 = tk.Label(display_frame,text='来访人员访问历史查询显示区',font=('黑体','12'))
display_frame.propagate(0)
lab2.pack()
show_label = tk.Label(display_frame,height=240, width=900,relief=tk.GROOVE,bd=5,bg='white')
show_label.pack(side=tk.TOP)

def data_judge():
    a = float(temp.get())
    b = ganran.get()
    c = place.get()
    d = body.get()
    if a >= 37.8 or b == "是" or c != "无" or d != "正常":
        msb.showwarning(title="提示", message="您的数据显示异常,禁止进入学校!")
    else:
        msb.showwarning(title="提示", message="您的数据显示正常,请进入学校,并及时保存数据!")
def save():
    now_time = datetime.datetime.now()
    a = entry1.get()
    b = entry3.get()
    c = float(temp.get())
    d = ganran.get()
    e = place.get()
    f = body.get()
    g = male.get()
    filename = 'information_record.txt'
    with open(filename, 'a') as f1:
        if c >= 37.8 or d == "是" or e != "无" or f != "正常":
            f1.write(a + "," + g + ",于" + str(now_time) + "时进行入校检测,检测结果异常,"+ "其联系方式是:" + b +"\n")
        else:
            f1.write(a + "," + g + ",于" + str(now_time) + "时进行入校检测,检测结果正常,"+ "其联系方式是:" + b +"\n")
    f1.close()

def select():
    name = entry1.get()
    filename = 'information_record.txt'
    with open(filename, 'r') as f2:
        lines = f2.readlines()
        print(lines)
        for line in lines[:len(lines)]:
            str1 = line.rstrip()
            list2 = str1.split(",")
            print(list2)
            if name == list2[0]:
                show_label["text"] = str1
                break
            else:
                show_label["text"] = "此人未有访问记录"



#创建查询,保存按钮
butten1 = tk.Button(root,text='查询',font=('黑体','12'),command=select)
butten1.place(x=300,y=25)
butten2 = tk.Button(root,text='保存信息',font=('黑体','12'),command=save)
butten2.place(x=600,y=300)
butten3 = tk.Button(root,text='条件检测',font=('黑体','12'),command=data_judge)
butten3.place(x=500,y=300)
root.mainloop()

效果图

在这里插入图片描述

结论

Tkinter提供了强大的工具集来构建功能丰富的桌面应用程序。通过本案例,我们不仅了解了Tkinter的基本使用,还学习了如何设计复杂界面,实现数据处理和文件操作。希望这篇教程能够帮助你掌握Tkinter,并鼓励你在自己的项目中尝试使用它。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱写代码的小朋友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值