0

A simple example without classes:

from tkinter import *    
master = Tk()

# Create this method before you create the entry
def return_entry(en):
    """Gets and prints the content of the entry"""
    content = entry.get()
    print(content)  

Label(master, text="Input: ").grid(row=0, sticky=W)

entry = Entry(master)
entry.grid(row=0, column=1)

# Connect the entry with the return button
entry.bind('<Return>', return_entry) 

mainloop()

Above was the answer to this question: Why is Tkinter Entry's get function returning nothing?

So I checked the code and it worked. However- I don't understand why there is an 'en' in the 'return_entry' brackets. It isn't mentioned in any other parts of the code so I think it is syntax- but for what?

I would have left a comment on said answer if I hadn't seen that OP's account has been inactive for 2 years

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
yolo
  • 127
  • 6
  • 1
    `bind` must expect that the function given to it takes a single argument; presumably an `Entry` in this case. Or are you asking what a function parameter is? – Carcigenicate Nov 25 '18 at 15:31
  • 1
    Presumably it's a typo; the parameter is named `en` but the code refers to `entry`. *"I think it is syntax"* - what do you *mean* by this? – jonrsharpe Nov 25 '18 at 15:32
  • Doesn't look like it does anything. I agree with @jonsharpe it's most likely a typo – roganjosh Nov 25 '18 at 15:32
  • ahhh. Thanks. I had a rough idea of a function parameter for example calling a function like this: A_function(y=1) A_function(y) But I never knew it could be used in that way – yolo Nov 25 '18 at 15:33
  • @jonrsharpe I mean what Carcigenicate said. Well- feel free to put your comments into an answer for me to close this – yolo Nov 25 '18 at 15:35
  • It's an argument being passed to the function but it's a typo when they try to use that value. The answer you found should be corrected I think but I don't use `Tkinter` so I'm wary of some crazy call back functionality. – roganjosh Nov 25 '18 at 15:36
  • Ya, jonsharp is probably right that it's a typo that should be `entry`. I was thinking that it was an unused parameter and that `entry` was in scope from somewhere else, but that doesn't seem to be the case. Still though, `bind` seems to take a function that takes a single argument. That argument appears to be an Entry element. It was just that the name was fubared. – Carcigenicate Nov 25 '18 at 15:49

1 Answers1

1

The callback function bound to the widget(s) by bind takes an event object as a parameter.

I amended your print statement as follows:

 print("Event Object: ", en.widget, en, "\nResult: ", content)

After typing 'Test{Return}' into the window created the following is produced:

Event Object:  .!entry <KeyPress event keysym=Return keycode=13 char='\r' x=52 y=8>
Result:  Test

The event object can be used to determine what processing is required. In your case it isn't relevant but is required by the callback mechanism. The event took place in the .!entry widget, was a KeyPress event with the other characteristics shown.

HTH

Tls Chris
  • 3,564
  • 1
  • 9
  • 24
  • It didn't need to be amended though- it worked as I expected it to. Anyway- thanks. – yolo Nov 25 '18 at 17:20