前言
本人又来教大家制作登陆界面啦!
大家知道弹窗在电脑中无处不在:浏览器界面啊,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)即可)
例:
import tkinter
root = tkinter.Tk()
root.title("示例")
root.geometry("200x300")
效果:
tkinter里面不仅有Tk(GUI主界面),title(标题)和geometry(大小),还有Entry(输入框),Label(标签),Frame(框,有些类似于Menu),Menu(菜单),Toplevel(叠加GUI)等。
无论如何,这些功能前面都要加一个tkinter.(也可以是tk.,前提是在导入模块时要输入import tkinter as tk),不然会报错:
2、制作
1、基本框架
导入模块:
from tkinter import * #这里为了简便,一般不建议使用
from tkinter import messagebox
import pickle
基本结构:
Lw = Tk()
Lw.title("测试")
Lw.geometry("500x300")
Label(Lw, text = "登录 ", font = ("幼圆", 26), fg = "white", bg = "green").place(x = 10, y = 10)
Label(Lw, text = "用户名:", font = ("幼圆", 14), fg = "green").place(x = 20, y = 70) #用户名
Label(Lw, text = "密码:", font = ("幼圆", 14), fg = "green").place(x = 20, y = 100) #密码
_usn = StringVar()
eusn = Entry(Lw, textvariable = _usn, font = ("幼圆", 14), width = 30)
eusn.place(x = 100, y = 70)
_pwd = StringVar()
epwd = Entry(Lw, textvariable = _pwd, font = ("幼圆", 14), width = 30, show = "⁕")
epwd.place(x = 100, y = 100)
2、功能部分
登录:
def login():
countdown = 0
usn = _usn.get()
pwd = _pwd.get()
try:
with open('usr_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usr_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
if usn in usrs_info:
if pwd == usrs_info[usn]:
messagebox.showinfo("欢迎", "登陆成功!")
else:
messagebox.showerror("错误", "用户名或密码错误!")
登录时会自动往用户库里写入用户信息,若没有库的话会自动创建一个。
此程序会辨别输入的用户到底有没有库里,不在的话会显示错误。
注册与控件:
def signIn():
def signUser():
nu = _nusr.get()
np = _npwd.get
nc = _npco.get()
try:
with open('usr_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
except FileNotFoundError:
exist_usr_info = {}
if nu and np in exist_usr_info:
messagebox.showerror("错误", "该用户已存在!")
elif np != nc:
messagebox.showerror("错误", "密码与验证密码不一致!")
elif nu == "" or np == "":
messagebox.showerror("错误", "用户名或密码不能为空!")
else:
exist_usr_info[nu] = np
with open('usr_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('欢迎', '注册成功')
Sw.destroy()
Sw = Toplevel(Lw)
Sw.geometry('350x200')
Sw.title('注册')
Label(Sw, text='用户名:').place(x=10, y=10)
Label(Sw, text='请输入密码:').place(x=10, y=50)
Label(Sw, text='请再次输入密码:').place(x=10, y=90)
_nusr = StringVar()
Entry(Sw, textvariable=_nusr).place(x=150, y=10)
_npwd = StringVar()
Entry(Sw, textvariable=new_pwd, show="⁕").place(x=150, y=50)
_npco = StringVar()
Entry(Sw, textvariable=new_pwd_confirm, show="⁕").place(x=150, y=90)
coBt = Button(Sw, text="确认注册",command=signUser)
coBt.place(x=150, y=130)
siBt = Button(Lw, text = "没有账号?注册", command = signIn)
siBt.place(x = 50, y = 200)
loBt = Button(Lw, text = "登录", command = login)
loBt.place(x = 10, y = 200)
3、完整代码
#!/usr/bin/env/python38
#-*- coding:utf-8 -*-
#Author:Ez135513
#Created time:7/20/2022
from tkinter import * #这里为了简便,一般不建议使用
from tkinter import messagebox
import pickle
Lw = Tk()
Lw.title("测试")
Lw.geometry("500x300")
Label(Lw, text = "登录 ", font = ("幼圆", 26), fg = "white", bg = "green").place(x = 10, y = 10)
Label(Lw, text = "用户名:", font = ("幼圆", 14), fg = "green").place(x = 20, y = 70) #用户名
Label(Lw, text = "密码:", font = ("幼圆", 14), fg = "green").place(x = 20, y = 100) #密码
_usn = StringVar()
eusn = Entry(Lw, textvariable = _usn, font = ("幼圆", 14), width = 30)
eusn.place(x = 100, y = 70)
_pwd = StringVar()
epwd = Entry(Lw, textvariable = _pwd, font = ("幼圆", 14), width = 30, show = "⁕")
epwd.place(x = 100, y = 100)
def login():
countdown = 0
usn = _usn.get()
pwd = _pwd.get()
try:
with open('usr_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usr_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
if usn in usrs_info:
if pwd == usrs_info[usn]:
messagebox.showinfo("欢迎", "登陆成功!")
else:
messagebox.showerror("错误", "用户名或密码错误!")
def signIn():
def signUser():
nu = _nusr.get()
np = _npwd.get
nc = _npco.get()
try:
with open('usr_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
except FileNotFoundError:
exist_usr_info = {}
if nu and np in exist_usr_info:
messagebox.showerror("错误", "该用户已存在!")
elif np != nc:
messagebox.showerror("错误", "密码与验证密码不一致!")
elif nu == "" or np == "":
messagebox.showerror("错误", "用户名或密码不能为空!")
else:
exist_usr_info[nu] = np
with open('usr_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('欢迎', '注册成功')
Sw.destroy()
Sw = Toplevel(Lw)
Sw.geometry('350x200')
Sw.title('注册')
Label(Sw, text='用户名:').place(x=10, y=10)
Label(Sw, text='请输入密码:').place(x=10, y=50)
Label(Sw, text='请再次输入密码:').place(x=10, y=90)
_nusr = StringVar()
Entry(Sw, textvariable=_nusr).place(x=150, y=10)
_npwd = StringVar()
Entry(Sw, textvariable=new_pwd, show="⁕").place(x=150, y=50)
_npco = StringVar()
Entry(Sw, textvariable=new_pwd_confirm, show="⁕").place(x=150, y=90)
coBt = Button(Sw, text="确认注册",command=signUser)
coBt.place(x=150, y=130)
siBt = Button(Lw, text = "没有账号?注册", command = signIn)
siBt.place(x = 50, y = 200)
loBt = Button(Lw, text = "登录", command = login)
loBt.place(x = 10, y = 200)
大功告成!