【问题标题】:In Tkinter how can I get the cursor position(line, column) from the text widget?在 Tkinter 中,如何从文本小部件获取光标位置(行、列)?
【发布时间】:2023-01-11 20:24:45
【问题描述】:
我想从文本小部件获取光标位置(行,列)并使用按钮打印它。
from tkinter import *
root=Tk()
def click():
print('line and column')#print location
button=Button(root,text="click",command=click)
button.pack()
text=Text(root)
text.pack()
root.mainloop()
【问题讨论】:
标签:
python
tkinter
text
cursor
【解决方案1】:
您调用记录在案的index 方法,为其提供索引“insert”。它将以以下形式返回一个字符串行.字符.
def click():
(line, char)= text.index().split(".")
print(f"line: {line} char: {char}")
【解决方案2】:
我们应该向 text.index 添加一个索引 id - 所以 Bryan 示例的更新版本是 (line, char) = text.index(tk.CURRENT).split(".")