我正在尝试找到一种可靠的方法来获取 tkinter 文本小部件中的当前光标位置。
到目前为止我所拥有的是:
import tkinter as tk
def check_pos(event):
print(t.index(tk.INSERT))
root = tk.Tk()
t = tk.Text(root)
t.pack()
t.bind("<Key>", check_pos)
t.bind("<Button-1>", check_pos)
root.mainloop()
但是,这会打印上一个光标位置,而不是当前光标位置。有人知道发生了什么事吗?
提前致谢。
感谢 Bryan Oakley 通过他在评论中发布的链接为我指明了正确的方向。我选择了第三个选择,它引入了额外的绑定。工作代码如下。现在,绑定发生在类绑定之后,以便函数可以看到文本小部件中位置的更改。
import tkinter as tk
def check_pos(event):
print(t.index(tk.INSERT))
root = tk.Tk()
t = tk.Text(root)
t.pack()
t.bindtags(('Text','post-class-bindings', '.', 'all'))
t.bind_class("post-class-bindings", "<KeyPress>", check_pos)
t.bind_class("post-class-bindings", "<Button-1>", check_pos)
root.mainloop()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)