如何在 Tkinter 条目小部件中获取选择的开始和结束索引?
How to get the start and end index of the selection in a Tkinter entry widget?
This question asks about getting the text selected but I was wondering how you can get the indexes of the start and end of the selection. Effbot mentions ANCHOR
here 但不是您使用它的方法。 get
和 selection_get
不接受任何额外的参数,所以我不知道如何使用它。我不能使用光标的位置和选择的长度,因为它可能位于光标的任一侧,而且我不能使用 index
方法,因为可能有多个实例。有什么建议吗?
您可以使用index
方法return选择索引"sel.first"
和"sel.last"
的开始和结束的索引。
entry=tk.Entry(...)
...
selection = (entry.index("sel.first"), entry.index("sel.last"))
注意:"sel.last"
指的是选择之后的字符。此外,如果在未选择任何内容时使用这些索引,将导致抛出错误。
This question asks about getting the text selected but I was wondering how you can get the indexes of the start and end of the selection. Effbot mentions ANCHOR
here 但不是您使用它的方法。 get
和 selection_get
不接受任何额外的参数,所以我不知道如何使用它。我不能使用光标的位置和选择的长度,因为它可能位于光标的任一侧,而且我不能使用 index
方法,因为可能有多个实例。有什么建议吗?
您可以使用index
方法return选择索引"sel.first"
和"sel.last"
的开始和结束的索引。
entry=tk.Entry(...)
...
selection = (entry.index("sel.first"), entry.index("sel.last"))
注意:"sel.last"
指的是选择之后的字符。此外,如果在未选择任何内容时使用这些索引,将导致抛出错误。