Python Forum
tkinter only storing last element/data from entry widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter only storing last element/data from entry widget
#1
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)
Reply
#2
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.
Reply
#3
(Today, 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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  LLM data I/O - for storing notes Alkanet 0 378 Dec-19-2024, 09:08 AM
Last Post: Alkanet
  Storing data in readable place user_404_lost_and_found 4 1,482 Jul-22-2024, 06:14 AM
Last Post: Pedroski55
  how to open a popup window in tkinter with entry,label and button lunacy90 1 3,419 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 1,548 May-29-2023, 02:42 PM
Last Post: Nietzsche
Question Inserting Numerical Value to the Element in Optionlist and Printing it into Entry drbilgehanbakirhan 1 1,579 Jan-30-2023, 05:16 AM
Last Post: deanhystad
  Tkinter Entry size limit vman44 3 11,563 Dec-22-2022, 06:58 AM
Last Post: vman44
  Auto increament in Entry field. in tkinter GUI cybertooth 7 6,334 Sep-17-2021, 07:56 AM
Last Post: cybertooth
  Help with storing temp data for each day then recording min/max in app. trthskr4 3 3,475 Sep-10-2021, 10:51 PM
Last Post: trthskr4
  Problem with storing data in SQLAlchemy db marcello86 1 3,328 Aug-31-2020, 09:44 PM
Last Post: Larz60+
  data from multiple Entry widgets beevee 13 6,995 Aug-16-2020, 12:31 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020