pogorman
pogorman

Reputation: 1711

how to a get text from python Tkinter entry after window has closed?

I have a script where I want it to popup a dialog to the user. Once the user enters text the window will close and the script can take the entered text an move on. I am having trouble getting the text, it is always empty. I based my code on this: Get value from Entry after root.destroy()

How can I get the text?

#2.7
from Tkinter import *

class GetUserInput(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pathVar = StringVar()
        self.path = Entry(master, bd =5)
        self.path.bind('<Return>', self.callback)
        self.path.pack(side = RIGHT)

        L1 = Label(master, text="Enter value")
        L1.pack( side = LEFT)


    def callback(*args):
        value = args[0].pathVar.get()
        print value
        args[0].master.destroy()

    def close(self):
        self.master.destroy()

if __name__ == '__main__':
    root = Tk()
    app = GetUserInput(master=root)
    app.mainloop()
    print(app.pathVar.get())

Upvotes: 0

Views: 1165

Answers (1)

Kevin
Kevin

Reputation: 76194

self.path = Entry(master, bd =5)

Try specifying the textvariable argument here. This will ensure that the StringVar will stay updated with the value of the Entry.

self.path = Entry(master, bd =5, textvariable=self.pathVar)

Upvotes: 2

Related Questions