Search code examples
tkintertkinter-entrytkinter-buttonmainloop

How do I get the return from button


how do i get the return from button command also tried without lambda doesn't work

import tkinter as tk

#value = ''
def button(e,f):
    #global value
    value = e.get()
    f.destroy()
    return value

def display(root):
    value=''
    f = tk.Frame(root,width=200 ,height=200)
    f.place(x=0,y=0)
    
    e = tk.Entry(f,width=200,font=17)
    e.place(x=0,y=0)
    
    b = tk.Button(f,text="submit",command = lambda : value == button(e,f))
    b.place(x=0,y=40)

    return value

root = tk.Tk()
root.geometry("200x200")
value = display(root)
print(value)
root.mainloop()
#print(value)

#print(value) <-- prints with global but only when i close root window


Solution

  • You can’t get the return value from a function called from an event.

    If you want the print to work after mainloop exits, you will need to have the function set a global or instance variable.