0

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.

Darsh. S.
  • 13
  • 3
  • 1
    It is not clear what your issue is and it is better to provide a [mre]. – acw1668 May 25 '23 at 07:14
  • How does the current behaviour differ from the expected behaviour ? From what I can see in the code you provided, the function `delete_image` is never called – rochard4u May 25 '23 at 07:14
  • The `delete_image()` function has been called further in the code, after the `refresh_file_list()` function definition. – Darsh. S. May 25 '23 at 12:19
  • the above code is supposed to display a dropdown containing the names of image files in the folder named 'KnownFaces'. Upon selecting the image and clicking delete, the image file is deleted from the folder. – Darsh. S. May 25 '23 at 12:23

1 Answers1

0

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.

Darsh. S.
  • 13
  • 3