python-tkinter学习

Posted Python_Heaven

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-tkinter学习相关的知识,希望对你有一定的参考价值。

13登陆验证的例子:

创建窗口,
上面为图片,
2个label,2个entry输入,用户名,密码
2个button,登陆和注册,定义了2个函数
点击登录后,进行验证,
点击注册,进行注册界面,包含3个label,3个entry
使用了pickle库,具体可以自行了解

import pickle
import tkinter as tk
from tkinter import messagebox

window = tk.Tk()
window.title("my login window")
window.geometry("500x400")

# welcome image
canvas = tk.Canvas(window, height=200, width=500)
image_file = tk.PhotoImage(file='welcome.gif')
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
canvas.pack(side='top')

# user information
tk.Label(window, text='User name:').place(x=100, y=200, anchor='nw')
tk.Label(window, text='Password:').place(x=100, y=250, anchor='nw')

# 输入用户名
var_usr_name = tk.StringVar()
var_usr_name.set('example@python.com')  # 设置默认值
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)  # 获取输入的用户名
entry_usr_name.place(x=200, y=200)
# 输入密码
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')  # 获取输入密码,显示为*
entry_usr_pwd.place(x=200, y=250)


# login and sign up button
def usr_login():
    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]:
            messagebox.showinfo(title='Welcome', message='How are you?' + usr_name)
        else:
            messagebox.showerror(message='Error,your password is wrong!,try again.')
    else:
        is_sign_up = messagebox.askyesno(title='Welcome', message='You have not sign up yet. Sign up today?')
        if is_sign_up:
            usr_sign_up()


def usr_sign_up():
    # 3个label,3个entry

    def sign_to_my_window():
        np = new_pwd.get()
        npf = new_pwd_confirm.get()
        nn = new_name.get()
        with open('usrs_info.pickle', 'rb') as usr_file:
            exist_usr_info = pickle.load(usr_file)
        if np != npf:
            messagebox.showerror('Error', 'Password and confirm password must be the same!')
        elif nn in exist_usr_info:
            messagebox.showerror('Error', 'The usr 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)
            messagebox.showinfo('Welcome', 'You have successfully signed up!')
            window_sign_up.destroy()  # 关闭窗口

    window_sign_up = tk.Toplevel(window)
    window_sign_up.geometry('350x200')
    window_sign_up.title('Sign up window')
    # 输入用户名
    new_name = tk.StringVar()
    new_name.set('example@python.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='Confirm Password:').place(x=10, y=90)
    entry_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')  # 获取输入密码,显示为*
    entry_pwd_confirm.place(x=150, y=90)

    comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_my_window)
    comfirm_sign_up.place(x=150, y=130)


btn_login = tk.Button(window, text='Login', command=usr_login)
btn_login.place(x=200, y=280)
btn_sign_up = tk.Button(window, text='sign up', command=usr_sign_up)
btn_sign_up.place(x=280, y=280)

window.mainloop()

主页运行如图:welcome图片可以自己百度下载

注册界面如图:

验证报错如图:

初学tkinter就这些了。希望能多做些工具,在实际工作中提升工作效率。

以上是关于python-tkinter学习的主要内容,如果未能解决你的问题,请参考以下文章

python-tkinter学习

python-tkinter学习

python-tkinter学习

python-tkinter学习

python-tkinter学习

python-tkinter学习