【问题标题】: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(".")

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-12
        • 2011-03-14
        • 2014-03-18
        • 1970-01-01
        • 1970-01-01
        • 2019-05-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多