蟒蛇 | TkInter |在 Tkinter GUI 中添加/删除按钮/行

问题描述 投票:0回答:0

我有一个带有选项菜单的 GUI,其中有一个数字列表 (1:8)。选择一个数字后,下方将填充等量的按钮。选择这些按钮后,将在其旁边生成一个标签,其中包含文件夹位置的选择。一切都很好。

但是,我希望能够动态改变按钮的数量。当我增加按钮的数量时,会添加按钮,这正是我所期望的。但是,如果说我最初选择 8,但将其减少到 4,我不知道如何删除按钮。

我还希望能够动态地增加/减少数字。

我已经尝试使用 grid_forget()、grid_remove() 和 destroy 来尝试从网格中删除该行/按钮/网格位置,但没有任何效果。

这是我的原始代码:

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()
python tkinter button destroy tkinter.optionmenu
© www.soinside.com 2019 - 2024. All rights reserved.