Now we will practice , Use tkinter Complete a simple login registration case .
# coding:utf-8
import tkinter as tk
from tkinter import messagebox
""" Log in to the case """
# database
date_base = [{
"username":"zxy", "password" :"123456"},
{
"username":"lxy", "password" :"123456"},
]
# Get the window
window = tk.Tk()
# Set the title of the window
window.title(" Login screen ")
# Set the size of the window
window.geometry("1000x500")
# Create a canvas
canvas = tk.Canvas(window, width=1000, height=200, bg="blue")
# Place the canvas
canvas.pack()
image_file = tk.PhotoImage(file="img_1.png")
# Put the picture file on the canvas
canvas.create_image(0, 0,anchor="nw", image = image_file)
# User name tag
username_l = tk.Label(window, text=" user name ", font=" Regular script ")
username_l.place(x=301, y=251)
# Password tags
password_l = tk.Label(window, text=" password ", font=" Regular script ", )
password_l.place(x=301, y=301)
var_username = tk.StringVar()
var_password = tk.StringVar()
# User name input box
username_entry = tk.Entry(window, width=30, textvariable=var_username)
username_entry.place(x=401, y=251)
# Password input box
password_entry = tk.Entry(window, width=30, textvariable=var_password, show="*")
password_entry.place(x=401, y=301)
var_register_username = tk.StringVar()
var_register_password = tk.StringVar()
var_confirm_password = tk.StringVar()
# Registration processing
def do_register():
register_username_v = var_register_username.get()
register_password_v = var_register_password.get()
confirm_password_v = var_confirm_password.get()
db_have = False
for item in date_base:
if item.get("username") == register_username_v:
db_have = True
break
# format checks , And prompt
if register_username_v == "":
messagebox.showwarning(title=" Registration failed ", message=" The username cannot be empty !")
return
elif register_password_v == "":
messagebox.showwarning(title=" Registration failed ", message=" The password cannot be empty !")
return
elif date_base == True:
messagebox.showwarning(title=" Registration failed ", message=" The user name already exists ")
elif register_password_v != confirm_password_v:
messagebox.showwarning(title=" The two passwords don't match ", message=" The passwords you entered twice are different !")
# Format verification succeeded , To register
if register_username_v != "" and register_password_v != "" and confirm_password_v != "" and register_password_v == confirm_password_v:
date_base.append({
"username": register_username_v, "password": register_password_v})
messagebox.showinfo(title=" Registered successfully ", message=f" Congratulations to the user :{
register_username_v} Registered successfully ")
# Registration page
def to_register():
# child window
sub_window = tk.Toplevel(window)
sub_window.title(" The registration screen ")
sub_window.geometry("800x400")
# User name tag
username_l = tk.Label(sub_window, text=" user name ", font=" Regular script ")
username_l.place(x=271, y=151)
# Password tags
password_l = tk.Label(sub_window, text=" password ", font=" Regular script ", )
password_l.place(x=271, y=201)
# Confirm the password label
confirm_password_l = tk.Label(sub_window, text=" Confirm the password ", font=" Regular script ", )
confirm_password_l.place(x=271, y=251)
# User name input box
username_entry = tk.Entry(sub_window, width=30, textvariable=var_register_username)
username_entry.place(x=351, y=151)
# Password input box
password_entry = tk.Entry(sub_window, width=30, textvariable=var_register_password, show="*")
password_entry.place(x=351, y=201)
# Confirm password input box
confirm_password_entry = tk.Entry(sub_window, width=30, textvariable=var_confirm_password, show="*")
confirm_password_entry.place(x=351, y=251)
# Registration button
register_button = tk.Button(sub_window, text=" register ", font=" Regular script ", command=do_register)
register_button.place(x=401, y=301)
# Log in to work
def login_job():
username_value = var_username.get()
password_value = var_password.get()
db_have = False
# First, check whether the user exists
for item in date_base:
if username_value == item.get("username"):
db_have = True
break
# Various calibrations
if username_value == "":
messagebox.showwarning(title=" The username cannot be empty ", message=" Pro - , The username cannot be empty ")
elif password_value == "":
messagebox.showwarning(title=" The password cannot be empty ", message=" Pro - , The password cannot be empty ")
elif db_have == False:
result = messagebox.askokcancel(title=" The user does not exist ", message=" The user does not exist , Pro - , Would you like to register now ?")
if result == True:
to_register()
else:
# The user name exists in the database
for i in date_base:
if username_value == i.get("username") and password_value == i.get("password"):
messagebox.showinfo(title=" Login successful ", message=f"{
username_value}, Congratulations on your successful login !")
break
elif username_value == i.get("username") and password_value != i.get("password"):
messagebox.showerror(title=" Login failed ", message=" Wrong password ")
# The login button
login_button =tk.Button(window, text=" Sign in ", font=" Regular script ", command=login_job)
login_button.place(x=401, y=351)
# Registration button
register_button = tk.Button(window, text=" register ", font=" Regular script ", command=to_register)
register_button.place(x=521, y=351)
# Loop the window
window.mainloop()