一、课件

二、习题

三、答案(2020年7月29日更新)

依赖库接昨天的函数

import tkinter as tk
import tkinter.messagebox
import threading
from tkinter import filedialog
from tk.answer import *
from PIL import ImageTk

window = tk.Tk()
# 初始人脸图片
imgPath = 'img/recong.gif'
# 窗口标题
window.title('人脸识别系统')

# 创建一个主frame,长在主window窗口上
frame = tk.Frame(window)
frame.pack()
# 主要frame
frame_l = tk.Frame(frame)
frame_r = tk.Frame(frame)
frame_l.pack(side='left', fill="both")
frame_r.pack(side='right', fill="both")


def showmodle(content):
    tkinter.messagebox.showinfo(title='提示', message=content)


# 1、增加1个Label、1个Entry控件、1个按钮,实现从界面输入LFW目录,再单击按钮调用扫描LFW目录的函数
def scan_lfw_path():
    r = tk.Label(frame_r, text='请输入人脸目录', bg='blue', font=('Arial', 12), width=30, height=2)
    r.pack()

    def load_lfw():
        if input1.get() == "":
            showmodle("目录不能为空")
        else:
            threading.Thread(target=insert_face_lfw, args=(input1.get(),)).start()
            showmodle("后台开始扫描…………")

    input1 = tk.Entry(frame_r, show=None, font=('Arial', 14))
    input1.pack()
    btn1 = tk.Button(frame_r, text='加载', font=('Arial', 12), width=10, height=1, command=load_lfw)
    btn1.pack()


# 2、增加1个按钮,单击此按钮,从本地文件夹选择一个人脸图片,将人脸记录的插入数据库(图片文件名为人名)
def insert_btn_img_to_db():
    def insert_img():
        file_path = filedialog.askopenfilename()
        print(file_path)
        if file_path == "":
            showmodle("请选择图片")
        else:
            insert_face_data(file_path.split("/")[-1].split(".")[0], file_path)
            showmodle("插入数据成功")

    btn2 = tk.Button(frame_r, text='插入数据库', font=('Arial', 12), width=10, height=1, command=insert_img)
    btn2.pack()


# 3、增加1个按钮,1个Entry和1个Label控件,单击此按钮,从数据库中删除对应的人脸记录(图片文件名为人名)
def delete_face_by_name():
    def delete_face():
        if input2.get() == "":
            showmodle("输入不能为空")
        else:
            status = delete_face_data_by_name(input2.get())
            if status == 0:
                showmodle("没有相关数据")
            else:
                showmodle("删除成功")

    label2 = tk.Label(frame_r, text='删除人脸记录', bg='blue', font=('Arial', 12), width=30, height=2)
    label2.pack()
    input2 = tk.Entry(frame_r, show=None, font=('Arial', 14))  # 显示成明文形式
    input2.pack()
    btn3 = tk.Button(frame_r, text='删除', font=('Arial', 12), width=10, height=1, command=delete_face)
    btn3.pack()


# 4、增加1个按钮,1个Entry和1个Label控件,实现输入一个人名 从数据库查询照片的功能,显示到Label上
def show_face_to_windiw():
    def query_by_name(label_show):
        global personPath
        global imgPath
        if input3.get() == '':
            showmodle("没有数据")
        else:
            res = query_face_data_by_name(input3.get())
            if res != []:
                imgPath2 = res[0]['data']["img_path"]
                personPath = imgPath2
                photos = ImageTk.PhotoImage(file=personPath)
                label_show.configure(image=photos)
                label_show.pack()
                label_show.image = photos
            else:
                showmodle("没有数据")

    label3 = tk.Label(frame_r, text='查询数据库', bg='blue', font=('Arial', 12), width=30, height=2)
    label3.pack()
    input3 = tk.Entry(frame_r, show=None, font=('Arial', 14))  # 显示成明文形式
    input3.pack()
    btn4 = tk.Button(frame_r, text='加载', font=('Arial', 12), width=10, height=1,
                     command=lambda: query_by_name(label_show))
    btn4.pack()

    personPath = imgPath
    print(imgPath)
    photo = ImageTk.PhotoImage(file=personPath)
    label_show = tk.Label(frame_r, imag=photo)
    label_show.pack()
    label_show.image = photo


# 5、增加1个按钮和一个Label,从本地文件夹选择一个人脸图片,实现单击按钮调用人脸识别身份验证的功能,
#    通过MessageBox提示是否为合法用户
def face_recong():
    def face_reco():
        file_path = filedialog.askopenfilename()
        res = face_database(file_path.split("/")[-1].split(".")[0])
        if res == 1:
            showmodle("合法用户")
        else:
            showmodle("不合法用户")

    label3 = tk.Label(frame_r, text='人脸识别', bg='blue', font=('Arial', 12), width=30, height=2)
    label3.pack()

    btn4 = tk.Button(frame_r, text='识别', font=('Arial', 12), width=10, height=1, command=face_reco)
    btn4.pack()


if __name__ == "__main__":
    scan_lfw_path()
    insert_btn_img_to_db()
    delete_face_by_name()
    show_face_to_windiw()
    face_recong()

    # 主窗口循环显示
    window.mainloop()

四、tkinter相关资料

https://www.cnblogs.com/shwee/p/9427975.html

五、程序动态更新lebel图片

参考:https://stackoverflow.com/questions/3482081/how-to-update-the-image-of-a-tkinter-label-widget

import Tkinter as tk
import ImageTk

root = tk.Tk()

img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")

def callback(e):
    img2 = ImageTk.PhotoImage(Image.open(path2))
    panel.configure(image=img2)
    panel.image = img2

root.bind("<Return>", callback)
root.mainloop()

六、command传参方式

import tkinter as tk

def bn_cmd(str=None):
    if str is not None:
        lb.config(text=str)

root = tk.Tk()
root.geometry('200x100')
lb = tk.Label(root, text='12345')
lb.grid()
tstr = 'I\'m from Button command'
bn = tk.Button(root, text='clickme', command=lambda:bn_cmd(tstr))
bn.grid()
root.mainloop()
# 主要用lambda表达式

欢迎欢迎~热烈欢迎~