【问题标题】:How to get cursor position details in tkinter Textbox如何在 tkinter 文本框中获取光标位置详细信息
【发布时间】:2021-07-08 06:27:43
【问题描述】:

我在里面做了一个文本编辑器我做了一个状态栏但是一个问题我不知道如何获取像记事本这样的光标位置详细信息

请帮帮我

【问题讨论】:

  • 你试过text.index('insert')吗?要获取 line 和 col,请使用 line, col = text.index('insert').split('.')

标签: python python-3.x tkinter tkinter-text


【解决方案1】:

这就是你要找的吗?

import tkinter

master = tkinter.Tk()

labelframe = tkinter.LabelFrame( master, labelanchor = 's' )
labelframe.grid( row=0, column= 0, sticky = 'nsew' )

text = tkinter.Text( labelframe, width = 80, height= 24 )
text.grid( row=0, column= 0, sticky = 'nsew' )

def rowcol( ev = None ):
    r, c = text.index( 'insert' ).split( '.' )
    labelframe[ 'text' ] = f'{r} | {c}'

text.event_add( '<<REACT>>', *( '<Motion>', '<ButtonRelease>', '<KeyPress>', '<KeyRelease>' ) )
b = text.bind( '<<REACT>>', rowcol )
rowcol( ) # get the ball rolling
text.focus()

master.mainloop()

【讨论】:

  • 你的答案是正确的,但我想将行和列分开而不是浮动
  • 那就行了@NaitikSinghal
  • 严格来说来自text.index 的值不是浮点数(虽然它看起来像一个)它是一个row.column 字符串
猜你喜欢
  • 2023-03-16
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 2014-10-15
  • 2015-11-14
  • 1970-01-01
相关资源
最近更新 更多