2022年 11月 13日

Python制作登陆界面(2)(中等级)

前言  

本人又来教大家制作登陆界面啦!

大家知道弹窗在电脑中无处不在:浏览器界面啊,Python编程界面啊,网络会议室啊等等。

相信你们在使用Python中肯定用过一个庞大的GUI模块:tkinter。

这次我们会用到它来制作弹窗登陆界面。

1、关于tkinter

tkinter是python中最基础,最标准的GUI库。

除了它以外,在python里还有许许多多的GUI库:wx,PyQt5……

而这些都是Python不自带的,必须使用管理员或终端使用pip install(使用Mac的伙伴用sudo apt-get)指令来下载。

(有些Python版本在下载的时候不会自带tkinter,cmd输入pip install tkinter(Mac的用brew install python-tk)即可)

例:

  1. import tkinter
  2. root = tkinter.Tk()
  3. root.title("示例")
  4. root.geometry("200x300")

效果:

tkinter里面不仅有Tk(GUI主界面),title(标题)和geometry(大小),还有Entry(输入框),Label(标签),Frame(框,有些类似于Menu),Menu(菜单),Toplevel(叠加GUI)等。

无论如何,这些功能前面都要加一个tkinter.(也可以是tk.,前提是在导入模块时要输入import tkinter as tk),不然会报错:

2、制作

1、基本框架

导入模块:

  1. from tkinter import * #这里为了简便,一般不建议使用
  2. from tkinter import messagebox
  3. import pickle

基本结构:

  1. Lw = Tk()
  2. Lw.title("测试")
  3. Lw.geometry("500x300")
  4. Label(Lw, text = "登录 ", font = ("幼圆", 26), fg = "white", bg = "green").place(x = 10, y = 10)
  5. Label(Lw, text = "用户名:", font = ("幼圆", 14), fg = "green").place(x = 20, y = 70) #用户名
  6. Label(Lw, text = "密码:", font = ("幼圆", 14), fg = "green").place(x = 20, y = 100) #密码
  7. _usn = StringVar()
  8. eusn = Entry(Lw, textvariable = _usn, font = ("幼圆", 14), width = 30)
  9. eusn.place(x = 100, y = 70)
  10. _pwd = StringVar()
  11. epwd = Entry(Lw, textvariable = _pwd, font = ("幼圆", 14), width = 30, show = "⁕")
  12. epwd.place(x = 100, y = 100)

2、功能部分

登录:

  1. def login():
  2. countdown = 0
  3. usn = _usn.get()
  4. pwd = _pwd.get()
  5. try:
  6. with open('usr_info.pickle', 'rb') as usr_file:
  7. usrs_info = pickle.load(usr_file)
  8. except FileNotFoundError:
  9. with open('usr_info.pickle', 'wb') as usr_file:
  10. usrs_info = {'admin': 'admin'}
  11. pickle.dump(usrs_info, usr_file)
  12. if usn in usrs_info:
  13. if pwd == usrs_info[usn]:
  14. messagebox.showinfo("欢迎", "登陆成功!")
  15. else:
  16. messagebox.showerror("错误", "用户名或密码错误!")

登录时会自动往用户库里写入用户信息,若没有库的话会自动创建一个。

此程序会辨别输入的用户到底有没有库里,不在的话会显示错误。

注册与控件:

  1. def signIn():
  2. def signUser():
  3. nu = _nusr.get()
  4. np = _npwd.get
  5. nc = _npco.get()
  6. try:
  7. with open('usr_info.pickle', 'rb') as usr_file:
  8. exist_usr_info = pickle.load(usr_file)
  9. except FileNotFoundError:
  10. exist_usr_info = {}
  11. if nu and np in exist_usr_info:
  12. messagebox.showerror("错误", "该用户已存在!")
  13. elif np != nc:
  14. messagebox.showerror("错误", "密码与验证密码不一致!")
  15. elif nu == "" or np == "":
  16. messagebox.showerror("错误", "用户名或密码不能为空!")
  17. else:
  18. exist_usr_info[nu] = np
  19. with open('usr_info.pickle', 'wb') as usr_file:
  20. pickle.dump(exist_usr_info, usr_file)
  21. tk.messagebox.showinfo('欢迎', '注册成功')
  22. Sw.destroy()
  23. Sw = Toplevel(Lw)
  24. Sw.geometry('350x200')
  25. Sw.title('注册')
  26. Label(Sw, text='用户名:').place(x=10, y=10)
  27. Label(Sw, text='请输入密码:').place(x=10, y=50)
  28. Label(Sw, text='请再次输入密码:').place(x=10, y=90)
  29. _nusr = StringVar()
  30. Entry(Sw, textvariable=_nusr).place(x=150, y=10)
  31. _npwd = StringVar()
  32. Entry(Sw, textvariable=new_pwd, show="⁕").place(x=150, y=50)
  33. _npco = StringVar()
  34. Entry(Sw, textvariable=new_pwd_confirm, show="⁕").place(x=150, y=90)
  35. coBt = Button(Sw, text="确认注册",command=signUser)
  36. coBt.place(x=150, y=130)
  37. siBt = Button(Lw, text = "没有账号?注册", command = signIn)
  38. siBt.place(x = 50, y = 200)
  39. loBt = Button(Lw, text = "登录", command = login)
  40. loBt.place(x = 10, y = 200)

3、完整代码

  1. #!/usr/bin/env/python38
  2. #-*- coding:utf-8 -*-
  3. #Author:Ez135513
  4. #Created time:7/20/2022
  5. from tkinter import * #这里为了简便,一般不建议使用
  6. from tkinter import messagebox
  7. import pickle
  8. Lw = Tk()
  9. Lw.title("测试")
  10. Lw.geometry("500x300")
  11. Label(Lw, text = "登录 ", font = ("幼圆", 26), fg = "white", bg = "green").place(x = 10, y = 10)
  12. Label(Lw, text = "用户名:", font = ("幼圆", 14), fg = "green").place(x = 20, y = 70) #用户名
  13. Label(Lw, text = "密码:", font = ("幼圆", 14), fg = "green").place(x = 20, y = 100) #密码
  14. _usn = StringVar()
  15. eusn = Entry(Lw, textvariable = _usn, font = ("幼圆", 14), width = 30)
  16. eusn.place(x = 100, y = 70)
  17. _pwd = StringVar()
  18. epwd = Entry(Lw, textvariable = _pwd, font = ("幼圆", 14), width = 30, show = "⁕")
  19. epwd.place(x = 100, y = 100)
  20. def login():
  21. countdown = 0
  22. usn = _usn.get()
  23. pwd = _pwd.get()
  24. try:
  25. with open('usr_info.pickle', 'rb') as usr_file:
  26. usrs_info = pickle.load(usr_file)
  27. except FileNotFoundError:
  28. with open('usr_info.pickle', 'wb') as usr_file:
  29. usrs_info = {'admin': 'admin'}
  30. pickle.dump(usrs_info, usr_file)
  31. if usn in usrs_info:
  32. if pwd == usrs_info[usn]:
  33. messagebox.showinfo("欢迎", "登陆成功!")
  34. else:
  35. messagebox.showerror("错误", "用户名或密码错误!")
  36. def signIn():
  37. def signUser():
  38. nu = _nusr.get()
  39. np = _npwd.get
  40. nc = _npco.get()
  41. try:
  42. with open('usr_info.pickle', 'rb') as usr_file:
  43. exist_usr_info = pickle.load(usr_file)
  44. except FileNotFoundError:
  45. exist_usr_info = {}
  46. if nu and np in exist_usr_info:
  47. messagebox.showerror("错误", "该用户已存在!")
  48. elif np != nc:
  49. messagebox.showerror("错误", "密码与验证密码不一致!")
  50. elif nu == "" or np == "":
  51. messagebox.showerror("错误", "用户名或密码不能为空!")
  52. else:
  53. exist_usr_info[nu] = np
  54. with open('usr_info.pickle', 'wb') as usr_file:
  55. pickle.dump(exist_usr_info, usr_file)
  56. tk.messagebox.showinfo('欢迎', '注册成功')
  57. Sw.destroy()
  58. Sw = Toplevel(Lw)
  59. Sw.geometry('350x200')
  60. Sw.title('注册')
  61. Label(Sw, text='用户名:').place(x=10, y=10)
  62. Label(Sw, text='请输入密码:').place(x=10, y=50)
  63. Label(Sw, text='请再次输入密码:').place(x=10, y=90)
  64. _nusr = StringVar()
  65. Entry(Sw, textvariable=_nusr).place(x=150, y=10)
  66. _npwd = StringVar()
  67. Entry(Sw, textvariable=new_pwd, show="⁕").place(x=150, y=50)
  68. _npco = StringVar()
  69. Entry(Sw, textvariable=new_pwd_confirm, show="⁕").place(x=150, y=90)
  70. coBt = Button(Sw, text="确认注册",command=signUser)
  71. coBt.place(x=150, y=130)
  72. siBt = Button(Lw, text = "没有账号?注册", command = signIn)
  73. siBt.place(x = 50, y = 200)
  74. loBt = Button(Lw, text = "登录", command = login)
  75. loBt.place(x = 10, y = 200)

大功告成!