赞
踩
#导出模块
importtkinter as tkfrom tkinter importmessageboximportpickle#定义登录的窗口、标题、大小和位置
window =tk.Tk()
window.title('Welcome to Python')
window.geometry('800x500+800+200')#window.configure(bg='pink') #设置主窗口的configure的一个参数:bg(背景颜色)
window.config(bg='pink') #等同上面的configure=config
#定义登录窗口的一块画布canvas,放置一张图片,welcome image
canvas = tk.Canvas(window, bg='black',height=140, width=550)
image_file= tk.PhotoImage(file='welcome1.gif') #默认目录,本机是home/xgj/
image = canvas.create_image(100,0, anchor='nw', image=image_file) #坐标x=0,y=0,錨定在西北角,就是左上角
canvas.pack(side='top') #pack为顶格布局
#登录窗口的界面:user information#2个标签label
tk.Label(window, bg='pink',font=10,text='User name:').place(x=50, y= 150)
tk.Label(window, bg='pink',font=10,text='Password:').place(x=50, y= 250)#定义输入框和取值函数
var_usr_name =tk.StringVar()
var_usr_name.set('example@qq.com') #初始化信息#对应的2个输入框entry
entry_usr_name = tk.Entry(window, font=10,textvariable=var_usr_name)
entry_usr_name.place(x=260, y=150)
var_usr_pwd= tk.StringVar() #初始化为空
entry_usr_pwd = tk.Entry(window,font=10, textvariable=var_usr_pwd, show='*') #密码显示*,为不可见
entry_usr_pwd.place(x=260, y=250)#登陆后的主窗口
defmain():
window=tk.Tk()
window.title('xgj main GUI')
window.geometry('1050x800+800+0')#---如果要设计main界面和窗口功能可以继续定义---
#---目前只是一个登录和注册界面的---
#---如果设计,可以继续添加---
#定义登录函数
def usr_login(): #初次登录,如果先点击这个,就不会报错,没有usrs_info.pickle这个文件
#获取值的赋值
usr_name =var_usr_name.get()
usr_pwd=var_usr_pwd.get()try: #下面使用pickle模块进行数据的管理和使用
#如果没有这个文件usrs_info.pickle,则会rb创建一个新的;如果有,则打开这个文件
#在登录窗口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 inusrs_info:if usr_pwd ==usrs_info[usr_name]:
tk.messagebox.showinfo(title='Welcome', message='How are you?' +usr_name)
window.destroy()#登录界面的窗口销毁、退出
main() #进入主界面
else:
tk.messagebox.showerror(message='Error, your password is wrong, try again.')else:
is_sign_up= tk.messagebox.askyesno('Welcome','You have not signed up yet. Sign up today?')if is_sign_up: #如果是按钮yes
usr_sign_up() #进入usr_sign_up(注册窗口)
#定义注册界面register
def usr_sign_up(): #小bug,初次登录或者注册,如果先点击这个就会报错,没有usrs_info.pickle文件
defsign_to_Python():
nn=new_name.get()
np=new_pwd.get()
npf=new_pwd_confirm.get()
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info=pickle.load(usr_file)#bug,在注册界面,由于默认usr,密码和确认密码是空格,也能注册,这是一个bug
if np==npf=='': #增加一个判断,不能是空格
tk.messagebox.showerror('Error', 'Password and confirm password must not be empty!')elif 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.pickle', 'wb') 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) #与下面的设置一样,但有一个区别,就是set()初始值可以有
#window_sign_up=tk.Tk() #与上面的设置一样,但是new_name.set('example@python.com') #初始值传不过去
window_sign_up.title('Sign up window')
window_sign_up.geometry('800x500+800+200')
new_name=tk.StringVar()#如果采取tk.Toplevel(window),则下面的初始值可以传过去,但是tk.Tk() 时,下面的初始值竟然传不过去?
new_name.set('example@qq.com') #初始值
#定义label标签和对应的entry---user name
tk.Label(window_sign_up,font=10, text='User name :').place(x=10, y= 10)
entry_new_name= tk.Entry(window_sign_up,font=10,textvariable=new_name)
entry_new_name.place(x=300, y=10)#定义label标签和对应的entry---Password
new_pwd =tk.StringVar()
tk.Label(window_sign_up, font=10,text='Password :').place(x=10, y=60)
entry_usr_pwd= tk.Entry(window_sign_up,font=10, textvariable=new_pwd, show='*')
entry_usr_pwd.place(x=300, y=60)#定义label标签和对应的entry---Confirm password
new_pwd_confirm =tk.StringVar()
tk.Label(window_sign_up, font=10,text='Confirm password:').place(x=10, y= 110)
entry_usr_pwd_confirm= tk.Entry(window_sign_up, font=10,textvariable=new_pwd_confirm, show='*')
entry_usr_pwd_confirm.place(x=300, y=110)#定义按钮---Sign up---就是注册按钮
btn_comfirm_sign_up= tk.Button(window_sign_up,font=10, text='Sign up', command=sign_to_Python)
btn_comfirm_sign_up.place(x=300, y=170)#登录界面的2个按钮---login and sign up button
btn_login = tk.Button(window,bg='yellow', font=10,text='Login', command=usr_login)
btn_login.place(x=260, y=350)
btn_sign_up= tk.Button(window, bg='yellow',font=10,text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=500, y=350)
window.mainloop()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。