Search code examples
pythonpython-3.xtkintertkinter-text

How to get cursor position details in tkinter Textbox


I made a text editor in it I made an status bar but a problem I did not know how to get cursor position details like notepad

Notepad image

Please help me


Solution

  • Is this what you were looking for?

    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()