-1

I the following code I want to bind all frame1 items to <'Enter'> Event, but it does not work. I mean canvas.focus_set() does not take effect. How can I solve my problem?

for w in frame1.winfo_children():
    w.bind('<Enter>',canvas1.focus_set())
  • 2
    https://stackoverflow.com/questions/21225198/tkinter-command-executed-automatically-when-binding-binding-not-acting-as-exp OR https://stackoverflow.com/questions/17205852/tkinter-bind-executes-immediately-when-script-is-run – Lafexlos Nov 27 '18 at 14:40
  • Also you might want to check `bind_all` method – Lafexlos Nov 27 '18 at 14:40
  • I checked all of your solutions but the problem exists again. My question is how to bind all frame items like check buttons to the same function event. – Alireza Keshavarz Choobeh Nov 27 '18 at 14:57
  • 1
    bind method needs method itself (callback method) not its return value. – Lafexlos Nov 27 '18 at 15:00

3 Answers3

2

The comment made by Lafexlos actually sends you in the right direction. When you do

w.bind('<Enter>', canvas1.focus_set())

you call canvas1.focus_set() and use the return value of this function call (which is None) to bind to the event. This isn't what you want, because now every time the event is triggered, None is executed instead of canvas1.focus_set().

What you should do is pass a function reference to the bind function. The reference for calling canvas1.focus_set() is canvas1.focus_set. However, using

w.bind('<Enter>', canvas1.focus_set)

still doesn't work.
This is because the bind function passes an event object to the function it has been given, so it will call canvas1.focus_set(event) instead of canvas1.focus_set(). Because focus_set does not accept any arguments, this fails.

You can fix this in two ways. You could make an extra function, which does accept an event object and then calls canvas1.focus_set() without arguments, and then bind the event to this new function. The other option is to use an anonymous "lambda" function to basically do the same like

w.bind('<Enter>', lambda e: canvas1.focus_set())

This way the lambda function accepts the event object as e, but doesn't pass it to focus_set.


P.S. The <Enter> event is not the event that is triggered when you press the Enter button on your keyboard (that is <Return>). The <Enter> event is triggered whenever you move the mouse onto a widget and is accompanied by the <Leave> event for when you leave the widget with your mouse. This might be what you want, but it often leads to confusion.

fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
0

by using canvas1.bind_all which is the parent of frame1 I solved my problem. Thanks for all solutions.

-1

If there is any mistake I see you making it is likely you are not calling the write command for the Enter key. Hopefully, if you are attempting to do this on windows, you should rather use Return.

More like:

for w in frame1.winfo_children():
    w.bind('<Return>',canvas1.focus_set())
Samuel Kazeem
  • 787
  • 1
  • 8
  • 15
  • Why the downvote? does `Enter` work on your own windows machine? uh? – Samuel Kazeem Nov 27 '18 at 15:15
  • Use `Return` and rather than `focus_set()` you should bind to a function you created as stated earlier by @Lafexlos – Samuel Kazeem Nov 27 '18 at 15:43
  • `` actually is a valid event and is fired whenever you enter a widget with your mouse. You're right in that `` is the event for pressing the Enter button, but whether that is or is not what the OP wants isn't clear from the question. – fhdrsdg Nov 27 '18 at 15:46
  • by using canvas1.bind_all which is the parent of frame1 I solved my problem. Thanks for all solutions. – Alireza Keshavarz Choobeh Nov 27 '18 at 16:46