pythontkintertkinter.optionmenu

Create a dropdown to generate buttons and assign those buttons to specify file paths


I have a situation where a user has stored video data in different file paths. The number of these file paths can vary.

I've created a dropdown menu where the user can select how many file paths they want to select (e.g. 1 to 8).

When they select the appropriate number of file paths, I've created corresponding buttons with which they can select the folder for each of those paths.

Where I'm stuck is that, while the buttons will generate a folder prompt, I want to: 1. display that file path selection next to the appropriate button and 2. store the selections in a list/dictionary and use that information later.

I've successfully:

Where I'm stuck is, I can't display the selection on the GUI next to the appropriate button. And then, even when I do that, I'm not sure how to save those selections in a list/dictionary and then extract them later.

Each button should have a corresponding file path (chosen by the user)

I'm lost on how to associate the function to open the file path selection GUI in a for loop. Or if that's the best way?

When I select a single button, I also have to open the folder equal to how many cameras I've selected. I just want to have x buttons, click it once, and assign the selected folder next to it.

Here's what I've done:

import tkinter as tk
from tkinter import ttk, StringVar, filedialog, OptionMenu


def main():
    # Create a GUI to select the number of video folders

    # root window
    root = tk.Tk()
    root.geometry("1200x500")
    root.resizable(True, True)
    root.title("Assign Videos")

    class AssignVideos:
        def __init__(self, master):
            """initializes the GUI with a dropdown to select the number of video cameras.
            The selection will then create an equal number of prompts to select where the data
             for each camera is stored"""

            # Header to select the number of reference video cameras
            self.number_of_cams_selection_header = ttk.Label(
                text="Number of Videos:"
            )
            self.number_of_cams_selection_header.grid(column=0, row=0)

            # Create an option menu with which to select the number of reference video cameras
            self.select_number_of_cams = OptionMenu(
                root,
                StringVar(root, value=" "),
                *[str(num) for num in range(1, 9)],
                command=self.create_inputs,
            )
            self.select_number_of_cams.grid(column=1, row=0)

            # Define variables that are going to be used later
            self.number_of_cams = int
            self.folder_path_label = {}
            self.folder_path_button = {}
            self.folder_path = {}
            self.folder_path_selection = ttk.Label

        def create_inputs(self, _):
            """Generates the equal number of folder selection inputs for the user to select the location of the VVIDs"""
            self.number_of_cams = int(self.select_number_of_cams["text"])
            for num in range(self.number_of_cams):
                # Creates the label to select the folder path
                self.folder_path_button[num] = ttk.Button(
                    root,
                    text=f"Select the Folder for Camera # " f"{num + 1}: ",
                    command=self.select_folder,
                )
                self.folder_path_button[num].grid(column=1, row=num + 1)

        def select_folder(self):
            """Places Buttons on the screen to select the folder"""
            for num in range(self.number_of_cams):
                self.folder_path_selection = ttk.Button(root, text=filedialog.askdirectory())
                self.folder_path[num] = ttk.Label(root, text=self.folder_path_selection['text'])
                self.folder_path[num].grid(column=2, row=1)

    _ = AssignVideos(root)
    root.mainloop()


if __name__ == "__main__":
    main()

Solution

  • Of course, there is no need for a cycle. You just need to pass the number of the pressed key to select_folder the function , and use it to write the path to the dictionary and place it on the corresponding label.

    import tkinter as tk
    from tkinter import ttk, StringVar, filedialog, OptionMenu
    from functools import partial
    
    def main():
        # Create a GUI to select the number of video folders
    
        # root window
        root = tk.Tk()
        root.geometry("1200x500")
        root.resizable(True, True)
        root.title("Assign Videos")
    
        class AssignVideos:
            def __init__(self, master):
                """initializes the GUI with a dropdown to select the number of video cameras.
                The selection will then create an equal number of prompts to select where the data
                 for each camera is stored"""
    
                # Header to select the number of reference video cameras
                self.number_of_cams_selection_header = ttk.Label(
                    text="Number of Videos:"
                )
                self.number_of_cams_selection_header.grid(column=0, row=0)
    
                # Create an option menu with which to select the number of reference video cameras
                self.select_number_of_cams = OptionMenu(
                    root,
                    StringVar(root, value=" "),
                    *[str(num) for num in range(1, 9)],
                    command=self.create_inputs,
                )
                self.select_number_of_cams.grid(column=1, row=0)
    
                # Define variables that are going to be used later
                self.number_of_cams = int
                self.folder_path_label = {}
                self.folder_path_button = {}
                self.folder_path = {}
                self.folder_path_selection = {}
    
            def create_inputs(self, _):
                """Generates the equal number of folder selection inputs for the user to select the location of the VVIDs"""
                self.number_of_cams = int(self.select_number_of_cams["text"])
                for num in range(self.number_of_cams):
                    # Creates the label to select the folder path
                    self.folder_path_button[num] = ttk.Button(
                        root,
                        text=f"Select the Folder for Camera # " f"{num + 1}: ",
                        command=partial(self.select_folder, num)
                    )
                    self.folder_path_button[num].grid(column=1, row=num + 1)
    
            def select_folder(self, num):
                """Places Buttons on the screen to select the folder"""
                self.folder_path_selection[num] = filedialog.askdirectory()
                self.folder_path[num] = ttk.Label(root, text=self.folder_path_selection[num])
                self.folder_path[num].grid(column=2, row=num+1)
    
    
        _ = AssignVideos(root)
        root.mainloop()
    
    
    if __name__ == "__main__":
        main()