python tkinter GUI案例----新生报到采集程序

import tkinter
import tkinter.messagebox
import tkinter.ttk

# 创建tkinter应用程序
root = tkinter.Tk()

# 设置窗口标题
root.title('新生报到采集程序')
# 定义窗口初始大小
root['height'] = 450
root['width'] = 350

# 在窗口上创建标签组件
labelName = tkinter.Label(root,
                          text='姓名:',
                          justify=tkinter.RIGHT,
                          width=50)
labelName.place(x=10, y=5, width=50, height=20)

# 创建文本框,同时设置关联的变量
varName = tkinter.StringVar(root, value='')
entryName = tkinter.Entry(root,
                          width=120,
                          textvariable=varName)
entryName.place(x=70, y=5, width=120, height=20)

# 在窗口上创建标签组件
labelloc = tkinter.Label(root,
                          text='籍贯:',
                          justify=tkinter.RIGHT,
                          width=50)
labelloc.place(x=10, y=40, width=50, height=20)

# 创建文本框,同时设置关联的变量
varloc = tkinter.StringVar(root, value='')
entryloc = tkinter.Entry(root,
                          width=120,
                          textvariable=varloc)
entryloc.place(x=70, y=40, width=120, height=20)


labelGrade = tkinter.Label(root,
                           text='专业:',
                           justify=tkinter.RIGHT,
                           width=50)
labelGrade.place(x=10, y=80, width=50, height=20)

# 模拟学生所在年级,字典键为年级,字典值为班级
studentClasses = {'数据科学与大数据技术':['1班', '2班', '3班', '4班'],
                  '生物医学工程':['1班', '2班', '3班', '4班'],
                  '临床工程技术':['1班', '2班', '3班', '4班'],
                  '医疗产品管理':['1班', '2班', '3班', '4班']}
# 学生年级组合框
comboGrade = tkinter.ttk.Combobox(root,width=50,
                                  values=tuple(studentClasses.keys()))
comboGrade.place(x=70, y=80, width=130, height=20)
# 年级组合框的事件处理函数
def comboChange(event):
    # 获取年级组合框的当前选择项
    grade = comboGrade.get()
    if grade:
        # 动态改变班级组合框的可选项
        comboClass["values"] = studentClasses.get(grade)
    else:
        comboClass["values"] = []
# 绑定组合框事件处理函数
comboGrade.bind('<<ComboboxSelected>>', comboChange)

labelClass = tkinter.Label(root,
                           text='班级:',
                           justify=tkinter.RIGHT,
                           width=50)
labelClass.place(x=200, y=80, width=50, height=20)

# 学生班级组合框
comboClass = tkinter.ttk.Combobox(root, width=50)
comboClass.place(x=240, y=80, width=50, height=20)


# 在窗口上创建标签组件
labelNum = tkinter.Label(root,
                          text='学号:',
                          justify=tkinter.RIGHT,
                          width=50)
labelNum.place(x=10, y=115, width=50, height=20)

# 创建文本框,同时设置关联的变量
varNum = tkinter.StringVar(root, value='')
entryNum = tkinter.Entry(root,
                          width=120,
                          textvariable=varNum)
entryNum.place(x=70, y=115, width=120, height=20)

labelSex = tkinter.Label(root,
                         text='性别:',
                         justify=tkinter.RIGHT,
                         width=50)
labelSex.place(x=10, y=145, width=50, height=20)

# 与性别单选钮关联的变量,1:男;0:女,默认为男
sex = tkinter.IntVar(root, value=1)
# 单选钮,男
radioMan = tkinter.Radiobutton(root,
                               variable=sex,
                               value=1,
                               text='男')
radioMan.place(x=70, y=145, width=50, height=20)

# 单选钮,女
radioWoman = tkinter.Radiobutton(root,
                                 variable=sex,
                                 value=0,
                                 text='女')
radioWoman.place(x=130, y=145, width=70, height=20)

# 与是否班长复选框关联的变量,默认当前学生不是班长
monitor = tkinter.IntVar(root, value=0)
#复选框,选中时变量值为1,#未选中时变量值为0
checkMonitor = tkinter.Checkbutton(root,
                                   text='是否为班长?',
                                   variable=monitor,
                                   onvalue=1,
                                   offvalue=0)
checkMonitor.place(x=20, y=175, width=100, height=20)

# 添加按钮单击事件处理函数
def addInformation():
    name = entryName.get()
    loc = entryloc.get()
    grade = comboGrade.get()
    classSelected = comboClass.get()
    num = entryNum.get()
    if not (name.strip() and grade and classSelected):
        tkinter.messagebox.showerror('拒绝添加',
                                     '信息不完整,请检查')
        return
    
    result = 'Name:' + name
   
    result = result + ';Location:' + loc
    result = result + ';Grade:' + grade
    result = result + ';Class:' + classSelected
    result = result + ';Sex:' + ('Man' if sex.get() else 'Woman')
    result = result + ';Num:' + num
    result = result + ';Monitor:' + ('Yes' if monitor.get() else 'No')
    
    
    
    # 将信息插入至列表框最上方
    listboxStudents.insert(0, result)
    # 清空姓名文本框
    varName.set('')
buttonAdd = tkinter.Button(root,
                           text='添加',
                           width=40,
                           command=addInformation)
buttonAdd.place(x=130, y=175, width=40, height=20)

# 删除按钮的事件处理函数
def deleteSelection():
    # 获取列表框当前选择项
    selection = listboxStudents.curselection()
    if  not selection:
        tkinter.messagebox.showinfo(title='Information',
                                    message='No Selection')
    else:
        listboxStudents.delete(selection)
buttonDelete = tkinter.Button(root,
                              text='删除已选',
                              width=100,
                              command=deleteSelection)
buttonDelete.place(x=180, y=175, width=100, height=20)

# 创建列表框组件
listboxStudents = tkinter.Listbox(root)
listboxStudents.place(x=10, y=205, width=310, height=230)

sb = tkinter.Scrollbar(listboxStudents,command=listboxStudents.yview)
sb.pack(side="right", fill="y") 
listboxStudents.config(yscrollcommand=sb.set)

root.mainloop()

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

weixin_44322234

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

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

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

打赏作者

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

抵扣说明:

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

余额充值