python 用python写一个登陆窗口

我玩一个新语言的时候都会拿这个语言写一个登陆窗口,可能是因为登陆窗口简单吧。。。

import tkinter as tk
import pickle
import tkinter.messagebox
# 基础弹窗
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)

 # 登录方法函数
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('欢迎','你已经注册成功')
            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()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,593评论 25 708
  • 我是爱的虔诚的信徒, 从山底,仰望起始 把沙粒轻轻勒进我的掌心 那些高不可攀,若有若无 在抬头低头间幻化出原形 光...
    筱棠花阅读 491评论 0 0
  • 單身狗還沒找到媳婦~~手機已經成為了TA的第二伴侶 (我居然不如你的手機!) 說你不如,還真不是冤枉你! 因為手機...
    穷树先生阅读 302评论 0 1
  • “你来的那天春天也来到” 从前徐纯怎么也没想到过,会有一个人,就像另一个她一样。 是的,她的存在就是为了证明其他3...
    北卿70阅读 559评论 0 1
  • 饭局散了,我们各自回到家。 我正准备睡觉,突然收到林巧巧的微信: “苏哥,你来我家陪我吧!这漫漫长夜,我好怕怕。”...
    苏戈的假想敌阅读 160评论 0 0