我玩一個新語言的時候都會拿這個語言寫一個登陸窗口毅糟,可能是因為登陸窗口簡單吧幅垮。揽乱。雷逆。
import tkinter as tk
import pickle
import tkinter.messagebox
# 基礎(chǔ)彈窗
window = tk.Tk()
window.title('Welcome')
window.geometry('450x300')
#用戶登錄界面
tk.Label(window, text='User name: ').place(x=50, y= 150)
tk.Label(window, text='Password: ').place(x=50, y= 190)
var_usr_name = tk.StringVar()
var_usr_name.set('example@qq.com')
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=150)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=190)
# 登錄方法函數(shù)
def usr_login():
# 獲得name和password
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
try:
with open('usrs_info.pickle','rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usrs_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tkinter.messagebox.showinfo(title='歡迎', message='你是弦讽? ' + usr_name)
else:
tkinter.messagebox.showinfo(message='不好意思輸入錯誤')
else:
is_sing_up=tkinter.messagebox.askyesno('第一次來請注冊吧')
if is_sing_up:
use_sign_up()
# 取消登錄的方法
def usr_sign_up():
def sign_to_Mofan_Python():
np = new_pwd.get()
npf = new_pwd_confirm.get()
nn = new_name.get()
with open('usrs_info.pickle', 'rb') as usr_file:
exits_usr_info=pickle.load(usr_file)
if np != npf:
tkinter.messagebox.showerror('密碼輸入錯誤')
elif nn in exits_usr_info:
tkinter.messagebox.showerror('用戶名輸入錯誤')
else:
exits_usr_info[nn] = np
with open('usrs_info.pickle', 'wb') as usr_file:
pickle.dump(exits_usr_info,usr_file)
tkinter.messagebox.showerror('歡迎','你已經(jīng)注冊成功')
window_sign_up.destroy()
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注冊窗口')
new_name = tk.StringVar()
new_name.set('example@qq.com')
tk.Label(window_sign_up,text='用戶名').place(x=10,y=50)
entry_new_name = tk.Entry(window_sign_up, textvariable=new_name)
entry_usr_pwd.place(x=150, y=50)
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up,text='密碼:').place(x=10,y=90)
entry_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
entry_usr_pwd.place(x=150, y=50)
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y= 90)
entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
entry_usr_pwd_confirm.place(x=150, y=90)
btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_Mofan_Python)
btn_comfirm_sign_up.place(x=150, y=130)
# 登錄窗口buttion
btn_login = tk.Button(window, text='Login', command=usr_login)
btn_login.place(x=170, y=230)
btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=270, y=230)
window.mainloop()