user1840752
user1840752

Reputation: 63

Entry string not being added to listbox

Could anyone help me debug this? The listbox isn't updating, and I'm not sure if the entry text (ment) is even transferring to the method.

def NewTask():
    ment = StringVar()
    top = Toplevel()
    top.title("Add New Task")
    top.minsize(300,300)
    top.maxsize(300,300)

    label_newtask = Label(top, text = "Entry New Task:", font = ("Purisa",20))
    label_newtask.pack()


    button_newtask = Button(top, text="Enter", command= NewTaskCount)
    button_newtask.pack()

    entry_newtask = Entry(top, textvariable=ment)
    entry_newtask.pack()



def NewTaskCount():
    ment = StringVar()
    mtext = ment.get()
    listbox.insert(END, mtext)
    return

Upvotes: 1

Views: 62

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385880

Your problem is that your stringvar ment is a local variable that is only visible within the scope of NewTask. In NewTaskCount, you are creating a new StringVar -- which is initially blank -- and immediately getting the value of that new variable. You need to make it a global variable, or use an object-oriented approach so that you can use an instance variable.

Upvotes: 2

Related Questions