katek1094
katek1094

Reputation: 92

How to create a number of Labels and Entry widgets, and get data from them in Tkinter with loop

I need to create a couple of Labels and Entry fields using Tkinker, they all will be the same, only difference is a text in the label which i could have in a list. This is what the problem looks like while done in a simple way, i want to do it smarter, using some kind of loop, so i could expand it.

from tkinter import *

root = Tk()

question1 = Label(root, text="Please give data number one")
question1.grid(row=1, column=0)
field1 = Entry(root)
field1.grid(row=1, column=1)

question2 = Label(root, text="Please give data number two")
question2.grid(row=2, column=0)
field2 = Entry(root)
field2.grid(row=2, column=1)

question3 = Label(root, text="Please give data number three")
question3.grid(row=3, column=0)
field3 = Entry(root)
field3.grid(row=3, column=1)

question4 = Label(root, text="Please give data number four")
question4.grid(row=4, column=0)
field4 = Entry(root)
field4.grid(row=4, column=1)

data1 = field1.get()
data2 = field2.get()
data3 = field3.get()
data4 = field4.get()

root.mainloop()

I thought abour something like this but i don't know how to get values from Enter widgets.

from tkinter import *

root = Tk()

questions = ["Please give data number one",
"Please give data number two"
"Please give data number three"
"Please give data number four"
]

for question in enumerate(questions):
    ask = Label(root, text=question[1])
    ask.grid(row=(question[0] + 1), column=0)
    field = Entry(root)
    field.grid(row=(question[0] + 1), column=1)

root.mainloop()

Upvotes: 1

Views: 411

Answers (1)

Mahsa Hassankashi
Mahsa Hassankashi

Reputation: 2139

You need to do two things: Firstly keep a reference to the widget, and then use the get() method to get the string.

For an example:

self.entry = Entry(...)

...some code

print("the text is", self.entry.get())

Sample Get Entries:

class InputPage(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self,parent)        

        label =  Label(self, text="Please give data number four")
        label.grid(row=0, column=0, sticky ='n', columnspan =2)

        # i brought your variable in the class for example sake 

        namesInput = ["First:", "second:", "Third:", "Fourth:", "Fifth:"]

        self.entryWidgets = [] # we want to call this in another function so we assign it as self.variableName

        labelWidgets = []

        #LOOP TO CREATE WIDGETS
        for i in range(0, len(namesInput)):
            labelWidgets.append(Label(self, text = namesInput[i]))
            self.entryWidgets.append(Entry(self))
            labelWidgets[-1].grid(row= i+1, column =0, sticky='e')
            self.entryWidgets[-1].grid(row= i+1, column = 1, sticky='w')

        submit = Button(self, text = "Submit", command = self.getEntries)
        submit.grid(row = 6, column =0, columnspan =2)

    def getEntries(self):
        results = []

        for x in self.entryWidgets: # i.e for each widget in entryWidget list
            results.append(x.get())
        print(results)

Upvotes: 3

Related Questions