当前位置:   article > 正文

python通讯录完整(包括GUI界面)_python完整通讯录包括gui界面

python完整通讯录包括gui界面

IT Xiao Ang Zai 8月28号

版本:python3.64

编程软件:pycharm

 

代码入下:

  1. from tkinter import *
  2. from tkinter import messagebox
  3. def add():
  4. #下面定义增加信息文件操作
  5. def addData():
  6. if v1.get() == '' or v2.get() == '' or v3.get() == '':
  7. messagebox.showerror("IT Xiao Ang Zai", "信息有一个不能为空!")
  8. else :
  9. messagebox.askokcancel("IT Xiao Ang Zai","您确认增加该联系人吗?")
  10. if messagebox.askokcancel() is True:
  11. #下面是进行增加信息文件操作
  12. with open("E:\\record.txt", "a") as f1:
  13. f1.write(v1.get())
  14. f1.write(",")
  15. f1.write(v2.get())
  16. f1.write(",")
  17. f1.write(v3.get())
  18. f1.write("\n")
  19. #下面是增加信息操作的界面化
  20. #创建一个顶级容器
  21. top1 = Toplevel()
  22. top1.title("通讯录增加界面")
  23. #不能使用两次Tk()去创建窗体,因为tkinter中只能有一个主线程,
  24. # 当你需要再次创建一个窗体时,请使用Toplevel()。
  25. addPhoto = PhotoImage(file="E:\\image\\2.gif")#创建背景图
  26. addZhuLabel = Label(top1, image=addPhoto)
  27. addZhuLabel.pack()
  28. addTextLabel1 = Label(top1, text="欢迎进入增加信息界面")#创建背景图上的文本
  29. addTextLabel1.place(relx=0.5, rely=0.1, anchor='center')
  30. addTextLabel2 = Label(top1,text = "请输入下面信息")
  31. addTextLabel2.place(relx=0.5, rely=0.3, anchor='center')
  32. #创建文本输入框
  33. v1 = StringVar()
  34. v2 = StringVar()
  35. v3 = StringVar()
  36. Label(top1,text = "人名").place(relx=0.2,rely=0.4)
  37. e1 = Entry(top1,textvariable = v1)
  38. e1.place(relx=0.3, rely=0.4, width=70)
  39. Label(top1, text="年龄").place(relx=0.6, rely=0.4)
  40. e2 = Entry(top1,textvariable = v2)
  41. e2.place(relx=0.7, rely=0.4, width=70)
  42. Label(top1, text="电话").place(relx=0.2, rely=0.6)
  43. e3 = Entry(top1,textvariable = v3)
  44. e3.place(relx=0.3, rely=0.6, width=160)
  45. # 创建选择按钮
  46. button1 = Button(top1, text='确认', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command=addData)
  47. button1.place(relx=0.2, rely=0.7)
  48. button2 = Button(top1, text='退出', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command = top1.withdraw)
  49. button2.place(relx=0.6,rely=0.7)
  50. mainloop()
  51. def find():
  52. def findData():
  53. findName = vv.get()
  54. n = len(vv.get())
  55. if findName == '':
  56. messagebox.showerror("IT Xiao Ang Zai", "姓名不能为空!")
  57. else:
  58. temp = False
  59. with open("E:\\record.txt", "r") as f2:
  60. text = f2.readline()
  61. while text:
  62. if text.find(findName) == 0:
  63. Label(top2,text = "下面是联系人信息",pady = 10).pack()
  64. findName = Label(top2,text = "姓名:"+text[0:len(findName)])
  65. findName.pack()
  66. findAge = Label(top2,text = "年龄:"+text[n+1:n+3])
  67. findAge.pack()
  68. findTele = Label(top2,text = "电话:"+text[n+4:])
  69. findTele.pack()
  70. messagebox.showinfo("IT Xiao Ang Zai", "操作成功")
  71. else:
  72. text = f2.readline()
  73. if temp == False:
  74. messagebox.showerror("IT Xiao Ang Zai", "没有该联系人,点击返回")
  75. top2 = Toplevel()
  76. top2.title("通讯录查找界面")
  77. findPhoto = PhotoImage(file="E:\\image\\3.gif") # 创建背景图
  78. findZhuLabel = Label(top2, image=findPhoto)
  79. findZhuLabel.pack()
  80. findTextLabel1 = Label(top2, text="欢迎进入查找信息界面", font=("", 15)) # 创建背景图上的文本
  81. findTextLabel1.place(relx=0.5, rely=0.1, anchor='center')
  82. findTextLabel2 = Label(top2, text="请确认要查找的联系人:")
  83. findTextLabel2.place(relx=0.5, rely=0.3, anchor='center')
  84. # 创建文本输入框
  85. vv = StringVar()
  86. Label(top2, text="姓名").place(relx=0.4, rely=0.5)
  87. e1 = Entry(top2, textvariable=vv)
  88. e1.place(relx=0.5, rely=0.5, width=50)
  89. # 创建选择按钮
  90. button1 = Button(top2, text='查找', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command=findData)
  91. button1.place(relx=0.2, rely=0.7)
  92. button2 = Button(top2, text='退出', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command=top2.withdraw)
  93. button2.place(relx=0.6, rely=0.7)
  94. mainloop()
  95. def change():
  96. def changeData():
  97. def writeFile():
  98. file_data = ""
  99. with open("E:\\record.txt", "r") as f:
  100. for line in f:
  101. if findName in line:
  102. line = line.replace(text[0:len(findName)], v1.get())
  103. line = line.replace(text[len(findName) + 1:len(findName) + 3], v2.get())
  104. line = line.replace(text[len(findName) + 4:], v3.get() + "\n")
  105. file_data += line
  106. with open("E:\\record.txt", "w") as f2:
  107. f2.write(file_data)
  108. messagebox.showinfo("IT Xiao Ang Zai", "确认成功,退出程序")
  109. if messagebox.showinfo == 'ok':
  110. exit()
  111. changeName = vv.get()
  112. if changeName == '':
  113. messagebox.showerror("IT Xiao Ang Zai", "姓名不能为空!")
  114. else:
  115. findName = vv.get()
  116. temp = False
  117. with open("E:\\record.txt", "r") as f2:
  118. text = f2.readline()
  119. while text:
  120. if text.find(findName) == 0:
  121. messagebox.askokcancel("IT Xiao Ang Zai", "您确认修改该联系人吗?")
  122. if messagebox.askokcancel() is True:
  123. topp = Toplevel()
  124. Label(topp, text="姓名").grid(row=0)
  125. Label(topp, text="年龄").grid(row=1)
  126. Label(topp, text="电话").grid(row=2)
  127. v1 = StringVar()
  128. v2 = StringVar()
  129. v3 = StringVar()
  130. Entry(topp,textvariable=v1).grid(row=0, column=1)
  131. Entry(topp,textvariable=v2).grid(row=1, column=1)
  132. Entry(topp,textvariable=v3).grid(row=2, column=1)
  133. button1 = Button(topp, text='确认', width=6, height=2, bg='#00FF7F SpringGreen 春绿色',
  134. command=writeFile)
  135. button1.grid(row=3, columnspan=3, pady=5)
  136. mainloop()
  137. else:
  138. text = f2.readline()
  139. if temp == False:
  140. messagebox.showerror("IT Xiao Ang Zai", "没有该联系人,点击返回")
  141. top3 = Toplevel()
  142. top3.title("通讯录修改界面")
  143. deletePhoto = PhotoImage(file="E:\\image\\4.gif") # 创建背景图
  144. deleteZhuLabel = Label(top3, image=deletePhoto)
  145. deleteZhuLabel.pack()
  146. deleteTextLabel1 = Label(top3, text="欢迎进入修改信息界面",font = ("",15)) # 创建背景图上的文本
  147. deleteTextLabel1.place(relx=0.5, rely=0.1, anchor='center')
  148. deleteTextLabel2 = Label(top3, text="请确认要修改的联系人:")
  149. deleteTextLabel2.place(relx=0.5, rely=0.3, anchor='center')
  150. # 创建文本输入框
  151. vv = StringVar()
  152. Label(top3, text="姓名").place(relx=0.4, rely=0.5)
  153. e1 = Entry(top3, textvariable=vv)
  154. e1.place(relx=0.5, rely=0.5, width=50)
  155. # 创建选择按钮
  156. button1 = Button(top3, text='确认', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command=changeData)
  157. button1.place(relx=0.2, rely=0.7)
  158. button2 = Button(top3, text='返回', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command=top3.withdraw)
  159. button2.place(relx=0.6, rely=0.7)
  160. mainloop()
  161. def delete():
  162. def deleteData():
  163. deleteName = vv.get()
  164. if deleteName == '':
  165. messagebox.showerror("IT Xiao Ang Zai", "姓名不能为空!")
  166. else :
  167. messagebox.askokcancel("IT Xiao Ang Zai", "您确认删除该联系人吗?")
  168. if messagebox.askokcancel() is True:
  169. temp = False
  170. with open("E:\\record.txt", "r") as f5:
  171. text = f5.readline()
  172. while text:
  173. if text.find(deleteName) == 0:
  174. file_data = ""
  175. with open("E:\\record.txt", "r") as f:
  176. for line in f:
  177. if deleteName in line:
  178. line = line.replace(text[0:len(text)], "")
  179. file_data += line
  180. with open("E:\\record.txt", "w") as f:
  181. f.write(file_data)
  182. messagebox.showinfo("IT Xiao Ang Zai","操作成功")
  183. if messagebox.showinfo == 'ok':
  184. exit()
  185. else:
  186. text = f5.readline()
  187. if temp == False:
  188. messagebox.showerror("IT Xiao Ang Zai", "没有该联系人,点击返回")
  189. top4 = Toplevel()
  190. top4.title("通讯录删除界面")
  191. deletePhoto = PhotoImage(file="E:\\image\\5.gif") # 创建背景图
  192. deleteZhuLabel = Label(top4, image=deletePhoto)
  193. deleteZhuLabel.pack()
  194. deleteTextLabel1 = Label(top4, text="欢迎进入删除信息界面",font = ("",15)) # 创建背景图上的文本
  195. deleteTextLabel1.place(relx=0.5, rely=0.1, anchor='center')
  196. deleteTextLabel2 = Label(top4, text="请确认要删除的联系人:")
  197. deleteTextLabel2.place(relx=0.5, rely=0.3, anchor='center')
  198. # 创建文本输入框
  199. vv = StringVar()
  200. Label(top4, text="姓名").place(relx=0.4, rely=0.5)
  201. e1 = Entry(top4, textvariable=vv)
  202. e1.place(relx=0.5, rely=0.5, width=50)
  203. # 创建选择按钮
  204. button1 = Button(top4, text='确认', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command=deleteData)
  205. button1.place(relx=0.2, rely=0.7)
  206. button2 = Button(top4, text='返回', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command=top4.withdraw)
  207. button2.place(relx=0.6, rely=0.7)
  208. mainloop()
  209. def main():
  210. def secondJieMian():
  211. # 为单选按钮进行不同选择
  212. getChoose = v.get()
  213. if getChoose == 1:
  214. add()
  215. elif getChoose == 2:
  216. find()
  217. elif getChoose == 3:
  218. change()
  219. elif getChoose == 4:
  220. delete()
  221. else:
  222. exit()
  223. # 创建主窗口
  224. root = Tk()
  225. root.title("通讯录系统")
  226. # 创建背景图片
  227. photo = PhotoImage(file="E:\\image\\1.gif")
  228. zhuLabel = Label(root, image=photo)
  229. zhuLabel.pack()
  230. # 创建背景图上的文本
  231. textLabel1 = Label(root, text="欢迎进入通讯录系统")
  232. textLabel1.place(relx=0.5, rely=0.1, anchor='center')
  233. textLabel2 = Label(root, text="请选择", font=("", 40), fg='#7CFC00 LawnGreen 草绿色/草坪绿', anchor='center')
  234. textLabel2.place(relx=0.5, rely=0.3, anchor='center')
  235. # 创建单选按钮
  236. choose = [("1.添加联系人", 1), ("2.查询联系人", 2), ("3.修改联系人", 3), ("4.删除联系人", 4),
  237. ("0.退出", 0)]
  238. v = IntVar()
  239. v.set(1)
  240. for lang, num in choose:
  241. a = Radiobutton(root, text=lang, variable=v, value=num)
  242. a.pack()
  243. # 创建选择按钮
  244. Button(root, text='确认', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command=secondJieMian).pack(side='left')
  245. Button(root, text='退出', width=10, height=3, bg='#00FF7F SpringGreen 春绿色', command=root.quit).pack(side='right')
  246. mainloop()
  247. if __name__ == '__main__':
  248. main()

 

效果如下:

 

一:下面是增加操作

当有一项为空时,会提示:

我们增加一条信息:

会弹出两次界面(可能是个BUG,之后我会完善):

我们看到E盘的record文件记录了这条信息:

我们多增加几个信息:

 

二:下面时查找操作

当输入要查找的姓名为空时,会提示:

但没有该联系人是,会提示:

当有该联系人时,会显示他的个人信息:

 

三:下面进行修改操作

注:内容过多,这里就不一一讲解了。

我们看一下最后的文件:

 

四:下面是删除操作:

我们看一下文件:

我们再删除一个:

 

退出选项和点界面右上角得"X",效果一样,这个程序还有很多BUG,还有许多改进的地方,欢迎大家进行评论留言,我还会持续更新python笔记和一些小项目分享给大家。

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/464768
推荐阅读
相关标签
  

闽ICP备14008679号