英文:
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("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()
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, "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 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, "end")
def F2(e):
e.widget.insert(0, "(Auto)")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论