1

sorry for what I assume is a noob question. this seems like it should be so simple. I am trying to create a list from an entry box generated by a loop.

I have two lists, one list "myLabelList" that has info such as "job Name, Project name" etc. and one empty list "myEntryLists" to capture the info from the entry.

The problem is when i print(myEntryList) it seems to display info about the entry rather than the input itself. I have a workaround but that's exactly what it is.

sorry if i have formatted this badly, its my first post.

from tkinter import *

root = Tk()
root.title("Job Information Entry")
root.geometry("400x150")
topFrame = Frame(root)
bottomFrame = Frame(root)
topFrame.grid(row=0, column=0)
bottomFrame.grid(row=1, column=0)

myLabelList = ["Enter Job Number", "Enter Project Name", "Enter Job Name", "Enter Drawing Number"]
myEntryList = []
lst = []


# this is where i seem to be having problems
def ok_button_click():
    for entry in myEntryList:       # this is my workaround
        lst.append(entry.get())     # this is my workaround
    print(myEntryList)              # this is what im getting
    print(lst)                      # this is what i want to print()


x = 0

for i in myLabelList:
    myLabel = Label(topFrame, text=i)
    myEntry = Entry(topFrame, width=50)
    myLabel.grid(row=x, sticky=E)
    myEntry.grid(row=x, column=1)
    x = x + 1
    myEntryList.append(myEntry)


# bottomFrame
okButton = Button(bottomFrame, text="OK", command=ok_button_click)
cancelButton = Button(bottomFrame, text="Cancel")
okButton.grid(row=0, column=0)
cancelButton.grid(row=0, column=1)

root.mainloop()
dhitsisco
  • 11
  • 2
  • Seems like you have solved the issue already, so what is the problem actually? – acw1668 Apr 20 '20 at 05:05
  • Don't get me wrong the code does what i want but i feel it could have been more efficient. I'm new to this and would rather try not to learn bad habits. i wanted to create a list to store the entry results, however when printing the list it returned a list containing info about the entry box rather than the input itself. my ""workaround was to create another list and extract the input from that. when i tried `myEntryList.append(myEntry.get())`it just returned the last result four times. – dhitsisco Apr 23 '20 at 12:52
  • If you use `myEntryList.append(myEntry.get())` in the for loop, then you will get four empty string because at the moment the entry boxes has nothing input. So using `myEntryList.append(myEntry)` is right and then use `print([x.get() for x in myEntryList])` in `ok_button_click()`. – acw1668 Apr 23 '20 at 15:05
  • you legend! ill squeeze that in my code later. – dhitsisco Apr 28 '20 at 17:35

1 Answers1

0

In this line you insert an Entry widget to myEntryList:

myEntryList.append(myEntry)

When you're trying to print it, it gives you a printable representation of the the Entry widgets, saved in the list.

If you want to get the input itself, just print the lst list.

EDIT:

Entry is an object. In OOP (Object-Oriented Programming), you define classes, which represent an object. Each object has properties and methods. You can read more about OOP here .

Entry widget is an example of an object. It has constructor that creates an instance of that class (Entry()), propeties like 'bg', 'fg' and methods like get().

When you insert to myEntryList an Entry widget, you insert the whole object to the list, and not the input it holds. In order to get the input, you need to use the get() method on the Entry widget.

I hope everything is clear now :)

  • hi and thanks for the reply, can I not just populate the list directly with the values? I'm sure its something I'm fundamentally not understanding (this is literally the first python i have done that isn't a tutorial) – dhitsisco Apr 19 '20 at 17:55
  • sorry I'll rephrase that, what would i append "myEntryList" with to return the input – dhitsisco Apr 19 '20 at 18:02
  • Thanks for the edit. I initially tried `myEntryList.append(myEntry.get())` however it was only returning the last result 4 times. I'm sure it comes to an issue with the loop. Thats where the work around came from. – dhitsisco Apr 20 '20 at 00:18