youtube python enty_python GUI尝鲜(但当涉猎,见往事耳)
from tkinter importmessageboximporttkinter as tkfrom tkinter import *
importpickle
window= tk.Tk() #窗口obj对象
window.title('Hello GTB') #窗口名字
window.geometry('450x300') #窗口宽度和高度
#welcome message
canvas = tk.Canvas(window,height=200,width=500)
image_file= PhotoImage(file='C:\\Users\\mu\\Desktop\\welcome.gif')
image= canvas.create_image(0,0,anchor='nw',image=image_file)
canvas.pack(side='top')
var_user_name=tk.StringVar()
var_user_name.set('guotianbao@gmail.com')
var_password=tk.StringVar()#userinfo
tk.Label(window,text='User name').place(x=50,y=150)
tk.Label(window,text='Password').place(x=50,y=190)
entry_user_name= tk.Entry(window,textvariable=var_user_name)
entry_user_name.place(x=160,y=150)
entry_password= tk.Entry(window,textvariable=var_password,show='*')
entry_password.place(x=160,y=190)#login and sign up
defuser_login():
user_name=var_user_name.get()
user_password=var_password.get()try:
with open('C:\\Users\\mu\\PycharmProjects\\mycrm\\app01\\user_info.txt','rb') as user_file:
user_info=pickle.load(user_file)exceptFileNotFoundError:
with open('user_info.txt', 'ab+') as user_file:
user_info= {'admin':'admin'}
pickle.dump(user_info,user_file)if user_name inuser_info:if user_password ==user_info[user_name]:
messagebox.showinfo(title='Welcome',message='How are you'+user_name)else:
messagebox.showerror(title='Hi man',message= 'Your password is wrong, try again.')else:
is_sign_up= messagebox.askyesno('Welcome','You have not sign up yet. Sign up now ?')ifis_sign_up:
user_sign()defuser_sign():defsign_last():
np= new_pwd.get() #注册密码
npf = new_pwd_confirm.get() #确认密码
nn = new_name.get() #注册的名字
with open('C:\\Users\\mu\\PycharmProjects\\mycrm\\app01\\user_info.txt','rb') as usr_file :
exist_usr_info=pickle.load(usr_file)if np !=npf:
tk.messagebox.showerror('Error', 'Password and confirm password must be the same!')elif nn inexist_usr_info:
tk.messagebox.showerror('Error', 'The user has already signed up!')else:
exist_usr_info[nn]=np
with open('usrs_info.txt', 'ab+') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('Welcome', 'You have successfully signed up!')
window_sign_up.destroy()
window_sign_up=tk.Toplevel()
window_sign_up.title('Sign up window')
window_sign_up.geometry('350x200')
new_name=tk.StringVar()
new_name.set('example@gmail.com')
tk.Label(window_sign_up,text='User name:').place(x=10,y=10)
entry_new_name= tk.Entry(window_sign_up,textvariable=new_name)
entry_new_name.place(x=150,y=10)
new_pwd=tk.StringVar()
tk.Label(window_sign_up, text='Password:').place(x=10, y=50)
entry_new_pwd= tk.Entry(window_sign_up, textvariable=new_pwd,show='*')
entry_new_pwd.place(x=150, y=50)
new_pwd_confirm=tk.StringVar()
tk.Label(window_sign_up, text='Password:').place(x=10, y=90)
entry_new_pwd_confirm= tk.Entry(window_sign_up, textvariable=new_pwd_confirm,show='*')
entry_new_pwd_confirm.place(x=150, y=90)
btn_comfirm_sign_up= tk.Button(window_sign_up, text='Sign up', command=sign_last)
btn_comfirm_sign_up.place(x=150, y=130)
btn_login= tk.Button(window,text='Login',command=user_login)
btn_login.place(x=160,y=230)
btn_login= tk.Button(window,text='Sign up',command=user_sign)
btn_login.place(x=250,y=230)
window.mainloop()#不断的循环循环窗口,保证我们点击窗口后,可以及时执行命令