以下是名片管理系统,前几天学习了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()
运行效果
主界面
添加信息
查询