赞
踩
import tkinter as tk
import tkinter.messagebox
from PIL import Image, ImageTk
import pickle
win = tk.Tk()
win.title("欢迎")
win.geometry("600x400")
使用tkinter包创建并设置win窗口大小
# 图片
image = Image.open("../连接测试/welcome.gif")
photo = ImageTk.PhotoImage(image)
label1 = tk.Label(win, image=photo)
label1.place(x=100, y=20) # 设置图片位置
使用PIL中的Image,ImageTK包完成图片元素的加载并定位
# 用户名标签
t1 = tk.Label(win, text="用户名")
t1.place(x=138, y=200) # 设置用户名标签位置
# 用户名输入框
itemE = tk.Entry(win, bd=2)
itemE.place(x=230, y=200) # 设置用户名输入框位置
default_text = "example@python.com"
itemE.insert(0, default_text)
设置用户名标签并使用绝对定位,在使用tk.Entyry设置用户名输入框并用insert给定默认值
# 密码标签
t2 = tk.Label(win, text="密码")
t2.place(x=138, y=240) # 设置密码标签位置
# 密码输入框
itemE1 = tk.Entry(win, bd=2,show='*')
itemE1.place(x=230, y=240) # 设置密码输入框位置
与用户名设置同理,但是密码使用了show用户将输入变为*保护用户隐私
我实现了用户名和密码的输入验证逻辑,提高了系统的安全性。
def usr_login(): usr_name = itemE.get() usr_pwd = itemE1.get() print(usr_name) try: with open('usrs_info.pickle', 'rb') as usr_file: print('1') usrs_info = pickle.load(usr_file) print(usrs_info) except FileNotFoundError: with open('usrs_info.pickle', 'wb') as usr_file: print('2') usrs_info = {'admin': 'admin'} pickle.dump(usrs_info, usr_file) print('ok') print('usr_name:',usr_name)
使用.get()将输入框里面的内容传给变量并打印用户名使用try与except判断用户名是否在usrs_info.pickle文件中,如果在就将用户名用pickle.load()从文件中读取字节流并反序列化为原始数据结构或对象到usrs_info并打印出来,如果文件中没有数据(即第一次运行程序或者文件被删除),会抛出FileNotFoundError异常,然后在except块中创建一个默认的管理员用户信息,并用pickle.dump()将数据序列化为字节流并写入文件到 pickle 文件中以字典的形式。
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='Welcome', message='How are you, ' + usr_name + '?')
else:
tk.messagebox.showerror(message='Error, your password is wrong, please try again.')
else:
is_sign_up = tk.messagebox.askyesno(title='Welcome', message='You have not signed up yet. Sign up today.')
if is_sign_up:
usr_sign_up()
如果用户在原字典里面并且密码相等则弹出窗口欢迎,若密码不相等则弹出你的密码不正确请重新输入,如果用户名在原字典里面找不到则弹出你是否需要注册窗口并连接注册函数
def usr_sign_up(): print('开始注册') def sign_up(): nn = itemE2.get() np = itemE3.get() npf = itemE4.get() with open('usrs_info.pickle','rb')as usr_file: exist_usr_info = pickle.load(usr_file) if np != npf: tk.messagebox.showerror('错误','第一次密码与第二次密码不匹配') 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('欢迎','你已经成功登入') up_win.destroy()
在外层函数中输入开始注册,在内层函数中分别设置三个变量分别储存用户名,新密码,与确认密码。用pickle.load(usr_file)将以前的信息反序列化到exist_usr_info以便后期我们添加新用户,如果新密码不等于确认密码则报错否则将信息序列化写入到usrs_info.pickle文件中并弹出欢迎窗口
# Add your sign-up logic here up_win = tk.Tk() up_win.title("欢迎") up_win.geometry("380x230") t3 = tk.Label(up_win, text="User name:") t3.place(x=30, y=20) itemE2 = tk.Entry(up_win, bd=2) itemE2.place(x=180, y=20) t4 = tk.Label(up_win, text="password:") t4.place(x=30, y=60) itemE3 = tk.Entry(up_win, bd=2) itemE3.place(x=180, y=60) t5 = tk.Label(up_win, text="Confirm password:") t5.place(x=30, y=100) itemE4 = tk.Entry(up_win, bd=2) itemE4.place(x=180, y=100) butt = tk.Button(up_win, text="注册",command=sign_up) butt.place(x=190, y=150) # 设置注册按钮位置 # 登录按钮 butt = tk.Button(win, text="登录", command=usr_login) butt.place(x=200, y=300) # 设置登录按钮位置 # 注册按钮 butt1 = tk.Button(win, text="注册",command=usr_sign_up) butt1.place(x=300, y=300) # 设置注册按钮位置 win.mainloop()
标签及输入框的创建与上面的相同,使用tk.Button创建按钮并设置绝对定位
import tkinter as tk import tkinter.messagebox from PIL import Image, ImageTk import pickle win = tk.Tk() win.title("欢迎") win.geometry("600x400") # 图片 image = Image.open("../连接测试/welcome.gif") photo = ImageTk.PhotoImage(image) label1 = tk.Label(win, image=photo) label1.place(x=100, y=20) # 设置图片位置 # 用户名标签 t1 = tk.Label(win, text="用户名") t1.place(x=138, y=200) # 设置用户名标签位置 # 密码标签 t2 = tk.Label(win, text="密码") t2.place(x=138, y=240) # 设置密码标签位置 # 用户名输入框 itemE = tk.Entry(win, bd=2) itemE.place(x=230, y=200) # 设置用户名输入框位置 default_text = "example@python.com" itemE.insert(0, default_text) # 密码输入框 itemE1 = tk.Entry(win, bd=2,show='*') itemE1.place(x=230, y=240) # 设置密码输入框位置 def usr_login(): usr_name = itemE.get() usr_pwd = itemE1.get() print(usr_name) try: with open('usrs_info.pickle', 'rb') as usr_file: print('1') usrs_info = pickle.load(usr_file) print(usrs_info) except FileNotFoundError: with open('usrs_info.pickle', 'wb') as usr_file: print('2') usrs_info = {'admin': 'admin'} pickle.dump(usrs_info, usr_file) print('ok') print('usr_name:',usr_name) if usr_name in usrs_info: if usr_pwd == usrs_info[usr_name]: tk.messagebox.showinfo(title='Welcome', message='How are you, ' + usr_name + '?') else: tk.messagebox.showerror(message='Error, your password is wrong, please try again.') else: is_sign_up = tk.messagebox.askyesno(title='Welcome', message='You have not signed up yet. Sign up today.') if is_sign_up: usr_sign_up() def usr_sign_up(): print('开始注册') def sign_up(): nn = itemE2.get() np = itemE3.get() npf = itemE4.get() with open('usrs_info.pickle','rb')as usr_file: exist_usr_info = pickle.load(usr_file) if np != npf: tk.messagebox.showerror('错误','第一次密码与第二次密码不匹配') 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('欢迎','你已经成功登入') up_win.destroy() # Add your sign-up logic here up_win = tk.Tk() up_win.title("欢迎") up_win.geometry("380x230") t3 = tk.Label(up_win, text="User name:") t3.place(x=30, y=20) itemE2 = tk.Entry(up_win, bd=2) itemE2.place(x=180, y=20) t4 = tk.Label(up_win, text="password:") t4.place(x=30, y=60) itemE3 = tk.Entry(up_win, bd=2) itemE3.place(x=180, y=60) t5 = tk.Label(up_win, text="Confirm password:") t5.place(x=30, y=100) itemE4 = tk.Entry(up_win, bd=2) itemE4.place(x=180, y=100) butt = tk.Button(up_win, text="注册",command=sign_up) butt.place(x=190, y=150) # 设置注册按钮位置 # 登录按钮 butt = tk.Button(win, text="登录", command=usr_login) butt.place(x=200, y=300) # 设置登录按钮位置 # 注册按钮 butt1 = tk.Button(win, text="注册",command=usr_sign_up) butt1.place(x=300, y=300) # 设置注册按钮位置 win.mainloop()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。