tkinter的简单应用

from tkinter import Tk,messagebox,Toplevel

import tkinter as tk

my_window=Tk()

my_window.title("登陆")

screen_width,screen_height=my_window.maxsize()

width=250
height=200

align_str="%dx%d+%d+%d" % (width,height,(screen_width-width)/2,(screen_height-height)/2)

my_window.geometry(align_str)

my_window.resizable(width=False, height=False)

#添加标签
user_name_label=tk.Label(my_window,text="账号",font=("Fangsong",14))
user_name_label.place(x=30,y=30)
user_pwd_label=tk.Label(my_window,text="密码",font=("Fangsong",14))
user_pwd_label.place(x=30,y=80)

#输入框
#账号输入
user_name_text=tk.StringVar()
user_name_text.set("输入账号")
user_name_entry=tk.Entry(my_window,textvariable=user_name_text,font=("Fangsong",14),width=15)
user_name_entry.place(x=80,y=30)
#密码输入
user_pwd_text=tk.StringVar()
user_pwd_text.set("输入密码")
user_pwd_entry=tk.Entry(my_window,textvariable=user_pwd_text,font=("Fangsong",14),width=15)
user_pwd_entry.place(x=80,y=80)

#数据读取
def read_data():
    with open("data.txt","r") as f:
        rows=f.readlines()
        user_info_dict={}
        for row in rows:
            dict_list=row.strip().split(":")
            user_info_dict[dict_list[0]]=dict_list[1]
        f.close()
        return user_info_dict

#登陆按钮时间处理回调
def user_login():
    #获取用户输入账号密码
    name=user_name_text.get()
    pwd=user_pwd_text.get()
    user_dict=read_data()
    if name != "" and pwd !="":
        if name in user_dict.keys():
            if pwd == user_dict[name]:
                messagebox.showerror(title="成功",message="欢迎"+name)
                print("ok")
            else:
                messagebox.showerror(title="错误",message="密码错误")
                # print("密码不正确")
        else:
            messagebox.showerror(title="错误",message="用户不存在")
            # print("用户不存在")
    else:
        messagebox.showerror(title="错误",message="用户名、密码不能为空")
        # print("账号、密码不能为空")

#弹出注册窗体
def pop_win():
    top=Toplevel()
    top.title("注册")
    top.geometry("250x200")
    #账号、密码、确认密码、注册按钮
    #布局方式
    tk.Label(top,text="账号",width=10).grid(row=1,column=0)
    user_name=tk.StringVar()
    tk.Entry(top,textvariable=user_name,width=15).grid(row=1,column=1)
    tk.Label(top,text="密码",width=10).grid(row=2,column=0)
    user_pwd=tk.StringVar()
    tk.Entry(top,textvariable=user_pwd,width=15).grid(row=2,column=1)
    tk.Label(top,text="确认密码",width=10).grid(row=3,column=0)
    user_confirm_pwd=tk.StringVar()
    tk.Entry(top,textvariable=user_confirm_pwd,width=15).grid(row=3,column=1)
    #注册按钮的回调函数
    def user_register():
        #获取输入框的值
        name=user_name.get()
        pwd=user_pwd.get()
        confirm_pwd=user_confirm_pwd.get()
        if pwd==confirm_pwd:
            with open("data.txt","a") as f:
                f.writelines(name+":"+pwd+"\n")
                f.flush()
                f.close()
                messagebox.showinfo(title="成功",message="注册成功")
                #销毁窗体
                top.destroy()
        else:
            messagebox.showerror(title="错误",message="两次密码不一致")
    tk.Button(top,text="注册",width=10,command=user_register).grid(row=4,columnspan=2,pady=15)

#按钮
#登陆按钮
user_login_button=tk.Button(my_window,text="登陆",font=("Fangsong",14),command=user_login)
user_login_button.place(x=30,y=130)
#注册按钮
user_reg_button=tk.Button(my_window,text="注册",font=("Fangsong",14),command=pop_win)
user_reg_button.place(x=130,y=130)

my_window.mainloop()

# print(read_data())
posted @ 2020-06-23 07:38  hank-li  阅读(239)  评论(0编辑  收藏  举报