关于tkinter的输入框(entry)以及FocusOut/FocusIn事件。

huangapple go评论127阅读模式
英文:

About tkinter entry, and the FocusOut/FocusIn

问题

I got the problem with FocusOut/FocusIn

I will show a part of my code below directly

win2 = tk.Tk()
win2.title("Bill")
entry0 = tk.Entry(win2, width=7)
entry0.insert(0, "(Auto)")
entry0.pack()
entry1 = tk.Entry(win2, width=7)
entry1.insert(0, "(Auto)")
entry1.pack()
entry2 = tk.Entry(win2, width=7)
entry2.insert(0, "(Auto)")
entry2.pack()
entryN = tk.Entry(win2, width=7)
entryN.insert(0, "(Auto)")
entryN.pack()

Because of the "entry" will appear too many times, I would replace it by 'N'.

And I'm doing something like this below:

def F1(e):
    x.delete(0, "end")
def F2(e):
    x.insert(0, "(Auto)")
list = [entry0, entry1, entry2] (Take three examples)
for x in list:
    x.bind("<FocusIn>", F1)
    x.bind("<FocusOut>", F2)

The purpose of the above is that when I select the input box, the box will be empty. And when I leave the box, the specific words will be filled in automatically. Fortunately, this code is very successful, if there is only one "entry" box.

Unfortunately, this code doesn't work well with multiple "entries", when I select the first input box, the first box did nothing and only the last "entry" responds, only the last box is working when I In/Out. Regardless of the number of "entries", only the last "entry" works.

I know there is another way is not run by list, and put them back to the main part. I tried, and it works as my purpose. The real situation is that the full version has hundreds of such "entries", I'm making them into a list not just for easy management, but there are many similar situations later. So if possible, do it with the list method first, unless there is no other way.

英文:

I got the problem with FocusOut/FocusIn

I will show a part of my code below directly

win2 = tk.Tk()
win2.title(&quot;Bill&quot;)
entry0 = tk.Entry(win2, width=7)
entry0.insert(0, &quot;(Auto)&quot;)
entry0.pack()
entry1 = tk.Entry(win2, width=7)
entry1.insert(0, &quot;(Auto)&quot;)
entry1.pack()
entry2 = tk.Entry(win2, width=7)
entry2.insert(0, &quot;(Auto)&quot;)
entry2.pack()
entryN = tk.Entry(win2, width=7)
entryN.insert(0, &quot;(Auto)&quot;)
entryN.pack()

Beacuse of the "entry" will appears too many times, I would replaced it by 'N'.

And I doing something like this below:

def F1(e):
    x.delete(0, &quot;end&quot;)
def F2(e):
    x.insert(0, &quot;(Auto)&quot;)
list = [entry0, entry1, entry2] (Take three examples)
for x in list:
    x.bind(&quot;&lt;FocusIn&gt;&quot;, F1)
    x.bind(&quot;&lt;FocusOut&gt;&quot;, F2)

The purpose of the above is that when I select the input box, the box will be empty. And when I leave the box, the specific words will be filled in automatically.
Fortunately, this code is very successful, if there is only one "entry" box.

Unfortunately, this code doesn't work well with multiple "entries",
when I select the first input box, the first box did nothing and only the last "entry" responds,
only the last box is working when I In/Out.
Regardless of the number of "entries", only the last "entry" works.

I know there is another way is not run by list, and put them back to main part.
I tried, and it works as my purpose.
The real situation is that the full version has hundreds of such "entries",
I making them into a list not just only easy management, there are many similar situations later.
So if possible, do it with the list method first, unless there is no other way.

答案1

得分: 1

The e parameter passed to the function is shorthand for event. It is an object representing the event that triggered the callback. One of the attributes of the object is widget. You can use that instead of x in your function.

def F1(e):
    e.widget.delete(0, "end")
def F2(e):
    e.widget.insert(0, "(Auto)")
英文:

The e parameter passed to the function is shorthand for event. It is an object representing the event that triggered the callback. One of the attributes of the object is widget. You can use that instead of x in your function.

def F1(e):
    e.widget.delete(0, &quot;end&quot;)
def F2(e):
    e.widget.insert(0, &quot;(Auto)&quot;)

huangapple
  • 本文由 发表于 2023年6月6日 11:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定