我正在开发一个基于Python Tkinter的GUI应用程序。我想在“文本”窗口小部件上单击鼠标右键得到整个单词。我尝试过:
from tkinter import *
def get_the_word(event):
"""Prints the index of the char, that was under the mouse cursor"""
print(textbox.index("@%d,%d" % (event.x, event.y)))
root = Tk() # create the root window
textbox = Text(root) # create a Text widget
textbox.grid() # show the widget using grid geometry manager
textbox.bind("<Button-3>", get_the_word) # bind the get_the_word(event) function to the right-click
root.mainloop() # start the mainloop
但是我需要得到整个单词(或者至少是单词的开始和结束索引),而不是单词的唯一一个字符的索引。