0

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

Bind
  • 25
  • 2

2 Answers2

0

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    I want display to return the entry when i press submit button inside mainloop, or any other way to get the entry when button is clicked inside mainloop, – Bind Sep 19 '21 at 06:42
0

any other way to get the entry when button is clicked inside mainloop

The function button is executed inside mainloop, so you already have the entry value inside mainloop; you can do everything you want to do with it right there.

Armali
  • 18,255
  • 14
  • 57
  • 171