我正在写一个配色方案编辑器.对于方案的预览,我使用文本小部件,其中我插入带有相应颜色标签的文本(我以编程方式生成).
我想要的是以下行为:
现在这是我的问题:
当我单击标记文本时,将调用标记的回调.到现在为止还挺好.但是,也调用了文本小部件的回调,尽管我在标签回调方法中返回"break"(应该停止进一步的事件处理).我怎么能阻止这个?
为了说明这个具体问题,我写了这个工作示例(对于Python 2和3):
#!/usr/bin/env python
try:
from Tkinter import *
from tkMessageBox import showinfo
except ImportError:
from tkinter import *
from tkinter.messagebox import showinfo
def on_click(event, widget_origin='?'):
showinfo('Click', '"{}"" clicked'.format(widget_origin))
return 'break'
root = Tk()
text = Text(root)
text.pack()
text.insert(CURRENT, 'Some untagged text...\n')
text.bind('<Button-1>', lambda e, w='textwidget': on_click(e, w))
for i in range(5):
tag_name = 'tag_{}'.format(i)
text.tag_config(tag_name)
text.tag_bind(tag_name, '<Button-1>',
lambda e, w=tag_name: on_click(e, w))
text.insert(CURRENT, tag_name + ' ', tag_name)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!
编辑:尝试Python 2.