Python|名片管理系统

以下是名片管理系统,前几天学习了tkinter,这个是本次期末考试的作业。代码写得不是美观,有很多缺陷,比如在循环列表里的字典,判断有还是没有时会弹出多个窗口。。。。希望有路过的大神,学长们多多指点。

from tkinter import Tk, messagebox,Toplevel
import tkinter as tk

info_man = [
    {
        '姓名': '张三',
        'ID': '001',
        '地址': '福建省',
        '职位': '工程师',
        '手机号码': '18850006123',
        '邮箱': '1234568909@qq.com'
    },
    {
        '姓名': '李四',
        'ID': '002',
        '地址': '四川省',
        '职位': '设计师',
        '手机号码': '12321312321',
        '邮箱': '213124342@qq.com'
    }
]

window = Tk()
window.title('【名片管理系统】V 1.0')
screen_width, screen_height = window.maxsize()

# 窗体的宽度、高度
width = 600
height = 350
align_str = '%dx%d+%d+%d' % (width, height, (screen_width-width)/2, (screen_height-height)/2)
window.geometry(align_str)
window.resizable(width=False, height=False)
tk.Label(window, text='欢迎进入【名片管理系统】V 1.0', font=('FangSong', 18)).place(width=580, height=50)

添加名片
def add_info():
    top = Toplevel()
    top.geometry('400x350')
    tk.Label(top, text='姓名:', width=10, height=2, font=('FangSong', 12)).grid(row=5, column=5)
    user_name = tk.StringVar()
    user_name.set('输入姓名')
    tk.Entry(top, textvariable=user_name, width=20, font=('FangSong', 12)).grid(row=5, column=10)

    tk.Label(top, text='ID:', width=10, height=2, font=('FangSong', 12)).grid(row=6, column=5)
    user_id = tk.StringVar()
    user_id.set('输入ID')
    tk.Entry(top, textvariable=user_id, width=20, font=('FangSong', 12)).grid(row=6, column=10)

    tk.Label(top, text='手机号码:', width=10, height=2, font=('FangSong', 12)).grid(row=7, column=5)
    user_phone = tk.StringVar()
    user_phone.set('输入手机号码')
    tk.Entry(top, textvariable=user_phone, width=20, font=('FangSong', 12)).grid(row=7, column=10)

    tk.Label(top, text='邮箱:', width=10, height=2, font=('FangSong', 12)).grid(row=8, column=5)
    user_email = tk.StringVar()
    user_email.set('输入邮箱')
    tk.Entry(top, textvariable=user_email, width=20, font=('FangSong', 12)).grid(row=8, column=10)

    tk.Label(top, text='工作单位:', width=10, height=2, font=('FangSong', 12)).grid(row=9, column=5)
    user_job = tk.StringVar()
    user_job.set('输入工作单位')
    tk.Entry(top, textvariable=user_job, width=20, font=('FangSong', 12)).grid(row=9, column=10)

    tk.Label(top, text='家庭住址:', width=10, height=2, font=('FangSong', 12)).grid(row=10, column=5)
    user_addr = tk.StringVar()
    user_addr.set('输入家庭住址')
    tk.Entry(top, textvariable=user_addr, width=20, font=('FangSong', 12)).grid(row=10, column=10)

    def confirm_info():
        top.destroy()
    tk.Button(top, text='确认', font=('FangSong', 14), command=confirm_info).place(x=170, y=250)
    data = {
        '姓名': user_name.get(),
        'ID': user_id.get(),
        '手机号码': user_phone.get(),
        '邮箱': user_email.get(),
        '职位': user_job.get(),
        '地址': user_addr.get(),

    }
    info_man.append(data)
add_button = tk.Button(window, text='添加名片', font=('FangSong', 14), command=add_info).place(x=150, y=100)
修改名片
def modify_info():
    top = Toplevel()
    top.geometry('400x350')

    tk.Label(top, text='欢迎进入【名片管理系统】V 1.0 修改名片!', width=40, font=('FangSong', 14)).grid(row=4, column=10)
    tk.Label(top, text='请输入要查询的ID:', width=18, font=('FangSong', 14)).place(x=105, y=90)
    user_id = tk.StringVar()
    user_id.set('输入id')
    tk.Entry(top, textvariable=user_id, width=20, font=('FangSong', 14)).place(x=100, y=130)

    def confirm_info():
        for info_dict in info_man:
            if user_id.get() == info_dict['ID']:
                top.destroy()
                def second_modify_info():
                    top_inner = Toplevel()
                    top_inner.geometry('400x300')
                    tk.Label(top_inner, text='请选择修改以下信息:', width=20, font=('FangSong', 14)).grid(row=4, column=10)
                    tk.Label(top_inner, text='姓名:', width=10, height=2, font=('FangSong', 12)).grid(row=5, column=5)
                    user_name = tk.StringVar()
                    user_name.set('输入姓名')
                    tk.Entry(top_inner, textvariable=user_name, width=20, font=('FangSong', 12)).grid(row=5, column=10)

                    tk.Label(top_inner, text='ID:', width=10, height=2, font=('FangSong', 12)).grid(row=6, column=5)
                    user_id = tk.StringVar()
                    user_id.set('输入ID')
                    tk.Entry(top_inner, textvariable=user_id, width=20, font=('FangSong', 12)).grid(row=6, column=10)

                    tk.Label(top_inner, text='手机号码:', width=10, height=2, font=('FangSong', 12)).grid(row=7, column=5)
                    user_phone = tk.StringVar()
                    user_phone.set('输入手机号码')
                    tk.Entry(top_inner, textvariable=user_phone, width=20, font=('FangSong', 12)).grid(row=7, column=10)

                    tk.Label(top_inner, text='邮箱:', width=10, height=2, font=('FangSong', 12)).grid(row=8, column=5)
                    user_email = tk.StringVar()
                    user_email.set('输入邮箱')
                    tk.Entry(top_inner, textvariable=user_email, width=20, font=('FangSong', 12)).grid(row=8, column=10)

                    tk.Label(top_inner, text='工作单位:', width=10, height=2, font=('FangSong', 12)).grid(row=9, column=5)
                    user_job = tk.StringVar()
                    user_job.set('输入工作单位')
                    tk.Entry(top_inner, textvariable=user_job, width=20, font=('FangSong', 12)).grid(row=9, column=10)

                    tk.Label(top_inner, text='家庭住址:', width=10, height=2, font=('FangSong', 12)).grid(row=10, column=5)
                    user_addr = tk.StringVar()
                    user_addr.set('输入家庭住址')
                    tk.Entry(top_inner, textvariable=user_addr, width=20, font=('FangSong', 12)).grid(row=10, column=10)

                    def add_info():
                        top_inner.destroy()
                        data = {
                            '姓名': user_name.get(),
                            'ID': user_id.get(),
                            '手机号码': user_phone.get(),
                            '邮箱': user_email.get(),
                            '职位': user_job.get(),
                            '地址': user_addr.get(),

                        }
                        info_dict.update(data)
                    tk.Button(top_inner, text='确认', font=('FangSong', 14), command=add_info).place(x=150, y=250)
                second_modify_info()

    tk.Button(top, text='确认', font=('FangSong', 14), command=confirm_info).place(x=150, y=175)
modify_button = tk.Button(window, text='修改名片', font=('FangSong', 14), command=modify_info).place(x=150,y=170)
删除名片
def del_info():
    top = Toplevel()
    top.geometry('400x300')

    tk.Label(top, text='欢迎进入【名片管理系统】V 1.0 删除名片!', width=40, font=('FangSong', 14)).grid(row=4, column=10)
    tk.Label(top, text='请输入要删除的ID:', width=18, font=('FangSong', 14)).place(x=105, y=90)
    user_id = tk.StringVar()
    user_id.set('输入id')
    tk.Entry(top, textvariable=user_id, width=20, font=('FangSong', 14)).place(x=100, y=130)

    def confirm_info():
        for info_dict in info_man:
            if user_id.get() == info_dict['ID']:
                info_man.remove(info_dict)
                top_inner = Toplevel()
                top_inner.geometry('200x150')
                def close():
                    top_inner.destroy()
                    messagebox.showinfo('title=提示', message='删除成功!')
                tk.Button(top_inner, text='确认', font=('FangSong', 14), command=close).place(x=70, y=90)

        top.destroy()
    tk.Button(top, text='确认', font=('FangSong', 14), command=confirm_info).place(x=170, y=180)
del_button = tk.Button(window, text='删除名片', font=('FangSong', 14), command=del_info).place(x=150,y=240)

查看所有
def check_info():
    top = Toplevel()
    top.geometry('400x300')

    tk.Label(top, text='欢迎进入【名片管理系统】V 1.0 查询名片!', width=40, font=('FangSong', 14)).grid(row=4, column=10)
    tk.Label(top, text='请输入要查询的ID:', width=18, font=('FangSong', 14)).place(x=105, y=90)
    user_id = tk.StringVar()
    user_id.set('输入id')
    tk.Entry(top, textvariable=user_id, width=20, font=('FangSong', 14)).place(x=100, y=130)

    def confirm_info():
        top_inner = Toplevel()
        top_inner.geometry('400x300')
        for info_dict in info_man:
            if user_id.get() == info_dict['ID']:
                tk.Label(top_inner, text='以下是%s的名片信息:' % info_dict['姓名'], font=('FangSong',18)).place(x=100, y=10)
                tk.Label(top_inner, text='姓名:' + info_dict['姓名'], font=('FangSong', 14)).place(x=50, y=50)
                tk.Label(top_inner, text='ID:' + info_dict['ID'], font=('FangSong', 14)).place(x=50, y=90)
                tk.Label(top_inner, text='手机号码:' + info_dict['手机号码'], font=('FangSong', 14)).place(x=50, y=130)
                tk.Label(top_inner, text='邮箱:' + info_dict['邮箱'], font=('FangSong', 14)).place(x=50, y=170)
                tk.Label(top_inner, text='职位:' + info_dict['职位'], font=('FangSong', 14)).place(x=50, y=210)
                tk.Label(top_inner, text='地址:' + info_dict['地址'], font=('FangSong', 14)).place(x=50, y=250)

    tk.Button(top, text='确认', font=('FangSong', 14), command=confirm_info).place(x=170, y=180)
check_button = tk.Button(window, text='查询名片', font=('FangSong', 14), command=check_info).place(x=320,y=100)

查看所有名片
def show_info():
    top = Toplevel()
    top.geometry('450x550')
    tk.Label(top, text='查看所有名片', font=('FangSong', 20)).place(x=120, y=10)
    for info_dict in info_man:
        tk.Label(top, text='******以下是%s的名片信息:*******' % info_dict['姓名'], font=('FangSong', 18)).place(x=20, y=50)
        tk.Label(top, text='姓名:' + info_dict['姓名'], font=('FangSong', 14)).place(x=50, y=90)
        tk.Label(top, text='ID:' + info_dict['ID'], font=('FangSong', 14)).place(x=50, y=130)
        tk.Label(top, text='手机号码:' + info_dict['手机号码'], font=('FangSong', 14)).place(x=50, y=170)
        tk.Label(top, text='邮箱:' + info_dict['邮箱'], font=('FangSong', 14)).place(x=50, y=210)
        tk.Label(top, text='职位:' + info_dict['职位'], font=('FangSong', 14)).place(x=50, y=250)
        tk.Label(top, text='地址:' + info_dict['地址'], font=('FangSong', 14)).place(x=50, y=290)
show_button = tk.Button(window, text='查看所有', font=('FangSong', 14), command=show_info).place(x=320, y=170)

退出
def exit():
    top = Toplevel()
    top.geometry('300x250')

    def confirm_exit():
        top.destroy()
        window.destroy()

    def return_sys():
        top.destroy()
    tk.Label(top, text='是否退出系统!', font=('FangSong', 14)).place(x=90, y=60)
    tk.Button(top, text='退出', font=('FangSong', 14), command=confirm_exit).place(x=200, y=160)
    tk.Button(top, text='取消', font=('FangSong', 14), command=return_sys).place(x=60, y=160)
exit_button = tk.Button(window, text='退出系统', font=('FangSong', 14), command=exit).place(x=320, y=240)


window.mainloop()

运行效果

主界面

在这里插入图片描述

添加信息

在这里插入图片描述

查询

在这里插入图片描述
在这里插入图片描述

修改名片

在这里插入图片描述

查看所有

在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jxiepc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值