简体   繁体   中英

How can I get the line.column index for the right-click event on Tkinter Text widget?

Say - I am working on a tk.Text widget with the following content:

this is a test text.
which has two lines.

Say I right-click on the beginning of the first "h" in the first word "which" in the second line - and I want to access its location in the tk.Text's line.column format just as follows:

rc_index = tk.Text.get_right_click_index('current') 
print(rc_index)

The output shall be:

2.1

Is there a way to do this?

Assume text is the instance of the Text widget, then you can bind "<Button-3>" (right click event of mouse in Windows) to a callback and get the index inside the callback:

def on_right_click(event):
    idx = event.widget.index("current")
    print(idx)

text.bind("<Button-3>", on_right_click)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM