利用GUI实现简单通讯录

利用python提供的tkinter库实现通讯录

代码如下:
(有挺多小bug,但基本功能能实现,想用的可以自己稍微修改一下。)
思路:用的最基本的方法:就是字典存入列表;每条信息就是字典键值,存入全局变量列表中。

import tkinter
import tkinter.messagebox
import _sqlite3
stuInfos=[]
#添加一个学生信息
def Serchone(leibie):
    i = 1
    for tempInfo in stuInfos:
        if tempInfo['类别'] == leibie:
            return tempInfo
            i += 1

def Del(leibie):
    for stu in stuInfos:                         #遍历列表
        if stu['类别'] == leibie:
            stuInfos.remove(stu)
def change():
    def changeDate():
        n = name.get()
        p = ph.get()
        l = leibie.get()
        if n == '' or p == '':
            tkinter.messagebox.showerror("错误", "信息不能为空!")
            win4.destroy()
        else:
            for stu in stuInfos:  # 遍历列表
                if stu['姓名'] == n:
                    stu['电话'] = p
                    stu['类别'] = l
                    tkinter.messagebox.showinfo("成功", "修改联系人成功")
                else:
                    tkinter.messagebox.showerror("错误", "没找到联系人")
            win4.destroy()
    win4 = tkinter.Toplevel()
    win4.title('修改联系人')
    win4.geometry('500x300')
    sw = win4.winfo_screenwidth()
    sh = win4.winfo_screenheight()
    win4.geometry('+%d+%d' % ((sw - 500) / 2, (sh - 300) / 2))
    # 欢迎语
    l = tkinter.Label(win4, text='欢迎进入修改页面', font=('华文行楷', 20), fg='purple')
    l.place(relx=0.5, rely=0.1, anchor='center')
    # 提示语
    l = tkinter.Label(win4, text='请输入修改人信息', font=('正楷', 15))
    l.place(relx=0.5, rely=0.3, anchor='center')
    # 姓名输入框
    Lname = tkinter.Label(win4, text='修改人姓名:')
    Lname.place(relx=0.1, rely=0.5, anchor='center')
    nu = tkinter.StringVar()
    name = tkinter.Entry(win4, textvariable=nu)
    name.place(relx=0.15, rely=0.47, width=70)
    # 电话号输入框
    Lph = tkinter.Label(win4, text='电话:')
    Lph.place(relx=0.4, rely=0.5, anchor='center')
    nu1 = tkinter.StringVar()
    ph = tkinter.Entry(win4, textvariable=nu1)
    ph.place(relx=0.45, rely=0.47, width=140)
    Lleibie = tkinter.Label(win4, text='类别:')
    Lleibie.place(relx=0.70, rely=0.5, anchor='center')
    nu2 = tkinter.StringVar()
    leibie = tkinter.Entry(win4, textvariable=nu2)
    leibie.place(relx=0.75, rely=0.47, width=100)
    # 按钮
    b = tkinter.Button(win4, text='修改', width=10, height=3, bg='gray', command=changeDate)
    b.place(relx=0, rely=1, anchor='sw')
    b2 = tkinter.Button(win4, text='退出', width=10, height=3, bg='gray', command=win4.destroy)
    b2.place(relx=1, rely=1, anchor='se')

def delete():
    def show():
        li = Serchone(Lleibie.get())
        if len(li) == 0:
            tkinter.messagebox.showerror("错误", "此人不存在")
            win3.destroy()
        pth=li['电话']
        pth2= li['姓名']
        print(pth)
        ph = tkinter.Label(win3, text='%s' % pth)
        ph.place(relx=0.55, rely=0.5, anchor='center')
        ph2 = tkinter.Label(win3, text='%s' % pth2)
        ph2.place(relx=0.85, rely=0.5, anchor='center')

    def delDate():
        Del(Lleibie.get())
        tkinter.messagebox.showerror("成功", "删除成功")
        win3.destroy()

    # 设置窗口位置
    # 不能使用两次Tk()去创建窗体,因为tkinter中只能有一个主线程,
    win3 = tkinter.Toplevel()
    win3.title('删除联系人')
    win3.geometry('500x300')
    sw = win3.winfo_screenwidth()
    sh = win3.winfo_screenheight()
    win3.geometry('+%d+%d' % ((sw - 500) / 2, (sh - 300) / 2))
    # 欢迎语
    l = tkinter.Label(win3, text='欢迎进入删除页面', font=('华文行楷', 20), fg='purple')
    l.place(relx=0.5, rely=0.1, anchor='center')
    # 类别展示框
    Leibie = tkinter.Label(win3, text='类别:')
    Leibie.place(relx=0.2, rely=0.5, anchor='center')
    nu = tkinter.StringVar()
    Lleibie = tkinter.Entry(win3, textvariable=nu)
    Lleibie.place(relx=0.25, rely=0.47, width=70)
    # 电话号展示框
    Lph = tkinter.Label(win3, text='电话:')
    Lph.place(relx=0.50, rely=0.5, anchor='center')
    # 姓名输入框
    name = tkinter.Label(win3, text='名字:')
    name.place(relx=0.80, rely=0.5, anchor='center')

    # 按钮
    b = tkinter.Button(win3, text='查询', width=10, height=3, bg='gray', command=show)
    b.place(relx=0, rely=1, anchor='sw')
    b = tkinter.Button(win3, text='确认删除', width=10, height=3, bg='gray', command=delDate)
    b.place(relx=0.15, rely=1, anchor='sw')
    b2 = tkinter.Button(win3, text='退出', width=10, height=3, bg='gray', command=win3.destroy)
    b2.place(relx=1, rely=1, anchor='se')
def add():
    newInfo = {}
    def addDate():
        n = name.get()
        p = ph.get()
        l = leibie.get()
        if n == '' or p == '':
            tkinter.messagebox.showerror("错误", "信息有不能为空!")
            win1.destroy()
        else:
            newInfo['姓名'] = n
            newInfo['电话'] = p
            newInfo['类别'] = l
            stuInfos.append(newInfo)
            tkinter.messagebox.showinfo("成功", "新增联系人成功")
            win1.destroy()
    win1 = tkinter.Toplevel()
    win1.title('新增联系人')
    win1.geometry('500x300')
    sw = win1.winfo_screenwidth()
    sh = win1.winfo_screenheight()
    win1.geometry('+%d+%d' % ((sw - 500) / 2, (sh - 300) / 2))
    # 欢迎语
    l = tkinter.Label(win1, text='欢迎进入新增页面', font=('华文行楷', 20), fg='purple')
    l.place(relx=0.5, rely=0.1, anchor='center')
    # 提示语
    l = tkinter.Label(win1, text='请输入联系人信息', font=('正楷', 15))
    l.place(relx=0.5, rely=0.3, anchor='center')
    # 姓名输入框
    Lname = tkinter.Label(win1, text='姓名:')
    Lname.place(relx=0.1, rely=0.5, anchor='center')
    nu = tkinter.StringVar()
    name = tkinter.Entry(win1, textvariable=nu)
    name.place(relx=0.15, rely=0.47, width=70)
    # 电话号输入框
    Lph = tkinter.Label(win1, text='电话:')
    Lph.place(relx=0.4, rely=0.5, anchor='center')
    nu1 = tkinter.StringVar()
    ph = tkinter.Entry(win1, textvariable=nu1)
    ph.place(relx=0.45, rely=0.47, width=140)
    Lleibie = tkinter.Label(win1, text='类别:')
    Lleibie.place(relx=0.70, rely=0.5, anchor='center')
    nu2 = tkinter.StringVar()
    leibie = tkinter.Entry(win1, textvariable=nu2)
    leibie.place(relx=0.75, rely=0.47, width=100)
    # 按钮
    b = tkinter.Button(win1, text='添加', width=10, height=3, bg='gray', command=addDate)
    b.place(relx=0, rely=1, anchor='sw')
    b2 = tkinter.Button(win1, text='退出', width=10, height=3, bg='gray', command=win1.destroy)
    b2.place(relx=1, rely=1, anchor='se')


def find():
    # 设置窗口位置
    # 不能使用两次Tk()去创建窗体,因为tkinter中只能有一个主线程,
    win2 = tkinter.Toplevel()
    win2.title('查询联系人')
    win2.geometry('500x300')
    sw = win2.winfo_screenwidth()
    sh = win2.winfo_screenheight()
    win2.geometry('+%d+%d' % ((sw - 600) / 2, (sh - 300) / 2))
    # 欢迎语
    l = tkinter.Label(win2, text='欢迎进入查询页面', font=('华文行楷', 20), fg='purple')
    l.place(relx=0.5, rely=0.1, anchor='center')

    ListB = tkinter.Listbox(win2)
    ListB.place(relx=0.1, rely=0.2, anchor='nw', width=500)
    #for item in li:
    i = 1
    for tempInfo in stuInfos:
        ListB.insert(0, tempInfo)
        i += 1


    # 按钮
    b2 = tkinter.Button(win2, text='退出', width=10, height=3, bg='gray', command=win2.destroy)
    b2.place(relx=1, rely=1, anchor='se')

if __name__ == '__main__':
    def secondMain():
        op = v.get()
        if op == 1:
            add()
        elif op == 2:
            find()
        elif op == 3:
            change()
        elif op == 4:
            delete()


    # 设置窗口位置
    win = tkinter.Tk()
    win.title('我的通讯录')
    win.geometry('500x300')
    sw = win.winfo_screenwidth()
    sh = win.winfo_screenheight()
    win.geometry('+%d+%d' % ((sw - 500) / 2, (sh - 300) / 2))
    # 欢迎语
    l = tkinter.Label(win, text='欢迎进入通讯录', font=('华文行楷', 20), fg='green')
    l.place(relx=0.5, rely=0.1, anchor='center')
    # 单选按钮
    choose = [('1.添加联系人', 1), ('2.查询联系人', 2), ('3.修改联系人', 3), ('4.删除联系人', 4)]
    v = tkinter.IntVar()
    v.set(1)
    x, y = 0.5, 0.2,
    for a, b in choose:
        cc = tkinter.Radiobutton(win, text=a, variable=v, value=b)
        cc.place(relx=x, rely=y, anchor='center')
        y += 0.1
    # 按钮
    b = tkinter.Button(win, text='确定', width=10, height=3, bg='gray', command=secondMain)
    b.place(relx=0, rely=1, anchor='sw')
    b2 = tkinter.Button(win, text='退出', width=10, height=3, bg='gray', command=win.quit)
    b2.place(relx=1, rely=1, anchor='se')

    win.mainloop()

你可能感兴趣的:(pyhon菜鸟案例,tkinter,python)