I am creating a program in Customtkinter where I want to the user to input their ingredients one ingredient at a time- ideally I want to have the text field and then they have the option to add additional text fields below the original while retaining the original. I didn't want to set a fixed number of fields as I won't know in advance how many they will be inputting- if this isn't possible I have alternate fixes but I was looking for where I should be looking in the documentation to resolve my issue.
The goal is for the form to add on additional elements until they are finished and they can save the whole form. I want the user to be able to edit the different elements of the form and then submit all at once.
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()
It is very basic at the moment- I haven't managed to find any examples of what I am looking for but I feel like it is a fairly common feature that is implemented.
Add a button that creates a new entry field each time it's clicked:
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, pady=10)
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
Hopefully it works out for you as intended 😊.