Search code examples
python-3.xtkintertkinter-entry

How to create multiple entry widgets at once?


I am making a data entry form as part of a GUI using python3 and tkinter. I have created 10 data entry widgets which continue like below:

enter1 = Entry(self,textvariable = shots1)
enter1.place(x=1000, y=200)
enter2 = Entry(self, textvariable = shots2)
enter2.place(x=1000, y=250)
enter3 = Entry(self, textvariable = shots3)
enter3.place(x=1000, y=300)
enter4 = Entry(self, textvariable = shots4)
enter4.place(x=1000, y=350)

And I was wondering if there was a more efficient way - maybe using a for loop or class - in which I could create all of these together?


Solution

  • Use a loop. Widgets are no different than any other sort of object.

    entries =[]
    for i in range(5):
        entries.append(Entry(self, ...))
    

    You almost never need to use textvariables, so I recommend not using them unless you need some of their unique features. You probably should also use pack or grid rather than place. place is generally good only for very specific edge cases.