How to bind all frame widgets to Enter event

2024/10/6 10:41:47

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())
Answer

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.

https://en.xdnf.cn/q/118967.html

Related Q&A

Typeerror takes no arguments [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Unable to access element within page

Form Screenshot HTML Inspect Code screenshotIm trying to access an element within a page. Cannot give out the exact page link owing to security concerns. Im writing a python program that uses selenium …

How to wtite Erlang B and Erlang C formulas in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Pip is not recognizing the install command (Windows 7, Python 3.3) [duplicate]

This question already has answers here:python3 --version shows "NameError: name python3 is not defined" [duplicate](2 answers)Closed 6 years ago.I am trying to install Python programs using P…

How do I check if 1 is always followed by a 0

In Python, I cannot find a solution as to how to determine whether there is always a 0 following a 1 somewhere in a list of numbers, to form a pair 10. It doesnt have to be direct follower.For clarity,…

Visual Studio Code debugs even when i run without debugging

i just installed VSCode and I used to work on it but now when I try to run without Debugging with Ctrl + F5. it seems to opens up python debug console and debug like below image enter image description…

Mock global function call while importing

Suppose I have a file called a.py with code likeimport mod1 mod1.a()def b():print("hi")Now if I want to mock fun b() then unittest.py while have import statement at top likefrom a import bat …

can the order of code make this program faster?

Hi this is my first post, I am learning how to write code so technically I am a newbie.I am learning python I am still at the very basics, I was getting to Know the if statement and I tried to mix it …

How can I label a node that is the initial vertex in a cycle from graph data

I need to implement an algorithm such that in a collection of unique and ordered graph edges, I can find a cyclic node. E.g. for a ->b, b->c, c->a, then a is a cyclic node and thus I want to a…

Pandas : How to drop #DIV/0! and NA values in new column in pandas dataframe?

I did some calculation and have #DIV/0! in my dataframe. How to drop these values and count further ? I followed df.dropna but dataframe still counting #DIV/0!. Please suggest.df.insert(loc=df.column…