The following is the code to remove image from the specified folder using a dropdown:-
def remove_face():
folder_path = "KnownFaces" # Replace with the actual folder path
selected_file_var = StringVar()
selected_file_var.set("Select an image")
def delete_image():
selected_file = selected_file_var.get()
print(selected_file)
if selected_file:
image_path = os.path.join(folder_path, selected_file)
# Check if the selected file exists
if os.path.exists(image_path):
os.remove(image_path)
result_label.config(text="Image deleted successfully.")
refresh_file_list()
else:
result_label.config(text="Image does not exist.")
else:
result_label.config(text="No image selected.")
We have the following variables which are defined globally and I think that is causing the problem because that value needs to be updated in the remove_face() function but removing it globally also gives error for missing argument from the OptionMenu()in the next snippet:-
selected_file_var = StringVar()
selected_file_var.set("Select an image")
The GUI components of the window are as follows(It contains the dropdown which takes selected_file_var as argument:-
file_dropdown = OptionMenu(root,selected_file_var, "Select an image")
delete_button = Button(root, text="Delete Image", command=remove_face)
result_label = Label(root, text="")
But when we run the code, the dropdown is empty and shows no options.
The dropdown is empty initially so first we need to call the refresh_file_list()
function so that the dropdown in delete_image()
gets updated and so the options in dropdown are displayed.