Search code examples
pythonpython-3.xtkintermessageboxtkinter-entry

Python tkinter entry() doesn't return any value to variable


I'm confused with how to get value entered in Entry() to a variable. any idea or suggestion on how to solve it.


import sys
from tkinter import *
from tkinter import messagebox

root=Tk()
root.title('Test')

e1=Entry(root)
e1.grid(row=1)

global e
e=e1.get()

def display():
    messagebox.showinfo(title="Message",message=e)

Submit=Button(root, justify=LEFT, padx=5,text="Submit",command= display).grid(row=2,sticky=W)

root.mainloop()

I want the value entered in entry box to be displayed in message box.


Solution

  • e will reference a empty string and will not change because it is set only after the Entry widget initialization.

    You need to call e1.get() to get current value of the entry widget.

    def display():
        messagebox.showinfo(title="Message", message=e1.get())