美文网首页
Python3_tkinter电话本制作

Python3_tkinter电话本制作

作者: f5065e26181b | 来源:发表于2018-09-03 21:30 被阅读74次

初学者一枚,记录一下GUI学习的过程。
一、开发环境
版本:python3.5.2
编程软件:pycharm
配置库:1.pip install tkinter
2.pip install pyintsaller
二、软件界面和对应代码
1.主界面
主界面设计考虑输入电话号码或人名查询时的方便,调用Entry显示按钮或键盘的输入信息,以及输出查询结果。按钮分为数字键,组合数字键如“850”,搜索,回退和清除键。布局模式采用pack。通过iconbitmap()设置左上角图标。效果如图所示。


主界面.PNG

对应代码:

def layout(root):
    #显示窗口
    entry1 = tkinter.Entry(root,  textvariable=vartext,bg='Honeydew')
    # entry1.grid(row=0, columnspan=4)
    entry1.place(relx=0.03, width=210, height=42,rely=0.03)
    entry1.bind('<Key-Return>',display.find)
    #第一行按钮
    button750=tkinter.Button(root,text='850',width=6,bg='PaleGreen',command=display('750').jia)
    button751 = tkinter.Button(root, text='851', width=6,bg='PaleGreen',command=display('751').jia)
    button755 = tkinter.Button(root, text='855', width=6,bg='PaleGreen',command=display('755').jia)
    button0 = tkinter.Button(root, text=' 0 ', width=6,bg='PaleGreen',command=display('0').jia)
    button750.place(relx=0.02, rely=0.27)
    button751.place(relx=0.265, rely=0.27)
    button755.place(relx=0.515, rely=0.27)
    button0.place(relx=0.755, rely=0.27)
    # 第二行按钮
    button1 = tkinter.Button(root, text=' 1 ', width=6,bg='PaleGreen',command=display('1').jia)
    button2 = tkinter.Button(root, text=' 2 ', width=6,bg='PaleGreen',command=display('2').jia)
    button3 = tkinter.Button(root, text=' 3 ', width=6,bg='PaleGreen',command=display('3').jia)
    buttonJ = tkinter.Button(root, text=' ← ', width=6,bg='PaleGreen',command=display(' ').tui)
    button1.place(relx=0.02, rely=0.45)
    button2.place(relx=0.265, rely=0.45)
    button3.place(relx=0.515, rely=0.45)
    buttonJ.place(relx=0.755, rely=0.45)
    # 第三行按钮
    button4 = tkinter.Button(root, text=' 4 ', width=6,bg='PaleGreen',command=display('4').jia)
    button5 = tkinter.Button(root, text=' 5 ', width=6,bg='PaleGreen',command=display('5').jia)
    button6 = tkinter.Button(root, text=' 6 ', width=6,bg='PaleGreen',command=display('6').jia)
    buttonE = tkinter.Button(root, text=' Search ', width=6,bg='PaleGreen',command=display('搜索中').find)
    button4.place(relx=0.02, rely=0.63)
    button5.place(relx=0.265, rely=0.63)
    button6.place(relx=0.515, rely=0.63)
    buttonE.place(relx=0.755, rely=0.63)
    # 第四行按钮
    button7 = tkinter.Button(root, text=' 7 ', width=6,bg='PaleGreen',command=display('7').jia)
    button8 = tkinter.Button(root, text=' 8 ', width=6,bg='PaleGreen',command=display('8').jia)
    button9 = tkinter.Button(root, text=' 9 ', width=6,bg='PaleGreen',command=display('9').jia)
    buttonC = tkinter.Button(root, text=' clear  ', width=6, bg='PaleGreen',command=display('').clear)
    button7.place(relx=0.02, rely=0.81)
    button8.place(relx=0.265, rely=0.81)
    button9.place(relx=0.515, rely=0.81)
    buttonC.place(relx=0.755, rely=0.81)

2.增加用户界面
做的比较简单,输入对应电话和姓名,写入文本。效果如图所示。


增加用户.PNG

对应代码:

def add():
    # 下面定义增加信息文件操作
    def addData():
        if v1.get() == '' or v2.get() == '' :
            messagebox.showerror("Q电话本", "信息不全!")
        else:
            if messagebox.askokcancel("Q电话本", "您确认增加该联系人吗?") is True:
                # 下面是进行增加信息文件操作
                with open("record.txt", "a") as f1:
                    f1.write("\n")
                    f1.write(v1.get())
                    f1.write(" ")
                    f1.write(v2.get())
    # 下面是增加信息操作的界面化
    # 创建一个顶级容器
    top1 = Toplevel()
    top1.title("增加用户")
    top1.iconbitmap('my48.ico')
    top1.attributes("-alpha", 0.9)  # 窗口透明度10 %
    addPhoto = PhotoImage(file="bg.gif")  # 创建背景图
    addZhuLabel = Label(top1, image=addPhoto)
    addZhuLabel.pack()
    addTextLabel2 = Label(top1, text="请填入下面信息")
    addTextLabel2.place(relx=0.5, rely=0.2, anchor='center')
    # 创建文本输入框
    v1 = StringVar()
    v2 = StringVar()
    Label(top1, text="电话").place(relx=0.2, rely=0.3)
    e1 = Entry(top1, textvariable=v1)
    e1.place(relx=0.4, rely=0.3, width=70)
    Label(top1, text="姓名").place(relx=0.2, rely=0.5)
    e2 = Entry(top1, textvariable=v2)
    e2.place(relx=0.4, rely=0.5, width=70)
    # 创建选择按钮
    button1 = Button(top1, text='确认', width=5, height=2,  command=addData)
    button1.place(relx=0.2, rely=0.7)
    button2 = Button(top1, text='退出', width=5, height=2, command=top1.withdraw)
    button2.place(relx=0.6, rely=0.7)
    mainloop()

3.删除用户界面
删除用户首先搜索找到该用户位置,在对记录进行删除。


删除.PNG

对应代码:

def delete():
    def deleteData():
        deleteName = vv.get()
        if deleteName == '':
            messagebox.showerror("Q电话本", "号码不能为空!")
        else:
            if messagebox.askokcancel("Q电话本", "您确认删除该联系人吗?") is True:
                temp = False
                with open("record.txt", "r") as f5:
                    text = f5.readline()
                    while text:
                        if text.find(deleteName) == 0:
                            file_data = ""
                            with open("record.txt", "r") as f:
                                for line in f:
                                    if deleteName in line:
                                        line = line.replace(text[0:len(text)], "")
                                        # line = line.strip('\n')
                                    file_data += line
                            with open("record.txt", "w") as f:
                                f.write(file_data)

                            if messagebox.showinfo("Q电话本", "操作成功") == 'ok':
                                # exit()
                                temp = True
                                break
                        else:
                            text = f5.readline()
                if temp == False:
                    messagebox.showerror("Q电话本", "没有该联系人,点击返回")
    top4 = Toplevel()
    top4.title("删除用户")
    top4.iconbitmap('my48.ico')
    top4.attributes("-alpha", 0.9)  # 窗口透明度10 %
    addPhoto = PhotoImage(file="bg.gif")  # 创建背景图
    addZhuLabel = Label(top4, image=addPhoto)
    addZhuLabel.pack()
    deleteTextLabel2 = Label(top4, text="删除的联系人:")
    deleteTextLabel2.place(relx=0.5, rely=0.3, anchor='center')
    # 创建文本输入框
    vv = StringVar()
    Label(top4, text="号码:").place(relx=0.2, rely=0.5)
    e1 = Entry(top4, textvariable=vv)
    e1.place(relx=0.4, rely=0.5, width=70)
    # 创建选择按钮
    button1 = Button(top4, text='确认', width=5, height=2,  command=deleteData)
    button1.place(relx=0.2, rely=0.7)
    button2 = Button(top4, text='返回', width=5, height=2,  command=top4.withdraw)
    button2.place(relx=0.6, rely=0.7)
    mainloop()

4.关于界面
简单显示软件和作者信息。


关于.PNG

对应代码:

def about():
    top2 = Toplevel()
    top2.title("关于")
    top2.iconbitmap('my48.ico')
    top2.attributes("-alpha", 0.9)  # 窗口透明度10 %
    addPhoto = PhotoImage(file="bg.gif")  # 创建背景图
    addZhuLabel = Label(top2, image=addPhoto)
    addZhuLabel.pack()
    # 不能使用两次Tk()去创建窗体,因为tkinter中只能有一个主线程,
    # 当你需要再次创建一个窗体时,请使用Toplevel()。
    addTextLabel1 = Label(top2, text="Q_phone_dic(Q电话本)")  # 创建背景图上的文本
    addTextLabel1.place(relx=0.5, rely=0.3, anchor='center')
    addTextLabel2 = Label(top2, text="作者:bc_zhang")
    addTextLabel2.place(relx=0.5, rely=0.5, anchor='center')
    addTextLabel2 = Label(top2, text="版本:version 2.1")
    addTextLabel2.place(relx=0.5, rely=0.7, anchor='center')
    mainloop()

5.搜索功能
通过对输入字符的判断,实现人名到号码和号码到人名的双向搜索。
对应代码:

def find(self):
    findName = vartext.get()
    n = len(findName)
    if findName == '':
        pass
    elif findName.isdigit()== True:
        temp = False
        with open("record.txt", "r") as f2:
            text = f2.readline()
            text_temp = text.split(' ')
            while text:
                if text_temp[0].find(findName) == 0:
                    temp1 =  text_temp[1]
                    vartext.set(temp1)
                    return vartext
                else:
                    text = f2.readline()
                    text_temp = text.split(' ')
        if temp == False:
            vartext.set('无该用户!!!')
            return vartext
    else:
        temp = False
        with open("record.txt", "r") as f2:
            text = f2.readline()
            text_temp = text.split(' ')
            while text:
                if text_temp[1].find(findName) == 0:
                    temp1 =  text_temp[0]
                    vartext.set(temp1)
                    return vartext
                else:
                    text = f2.readline()
                    text_temp = text.split(' ')
        if temp == False:
            vartext.set('无该用户!!!')
            return vartext

三、pyinstaller打包成exe
解决软件在不同平台上运行的问题,通过pyinstaller库(3.3.1)对程序进行打包,在程序所在文件夹打开命令行,运行pyinstaller -F -i -w **.ico ***.py,将软件和icon打包好。

打包.PNG
目前测试win10,win7均可运行。在XP系统上会报“不是有效的win32应用程序”错误,还未解决,希望了解的大佬教一教。
四、附件
https://github.com/zhangbinchao/tkinter-Q-phone-dic

相关文章

  • Python3_tkinter电话本制作

    初学者一枚,记录一下GUI学习的过程。一、开发环境版本:python3.5.2编程软件:pycharm配置库:1....

  • 【Python】用Python实现最基本的电话本

    <用Python实现最基本的电话本> 用Python实现最基本的电话本代码片段 [文件] myPhoneBook2...

  • 3.3 通信录(电话本)---使用FMDB进行数据存储

    通信录(电话本)---使用FMDB进行数据存储 通信录(电话本)---使用FMDB进行数据存储.png

  • 电话本

    原创:合肥市琥珀中学2016级 王可 一本破旧不堪的电话本,一颗承载思念儿孙的心。 ...

  • 电话本

    以前的生活都是慢节奏,习惯把家人的、朋友的、客户的电话号码和地址都认真抄写在电话本上,慢慢时兴QQ,微信,手...

  • 电话本。

    爷爷从黑匣子里抽出方方的电话本, 一个人,一个号,一笔一划, 一个姓,一个名,一横一竖, 小心翼翼地,慢悠悠地填写...

  • 2020-01-03电话本管理系统数组版

    需求:一、电话本管理系统 1.主界面 2.添加 2.修改 1)无信息 2)正常修改 3 . 打印所有电话本 [图片...

  • iOS开发:UITableVIew实现类似于电话本的首字母索引

    UITableVIew实现类似于电话本的首字母索引 实际上UITableView默认就支持象电话本那样的按首字母索...

  • 父亲的电话本

    打扫房间无意看到父亲的电话本 看着上面熟悉的笔迹我的眼角湿润了 虽然父亲去世前早已淘汰了电话本 ...

  • Python3_tkinter模块常用参数

    1、使用tkinter.Tk() 生成主窗口(root=tkinter.Tk()); 2、初级样例: 3、tkin...

网友评论

      本文标题:Python3_tkinter电话本制作

      本文链接:https://www.haomeiwen.com/subject/edaiwftx.html