Python Forum

Full Version: tkinter only storing last element/data from entry widget
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I'm new here, and in desperate need of help! I don't have much experience with tkinter, so I may have botched something along the way. Here's my issue:

My application makes and API call to get due dates for assignments from our learning management system. The user is then asked to update the due date with NEW dates. I need these new dates to be stored in a list, but my code seems to only store the last date in the list. Here's part of my code.


assignmentName = tk.LabelFrame(content_frame)
assignmentName.pack(side="left")
dateDue = tk.LabelFrame(content_frame)
dateDue.pack(side="left")
timeDue = tk.LabelFrame(content_frame)
timeDue.pack(side="left")

 def button_clicked():
    API_URL = [MY URL]
    API_KEY = [MY TOKEN]
    canvas_api = Canvas(API_URL, API_KEY)
    courseID = uinput.get()
    course = canvas_api.get_course(courseID)
    global dueDate
    dueDate = course.get_course_level_assignment_data()
    due_dates_text = ""
    
    for stuff in dueDate:
        entry = tk.Entry(assignmentName)
        global entry1
        entry1 = tk.Entry(dateDue)
        global entry2
        entry2 = tk.Entry(timeDue)
        entry.insert(0, f"{stuff['title']}") #Prints out the assignment names
        entry.pack()

        ### If statements to find the due date and time of the assignments and prints it out
        if stuff['due_at'] == None:
            entry1.insert(0, "No Due Date")
            entry2.insert(0, "No Due Date")
            entry1.pack()
            entry2.pack()
        elif stuff['due_at']:
            entry1.insert(0, f"{stuff['due_at'].split('T')[0]}")            
            entry2.insert(0, f"{stuff['due_at'].split('T')[1]}")
        entry1.pack()
        entry2.pack()

def pushDueDate():
    API_URL = [MY URL]
    API_KEY = [MY TOKEN]
    canvas = Canvas(API_URL, API_KEY)
    course = canvas.get_course(uinput.get())
    assignment = course.get_assignments()
    
    date = []
    time = []

    for x in dueDate:
        x = entry1.get()
        date.append(x)
    print(date)

    for i in assignment:
        update = i.edit(assignment={'due_at': date + 'T' + time + 'Z'})
        update
        print(date + ' ' + time)
        print(update)
I think this is the problem
    for stuff in dueDate:
        entry = tk.Entry(assignmentName)
        global entry1
        entry1 = tk.Entry(dateDue)
        global entry2
        entry2 = tk.Entry(timeDue)
global entry1 and global entry2 indicate that you want entry1 and entry2 to be global variables. Why? If there is multiple stuff in dueDate, entry1 and entry2 are reassigned each time though the loop. You might create 10 entry1 and 10 entry2, but the global variable only references the last.
This has nothing to do with tkinter. You would have the same problem with any variable that you assign in a loop. If a loop produces multiple results, and you want all the results, save them in a list.
(Yesterday, 02:24 AM)deanhystad Wrote: [ -> ]I think this is the problem

global entry1 and global entry2 indicate that you want entry1 and entry2 to be global variables. Why? If there is multiple stuff in dueDate, entry1 and entry2 are reassigned each time though the loop. You might create 10 entry1 and 10 entry2, but the global variable only references the last.
This has nothing to do with tkinter. You would have the same problem with any variable that you assign in a loop. If a loop produces multiple results, and you want all the results, save them in a list.

I have them as global variables because I initialized entry1 and entry2 variables to pull in data, but then my next button's function gets the updated data. So I made them global so I can call them outside that function. Hopefully that makes sense? Should I do something different?
In your code entry1 can only reference one Entry, the last one. If there are multiple entry1's, you need to keep them in a list.
global entry1, entry2
entry1 = []
entry2 = []
for stuff in dueDate:
    entry1.append(tk.Entry(dateDue))
    entry2.append(tk.Entry(timeDue))