Python TKinter文本小部件中的get clicked标记
我在一个文本小部件中有一些标签,并为它们绑定了一个单击功能 我的例句是“我可爱的小猫”。“可爱”和“小”是带标签的单词 在这个click函数中,我无法找到获取我单击的字符串的方法。当我点击可爱时,我想把可爱打印到控制台上 这就是我到目前为止所做的,我没有包括如何应用标记,因为这是有效的。正确调用了click函数Python TKinter文本小部件中的get clicked标记,python,tkinter,Python,Tkinter,我在一个文本小部件中有一些标签,并为它们绑定了一个单击功能 我的例句是“我可爱的小猫”。“可爱”和“小”是带标签的单词 在这个click函数中,我无法找到获取我单击的字符串的方法。当我点击可爱时,我想把可爱打印到控制台上 这就是我到目前为止所做的,我没有包括如何应用标记,因为这是有效的。正确调用了click函数 def __init__(self, master): # skipped some stuff here self.MT.tag_config(
def __init__(self, master):
# skipped some stuff here
self.MT.tag_config('adj', foreground='orange')
# here i bind the click function
self.MT.tag_bind('adj', '<Button-1>', self.click)
def click(self, event):
print(dir(event))
# i want to print the clicked tag text here
def\uuuuu init\uuuuuuuuuu(自,主):
#跳过了一些东西
self.MT.tag_配置('adj',前台='orange')
#这里我绑定了click函数
self.MT.tag_bind('adj','',self.click)
def单击(自身,事件):
印刷(主任(活动))
#我想在这里打印单击的标记文本
有办法做到这一点吗
最好的,
Michael我设法从光标位置提取单击标签的文本。我将其转换为索引,并检查覆盖该索引的标记 以下是我的解决方案:
def click(self, event):
# get the index of the mouse click
index = self.MT.index("@%s,%s" % (event.x, event.y))
# get the indices of all "adj" tags
tag_indices = list(self.MT.tag_ranges('adj'))
# iterate them pairwise (start and end index)
for start, end in zip(tag_indices[0::2], tag_indices[1::2]):
# check if the tag matches the mouse click index
if self.MT.compare(start, '<=', index) and self.MT.compare(index, '<', end):
# return string between tag start and end
return (start, end, self.MT.get(start, end))
def单击(自身,事件):
#获取鼠标单击的索引
index=self.MT.index(“@%s,%s”%(event.x,event.y))
#获取所有“adj”标记的索引
标签索引=列表(self.MT.tag标签范围('adj'))
#两两迭代(开始索引和结束索引)
对于开始,以zip结尾(标记索引[0::2],标记索引[1::2]):
#检查标记是否与鼠标单击索引匹配
如果自我MT比较(开始,'