首页 > 解决方案 > 当我销毁我的 tkinter 根时,会弹出一个灰色窗口,我必须关闭它,我该如何修复它

问题描述

我的代码按我想要的方式工作,但是当我销毁我的根目录时出现问题,弹出一个灰色窗口,我还必须关闭我的其余代码才能工作和运行,这是一个小问题,我会想解决,谁能帮我解决这个小问题,因为它真的很烦我,这是我的代码:

import face_recognition
import os
import cv2
from face_recognition.api import face_locations
import tkinter as tk

used_list = []
image_list= os.listdir("Faces")
class_names = []
add_key = 0
width = '400'
height = '700'
width_int = int(width) 
height_int = int(height) 
but_height = height_int / len(image_list)
but_width = width_int / len(image_list)
lbl_add = but_height / 2
but_width = int(but_width)
but_height = int(but_height)
root = tk.Tk()
root.geometry(f"{width}x{height}")
for fle in image_list:
  def fun2(fle):
    if len(used_list) < 2:
      used_list.append(fle)
    if len(used_list) == 2:
      root.destroy()
  cur_but = tk.Button(root,width = but_width,height = height_int,bg='#8a081e',command = lambda i=fle:fun2(i))
  cur_but.place(x = 0,y = add_key)
  add_key += but_height
for thing in image_list:
  txt = tk.Label(root,text = f"{thing}",bg='#8a081e',fg = 'white')
  txt.place(x = (width_int / 2) - 30,y =lbl_add)
  lbl_add += but_height
big_text = tk.Label(root,text = '                   Pick Two Files to see if both people have matching faces',width = 100,height = 5, anchor='w')
big_text.place(x=0,y=0)
root.mainloop()
image_list= os.listdir("Faces")
add_key = 0
width = '400'
height = '800'
width_int = int(width)
height_int = int(height)
but_height = height_int / len(image_list)
but_width = width_int / len(image_list)
but_width = int(but_width)
root = tk.Tk()
root.geometry(f"{width}x{height}")
for file_name in image_list:
    tk.Button(root,text = f'{file_name}',width = but_width,height = height_int)
root.mainloop()
print()

img1 = used_list[0]
img2 = used_list[1]
def check_two_faces(image1,image2):
    class_names = []
    image1 = 'Faces/' + image1
    image2 = 'Faces/' + image2
    read_image = cv2.imread(image1)
    Steve_image = face_recognition.load_image_file(image1)
    Steve_image = cv2.cvtColor(Steve_image,cv2.COLOR_BGR2RGB)
    second_pic = face_recognition.load_image_file(image2)
    second_pic = cv2.cvtColor(second_pic,cv2.COLOR_BGR2RGB)

    face_loc = face_recognition.face_locations(Steve_image)[0]
    face_enc = face_recognition.face_encodings(Steve_image)[0]
    cv2.rectangle(Steve_image, (face_loc[3],face_loc[0]), ( face_loc[1],face_loc[2]), (0, 255, 0), 2)


    second_loc = face_recognition.face_locations(second_pic)[0]
    second_enc = face_recognition.face_encodings(second_pic)[0]
    cv2.rectangle(second_pic, (second_loc[3],second_loc[0]), ( second_loc[1],second_loc[2]), (0, 255, 0), 2)

    comparision = face_recognition.compare_faces([face_enc],second_enc)
    print()
    name = os.path.splitext(image1)[0]
    if comparision == [False]:
        print("Both pictures are different people.")
    else:
        print(f"""Both pictures are the same people.
        
        """)
check_two_faces(image1=img1,image2=img2)

标签: pythontkinter

解决方案


推荐阅读