Python-tkinter自制登录界面(含注册)

简单的用户登录、注册界面

import tkinter as tk
import time
import subprocess
import sys
import os
import tkinter.messagebox

window = tk.Tk()
window.title('GCHEK')
window.geometry('400x300')
#设置储存用户信息的容器,这里用的txt。
if not os.path.exists('Users'):
    os.mkdir('Users')
with open('Users/Users.txt','r') as fp:
    fp.close()
#设置美化图片
canvas = tk.Canvas(window, bg='blue', height=120, width=400)
canvas.pack(side='top')
image_file = tk.PhotoImage(file='pic.gif')  # 图片位置(相对路径,与.py文件同一文件夹下,也可以用绝对路径,需要给定图片具体绝对路径)
image = canvas.create_image(200, 0, anchor='n',image=image_file)
#注册窗口
def sign_up_winow():
    window1 = tk.Toplevel(window)
    window1.title('注册')
    window1.geometry('300x130')
    window1.attributes("-topmost", True)
    #注册账号、密码设置规则,避免影响后续识别错误
    def pd():
        if "@" in e1.get():
            tkinter.messagebox.showerror(title='GCHEK',message='账号密码中不能带@',parent=window1)
            return None
        elif  "@" in e2.get():
            tkinter.messagebox.showerror(title='GCHEK',message='账号密码中不能带@',parent=window1)
            return None
        else:
            return Users()
    #储存用户注册信息
    def Users():
        with open('Users/Users.txt','a') as fp:
            fp.write(e1.get()+'@' + e2.get()+'\n')
            tkinter.messagebox.showinfo(title='GCHEK',message='注册成功',parent=window1)
            fp.close()
            window1.destroy()
    def exit():
        window1.destroy()
    # 输入账号
    l1 = tk.Label(window1, text='User-name:', font=('Arial', 12), width=20)
    l1.place(x=80, y=0, anchor='n')
    e1 = tk.Entry(window1, font=('Arial', 10), width=20)
    e1.place(x=200, y=0, anchor='n')
    # 输入密码
    l2 = tk.Label(window1, text='User-password:', font=('Arial', 12), width=20)
    l2.place(x=65, y=23, anchor='n')
    e2 = tk.Entry(window1, font=('Arial', 10), width=20)
    e2.place(x=200, y=25, anchor='n')
    # 注册按钮
    b1 = tk.Button(window1, text='Sign In', width=10, command=pd)
    b1.place(x=80, y=70, anchor='n')
    # 退出按钮
    b2 = tk.Button(window1, text='Exit', width=10, command=exit)
    b2.place(x=220, y=70, anchor='n')
    window1.mainloop()
#验证用户账号、密码,做出判断
def find_Users():
    try:
        with open('Users/Users.txt','r',encoding='utf8') as fp:
            txt=fp.readlines()
            for i in txt:
                a=i.strip()
                name,password = a.split('@')
                if e1.get() == name and e2.get() == password:
                    tkinter.messagebox.showinfo(title='GCHEK', message='Welcome!登录成功')
                    fp.close()
                    return next_part()
                elif e1.get()==name:
                    tkinter.messagebox.showerror(title='GCHEK', message='账号密码错误')
                    fp.close()
                    return None
                else:
                    continue
            tkinter.messagebox.showwarning(title='GEHEK', message='账号未注册')
    except:
        tkinter.messagebox.showerror(title="GCHEK",message='错误')
#验证成功开启下一步流程
def next_part():
    window.destroy()
    subprocess.run([{}{输入你想运行的程序}],shell=True)
	sys.exit()
	
def exit():
    window.destroy()
#输入账号
l1 = tk.Label(window,text='User-name:',font=('Arial',12),width=20)
l1.place(x=85,y=127,anchor='n')
e1 = tk.Entry(window,font=('Arial',10),width=20)
e1.place(x=200,y=130,anchor='n')
#输入密码
l2 = tk.Label(window,text='User-password:',font=('Arial',12),width=20)
l2.place(x=70,y=152,anchor='n')
e2 = tk.Entry(window,font=('Arial',10),width=20)
e2.place(x=200,y=155,anchor='n')
#登录按钮
b1 = tk.Button(window,text='Login In',width=10,command=find_Users)
b1.place(x=150,y=180,anchor='n')
#退出按钮
b2 = tk.Button(window,text='Exit',width=20,command=exit)
b2.place(x=200,y=210,anchor='n')
#注册按钮
b3 = tk.Button(window,text='Sign Up',width=10,command=sign_up_winow)
b3.place(x=250,y=180,anchor='n')

window.mainloop()

你可能感兴趣的:(python,开发语言)