0

I was working on a tkinter page that created a variable amount of Entry widgets as it was in a for loop. The code is as follows:

EntryList = ["Enter value 1", "Enter value 2", "Enter value 3", "Enter value 4"]

Now I plan on letting the user add how many entries they want. So the size of the EntryList would change based on that.

for i in EntryList:
    count = EntryList.index(i)
    Heading = Label(root, text=i)
    Heading.grid(row=count, column=0)
    myinput = Entry(root)
    myinput.grid(row=count, column=1)

This produces the UI that I desire, where there is a Text box asking what value to enter and an Entry box next to it. This is repeated x number of times specified by the user.

The user will be able to enter the values corresponding to the heading.

But I was not able to find a way to myinput.get() the values in all of the 'x' number of Entry Widgets as all of them have the same variable name due to the for loop. The best I could do was get the value of the last Entry Widget.

Is there any work around to this?

Edit: I need this in a frame and has to happen multiple times as many buttons will have similar variations of this code as their command.

I found a solution to it in stackoverflow but the program uses an app and does not get me the desired results as I do not know how I can get it to work in a frame.

Perhaps the next set of Text box and Entry Box would load after the user presses Enter? And that way I can save the value of myinput.get() into a list before the for loop continues?

Edit: Got the solution in a new post where I expressed my problem better. Sorry but I am new to a lot of stuff around here.

  • store them in a list or dictionary. For example, https://stackoverflow.com/questions/39803630/how-to-create-buttons-text-boxes-and-label-in-loop-in-tkinter/39816274#39816274 – Bryan Oakley Oct 14 '21 at 16:51
  • `def readv(): for txt in entries: print(txt) entries = [] for i in TypeList: count = TypeList.index(i) label = Label(frame1) label.grid(row=count, column=0) entry = Entry(frame1, width=8) entries.append(entry) entry.grid(row=count, column=1) btn = Button(frame1, text='GO!', command=readv) btn.grid(row=count+1, column=2)` So this is my code based on the link @bryan-oakley gave but I get the output as .!frame4.!entry .!frame4.!entry2 .!frame4.!entry3 .!frame4.!entry4 What should I do? – Karan Hathwar Oct 14 '21 at 17:19
  • 1
    Please don't put code in the comments, it's nearly impossible to read. As for "what should I do", the answer is to call the `get` method on each entry. – Bryan Oakley Oct 14 '21 at 17:30
  • I will make a new, more thorough post about this issue. I think putting them into lists or dictionaries is the right way but I do not have a clear idea about it. – Karan Hathwar Oct 14 '21 at 17:39

1 Answers1

0

I need this in a frame and has to happen multiple times as many buttons will have similar variations of this code as their command.

If you are going to have multiple groups of buttons, and each of these groups will be in a frame, you should create a custom class based on a frame. You can then store the entries in an instance attribute, and provide a method to get the values.

It might look something like this:

import tkinter as tk

class EntryFrame(tk.Frame):
    def __init__(self, parent, *prompts, **kwargs):
        super().__init__(parent, **kwargs)
        self.entries = []
        for row, prompt in enumerate(prompts):
            label = tk.Label(self, text=prompt)
            entry = tk.Entry(self)
            self.entries.append(entry)

            label.grid(row=row, column=0)
            entry.grid(row=row, column=1, sticky="ew")
        self.grid_columnconfigure(1, weight=1)

    def get_values(self):
        results = [entry.get() for entry in self.entries]
        return results

def submit():
    print(f1.get_values(), f2.get_values())


root = tk.Tk()
f1 = EntryFrame(root, "Enter value 1", "Enter value 2", "Enter value 3", bd=2, relief="groove")
f2 = EntryFrame(root, "color", "size", "weight", bd=2, relief="groove")
btn = tk.Button(root, text="Submit", command=submit)
f1.pack(side="top", fill="x")
f2.pack(side="top", fill="x")
btn.pack(side="bottom")

root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685