TKinter is a wonderful GUI tool from python
Below is a simple but useful GUI of the learning proccess with TKinter
Code:
import tkinter as tk
import pickle
from tkinter import messagebox
window=tk.Tk()
window.title('Welcome to login site')
window.geometry('630x500')
#Canvas 画布设置
canvas = tk.Canvas(window,height=200,width=630)
image_file=tk.PhotoImage(file='1.gif')
image=canvas.create_image(0,0,anchor='nw',image=image_file)
canvas.pack(side='top')
#label 设置
tk.Label(window,text='User name:').place(x=150,y=300)
tk.Label(window,text='User password:').place(x=150,y=350)
#entry 设置
var_usr_name=tk.StringVar()
var_usr_name.set('example@163.com')
entry_user_name=tk.Entry(window,textvariable=var_usr_name)
entry_user_name.place(x=250,y=300)
var_usr_password=tk.StringVar()
entry_user_password=tk.Entry(window,textvariable=var_usr_password,show='*')
entry_user_password.place(x=250,y=350)
#login and sign up button
def user_login():
user_name=var_usr_name.get()
user_password=var_usr_password.get()
try:
with open('users_info.pickle','rb') as user_file:
user_info=pickle.load(user_file)
except FileNotFoundError:
with open('users_info.pickle','wb') as user_file:
user_info={'admin':'admin'}
pickle.dump(user_info,user_file)
if user_name in user_info:
if user_password == user_info[user_name]:
tk.messagebox.showinfo(title='welcome!',message='Login Successfully! '+user_name)
else:
tk.messagebox.showinfo(message='Wrong password')
else:
is_signup = tk.messagebox.askyesno(message='You have not sign up yet.Sign up now?')
if is_signup:
user_signup()
def user_signup():
def signup_to_db():
np=new_password.get()
npf=new_password_confirm.get()
nn=new_name.get()
with open('users_info.pickle','rb') as user_file:
exist_user_info=pickle.load(user_file)
if np != npf:
tk.messagebox.showinfo('Error','Password and confirm password must be the same')
elif nn in exist_user_info:
tk.messagebox.showerror('Error','The user has aleardy signed up!')
else:
exist_user_info[nn]=np
with open('users_info.pickle','wb') as user_file:
pickle.dump(exist_user_info,user_file)
tk.messagebox.showinfo('Welcome','You have successfully signed up!')
window_signup.destroy()
window_signup = tk.Toplevel(window)
window_signup.geometry('350x200')
window_signup.title('Sign up')
new_name = tk.StringVar()
new_name.set('example@163.com')
tk.Label(window_signup,text='User name:').place(x=10,y=10)
entry_user_name=tk.Entry(window_signup,textvariable=new_name)
entry_user_name.place(x=150,y=10)
new_password = tk.StringVar()
tk.Label(window_signup, text='Password:').place(x=10, y=50)
entry_user_password = tk.Entry(window_signup, textvariable=new_password,show='*')
entry_user_password.place(x=150, y=50)
new_password_confirm = tk.StringVar()
tk.Label(window_signup, text='Confirm Password:').place(x=10, y=90)
entry_user_password_confirm = tk.Entry(window_signup, textvariable=new_password_confirm,show='*')
entry_user_password_confirm.place(x=150, y=90)
button_confirm_signup = tk.Button(window_signup,text='Sign up',command=signup_to_db)
button_confirm_signup.place(x=150,y=130)
button_login=tk.Button(window,text='Login',command=user_login)
button_login.place(x=150,y=400)
button_signup=tk.Button(window,text='Sign up',command=user_signup)
button_signup.place(x=350,y=400)
# start!
window.mainloop()