I've actually created something just like that!
class NewWindowWithNControls:
def __init__(self, number_of_controls):
self.root = Toplevel()
windowWidth = self.root.master.winfo_reqwidth()
windowHeight = self.root.master.winfo_reqheight()
# Gets both half the screen width/height and window width/height
positionRight = int(self.root.winfo_screenwidth() / 2 - windowWidth / 2)
positionDown = int(self.root.winfo_screenheight() / 2 - windowHeight / 2)
y_pad = "3"
self.rows = 0
self.root.geometry("+%s+%s" % (positionRight, positionDown - 200))
self.root.title("Plan From File")
self.root.configure(background=menu_background_color)
self.input_data_list= list()
for i in range(int(number_of_controls)):
data_input_dict = dict()
data_input_dict ['data_header_key'] = Entry(self.root)
data_input_dict ['data_header_key'].configure(background=entry_box_background,
foreground=text_color,
width=25)
data_input_dict ['data_header_key'].grid(row=self.rows, column=0, sticky='w', pady=y_pad)
data_input_dict ['data_header_key'].bind('<Control-a>', select_text)
data_input_dict ['data_header_key'].insert(0, '')
self.rows += 1
self.input_data_list.append(data_input_dict )
What it does is basically creates a dictionary for each series of controls (if you want also several text boxes in one row, if you need only you one you can skip the dictionary part and just hold it in the list) and then saves it in a list that holds all the information for all the n controls, you later access this list to take the input from the text boxes
On you parent window you create something like this:
self.number_of_controls_entry = Entry(self.root)
self.number_of_controls_entry.configure(background=entry_box_background,
foreground=text_color,
width=8)
self.number_of_controls_entry.grid(row=self.rows, column=1, sticky='e')
self.number_of_controls_entry.bind('<Control-a>', select_text)
self.number_of_controls_entry.insert(0, '')
self.rows += 1
self.apply_btn = Button(self.root)
self.apply_btn.configure(background=widget_background_color, foreground=text_color, width=10)
self.apply_btn.grid(row=self.rows, column=0, pady=15, padx=30, columnspan=2, sticky='nsew')
self.apply_btn.configure(text='''Apply''')
self.apply_btn.configure(command=lambda: self.apply_changes(self.number_of_controls_entry.get()))
def apply_changes(self, number_of_controls):
self.root.destroy()
NewWindowWithNControls(number_of_controls)
forloop. First, take input as an integer then iterate over therange(n)and create annno ofEntryin a new Tkinter window 🙂👍.input()returns a string, so you'll need to don = int(input("enter the number of text boxes"))for starts :D