0
votes

For some reason when I enter something into the entry field in this code and then hit the save button which should call to simply print the name and initials, it does not take what is currently in the entry box.

My interpretation is that when the button is clicked to create the window it is taking whatever is called from the code but is not dynamically checking the entry box, even when I click the button save (which I thought would check it). Any idea on how to do this?

This is a toplevel window and I created the main root with a function and had a button call this function to open up a new window to edit the name and initials.

def SaveChanges(name, initials, person):
    print(name)
    print(initials)


##    if person.initials in employees: del employees[person.initials]
##    employees[str(initials)] = person
##    employees[str(initials)].name = str(name)
##    
##    Save()
##
##    CreateMain()





def EditEmployee(person):


    window = Toplevel()

    l = Label(window,text='Edit Employee')
    l.grid(row=0,column=0,columnspan=2)
    nameLabel = Label(window,text='Name:')
    nameLabel.grid(row=1,column=0)

    initialsLabel = Label(window,text='Initials:')
    initialsLabel.grid(row=2,column=0)


    name = Entry(window)
    name.grid(row=1,column=1)
    name = name.get()
    print(name)


    initials = Entry(window)
    initials.grid(row=2,column=1)
    initials = initials.get()
    print(initials)


    save = Button(window, text='Save',
                  command= lambda n=name, i=initials, p=person: SaveChanges(n,i,p))
    save.grid(row=3, column=0,columnspan=2)
1
Please provide a minimal reproducible example. Above code does nothing by itself. - Nae
Is this necessary given that the question has been answered and is solved? - Ying n Yang
Nothing is necessary. However, providing a minimal reproducible example would increase the quality of your question, which would make it more likely to help others as well. - Nae
I think the answer given outlines where the confusion was, pretty quickly. - Ying n Yang

1 Answers

1
votes

The way you have it now the data is collected before the user has a chance to enter anything. You need to call .get() when the button is pushed.

save = Button(window, text='Save',
              command= lambda: SaveChanges(name.get(),initials.get(),person.get()))

This would be a lot neater if you used a class and a normal method instead of lambda.