CustomTKinter-向表单添加额外的输入字段

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

我正在 Customtkinter 中创建一个程序,我希望用户一次输入一种成分 - 理想情况下,我希望有文本字段,然后他们可以选择在原始文本下方添加其他文本字段,同时保留原始文本。我不想设置固定数量的字段,因为我不会提前知道它们将输入多少个 - 如果这不可能,我有替代修复程序,但我正在寻找应该在文档中查找的位置解决我的问题。

表单的目标是添加其他元素,直到完成并且可以保存整个表单。我希望用户能够编辑表单的不同元素,然后一次性提交。

import customtkinter as ctk

ctk.set_appearance_mode("Dark")
ctk.set_default_color_theme("dark-blue")

class root(ctk.CTk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
        self.title("GenSoft")
        self.geometry("800x480")
        self.resizable()
        self.state("normal")
        self.iconbitmap()

        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure((2, 3), weight=0)
        self.grid_rowconfigure((0, 1, 2), weight=1)

    
        self.tabview = ctk.CTkTabview(self,
                             width=800,
                             height=480,
                             corner_radius=20,
                             fg_color="#ffffff",
                             segmented_button_selected_color="red",
                             segmented_button_fg_color="black",
                             segmented_button_selected_hover_color="green",
                             segmented_button_unselected_hover_color="green")
        self.tabview.pack(padx=20, pady=20)
        self.tabview.add("Home")
        self.tabview.add("New Recipe")
        self.tabview.add("Saved Recipe")
        self.tabview.tab("Home").grid_columnconfigure(0, weight=1)  # configure grid of individual tabs
        self.tabview.tab("New Recipe").grid_columnconfigure(0, weight=1)
        self.tabview.tab("Saved Recipe").grid_columnconfigure(0, weight=1)
        self.ingredientEntry = ctk.CTkEntry(self.tabview.tab("New Recipe"),
                         placeholder_text="ingredient")
        self.ingredientEntry.pack(padx=20, pady=10)



if __name__ == "__main__":
    app = root()
    app.mainloop()
    main()

目前这是非常基本的 - 我还没有找到任何我正在寻找的示例,但我觉得这是一个相当常见的已实现的功能。

python customtkinter
1个回答
0
投票

添加一个按钮,每次单击时都会创建一个新的输入字段:

class root(ctk.CTk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Your already existing code
        self.ingredientEntries = [] # The list that stores all the ingredient entries
        self.addButton = ctk.CTkButton(self.tabview.tab("New Recipe"), text="Add Ingredient", command=self.add_ingredient)
        self.addButton.pack(padx=20, pady10)
    def add_ingredient(self):
        newEntry = ctk.CTkEntry(self.tabview.tab("New Recipe"), placeholder_text="ingredient")
        newEntry.pack(padx=20, pady=10)
        self.ingredientEntries.append(newEntry) # Add the new entry to your list

希望它能如你所愿😊.

© www.soinside.com 2019 - 2024. All rights reserved.