在使用text文本的通讯录的基础之上进行了改进 使用GUI图形界面 之前做了一个作业,后面帮别人搞了一个作业,为了跟自己不一样稍微排版了一下之类的 简化了一下代码之类的 上篇的通讯录PythonPython 实例:)手机通讯录(使用text文本记录数据 下面是运行截图: 复制粘贴运行就ok了 贴代码:
import tkinter.messagebox
import json
import os
import tkinter
root = tkinter.Tk()
root.title('通讯录')
root.geometry("550x550")
def showinfo():
Frame_info = tkinter.Frame(root, height=150, width=180)
Frame_info.place(x=40, y=180)
name_label = tkinter.Label(Frame_info, text="名字", font=('宋体', 18))
name_label.grid(row=0, column=0)
phone_label = tkinter.Label(Frame_info, text="手机号", font=('宋体', 18))
phone_label.grid(row=0, column=1)
mail_label = tkinter.Label(Frame_info, text="邮箱", font=('宋体', 18))
mail_label.grid(row=0, column=2)
address_label = tkinter.Label(Frame_info, text="地址", font=('宋体', 18))
address_label.grid(row=0, column=3)
file = open("通讯录.txt", mode='r', encoding='utf-8')
if len(file.read()) != 0:
file.seek(0, 0)
file_data = file.read()
split_info = file_data.split('\n')
split_info.remove(split_info[len(split_info) - 1])
name_li = []
all_info_li = []
for i in split_info:
dict_info = json.loads(i.replace("\'", '\"'))
all_info_li.append(dict_info)
row_count = 0
for person_info in all_info_li:
row_count += 1
column_count = 0
for title, info_value in person_info.items():
tktest = tkinter.Label(Frame_info, text=info_value, font=('宋体', 15, 'bold'))
tktest.grid(row=row_count, column=column_count)
column_count += 1
row_count += 1
tktest = tkinter.Label(Frame_info, text=" "*20, font=('宋体', 15, 'bold'))
tktest.grid(row=row_count, column=column_count)
showinfo()
def add_def(event):
def add_man(event):
name = name_new.get()
phone = phone_new.get()
mail = mail_new.get()
address = adr_new.get()
if name == "" or phone == " " or mail == " " or address == " ":
tkinter.messagebox.showerror('错误', '所填信息都不能为空')
else:
card_dict = {"姓名": name, "手机号": phone,
"邮箱": mail, "地址": address}
f = open("通讯录.txt", mode='a+', encoding='utf-8')
f.write(str(card_dict) + '\n')
f.close()
tkinter.messagebox.showinfo('消息提示框', f'添加“{name}“为联系人成功!')
showinfo()
window_add.destroy()
window_add = tkinter.Toplevel(root)
window_add.geometry('300x250')
name_new = tkinter.StringVar()
tkinter.Label(window_add, text='新联系人姓 名:').place(x=10, y=10)
tkinter.Entry(window_add, textvariable=name_new).place(x=110, y=10)
phone_new = tkinter.StringVar()
tkinter.Label(window_add, text='新联系人手机号:').place(x=10, y=50)
tkinter.Entry(window_add, textvariable=phone_new).place(x=110, y=50)
mail_new = tkinter.StringVar()
tkinter.Label(window_add, text='新联系人邮 箱:').place(x=10, y=90)
tkinter.Entry(window_add, textvariable=mail_new).place(x=110, y=90)
adr_new = tkinter.StringVar()
tkinter.Label(window_add, text='新联系人地 址:').place(x=10, y=130)
tkinter.Entry(window_add, textvariable=adr_new).place(x=110, y=130)
confirm_button = tkinter.Button(window_add, text='确认添加', font=('宋体', 15))
confirm_button.bind("<Button-1>", add_man)
confirm_button.place(x=100, y=160)
def del_def(event):
name = man_name.get()
file = open("通讯录.txt", mode='r+', encoding='utf-8')
if len(file.read()) != 0:
file.seek(0, 0)
file_data = file.read()
split_info = file_data.split('\n')
split_info.remove(split_info[len(split_info) - 1])
name_li = []
all_info_li = []
for i in split_info:
dict_info = json.loads(i.replace("\'", '\"'))
all_info_li.append(dict_info)
name_li.append(dict_info['姓名'])
if name in name_li:
通讯录_copy = open('通讯录_copy.txt', mode='w', encoding="utf-8")
for person_info in all_info_li:
if name not in str(person_info):
通讯录_copy.write(str(person_info) + '\n')
通讯录_copy.close()
file.close()
os.rename('通讯录.txt', '通讯录_del.txt')
os.rename('通讯录_copy.txt', '通讯录.txt')
os.remove('通讯录_del.txt')
tkinter.messagebox.showinfo('消息提示', f'删除“{name}“成功!')
showinfo()
else:
tkinter.messagebox.showinfo('消息提示', '查无此人!')
def update_def(event):
def update_man(event):
name = name_new.get()
phone = phone_new.get()
mail = mail_new.get()
address = address_new.get()
if name != "" and phone != "" and mail != "" and address != "":
通讯录_copy = open('通讯录_copy.txt', mode='w', encoding="utf-8")
card_dict = {"姓名": name, "手机号": phone,
"邮箱": mail, "地址": address}
for person_info in all_info_li:
if name_old in str(person_info):
person_info = str(card_dict)
通讯录_copy.write(str(person_info) + '\n')
通讯录_copy.close()
file.close()
os.rename('通讯录.txt', '通讯录_del.txt')
os.rename('通讯录_copy.txt', '通讯录.txt')
os.remove('通讯录_del.txt')
tkinter.messagebox.showinfo('消息提示框', '更新成功!')
showinfo()
else:
tkinter.messagebox.showinfo('消息提示框', '请输入正确信息,或输入完整信息')
name_old = man_name.get()
file = open("通讯录.txt", mode='r+', encoding='utf-8')
if len(file.read()) != 0:
file.seek(0, 0)
file_data = file.read()
split_info = file_data.split('\n')
split_info.remove(split_info[len(split_info) - 1])
name_li = []
all_info_li = []
for i in split_info:
dict_info = json.loads(i.replace("\'", '\"'))
all_info_li.append(dict_info)
name_li.append(dict_info['姓名'])
if name_old in name_li:
window_update = tkinter.Toplevel(root)
window_update.geometry('280x200')
name_new = tkinter.StringVar()
phone_new = tkinter.StringVar()
mail_new = tkinter.StringVar()
address_new = tkinter.StringVar()
tkinter.Label(window_update, text='姓 名:').place(x=20, y=0)
tkinter.Entry(window_update, textvariable=name_new).place(x=80, y=0)
tkinter.Label(window_update, text='手机号:').place(x=20, y=30)
tkinter.Entry(window_update, textvariable=phone_new).place(x=80, y=30)
tkinter.Label(window_update, text='邮 箱:').place(x=20, y=60)
tkinter.Entry(window_update, textvariable=mail_new).place(x=80, y=60)
tkinter.Label(window_update, text='地 址:').place(x=20, y=90)
tkinter.Entry(window_update, textvariable=address_new).place(x=80, y=90)
confirm_button = tkinter.Button(window_update, text='确认修改', font=('宋体', 15))
confirm_button.bind("<Button-1>", update_man)
confirm_button.place(x=100, y=155)
else:
tkinter.messagebox.showinfo('消息提示', '通讯录中查无此人')
def find_def(event):
name = man_name.get()
file = open("通讯录.txt", mode='r+', encoding='utf-8')
if len(file.read()) != 0:
file.seek(0, 0)
file_data = file.read()
split_info = file_data.split('\n')
split_info.remove(split_info[len(split_info) - 1])
name_li = []
all_info_li = []
for i in split_info:
dict_info = json.loads(i.replace("\'", '\"'))
all_info_li.append(dict_info)
name_li.append(dict_info['姓名'])
if name in name_li:
man_find = tkinter.Toplevel(root)
man_find.geometry('300x120')
for person_info in all_info_li:
if name in str(person_info):
for title, info_value in person_info.items():
tem_text = title + ":" + info_value + " " *10
tktest = tkinter.Label(man_find, text=tem_text, font=('宋体', 15))
tktest.pack(side="top", anchor='w')
else:
tkinter.messagebox.showinfo('消息提示', '通讯录中查无此人')
def over_def(event):
root.destroy()
man_nametitle = tkinter.Label(root,text="请输入联系人姓名:", font=('宋体', 10)).place(x=10,y=10)
man_name = tkinter.StringVar()
tkinter.Entry(root, textvariable=man_name, width=40).place(x=135, y=10)
man_nametitle = tkinter.Label(root,text="联系人名单列表", font=('宋体', 20, 'bold')).place(x=160,y=130)
man_nametitle = tkinter.Label(root,text="-"*150,).place(x=0,y=160)
button1 = tkinter.Button(root, text="添加联系人", width=10, font=('宋体', 15))
button1.place(x=60, y=40)
button1.bind('<Button-1>', add_def)
button3 = tkinter.Button(root, text="删除联系人", width=10, font=('宋体', 15))
button3.place(x=210, y=40)
button3.bind('<Button-1>', del_def)
button4 = tkinter.Button(root, text="修改联系人", width=10, font=('宋体', 15))
button4.place(x=60, y=80)
button4.bind('<Button-1>', update_def)
button5 = tkinter.Button(root, text="查找联系人", width=10, font=('宋体', 15))
button5.place(x=210, y=80)
button5.bind('<Button-1>', find_def)
button6 = tkinter.Button(root, text="退出", width=10, height=3, font=('宋体', 15))
button6.place(x=350, y=40)
button6.bind('<Button-1>', over_def)
root.mainloop()
|