Note that there are some explanatory texts on larger screens.

plurals
  1. POPython tkinter: stopping event propagation in text widgets tags
    text
    copied!<p>I'm currently writing a color scheme editor. For the preview of the scheme, I use a text widget, where I insert text with the corresponding color tags (which I generate programmatically). </p> <p>What I want is the following behaviour:</p> <ul> <li>click anywhere on the text widget where no text is: change background color</li> <li>click on text inserted with a tag: change tags corresponding foreground color</li> </ul> <p><strong>Now here's my problem:</strong></p> <p>When I click on a tagged text, the callback of the tag is called. So far so good. But then, the callback of the text widget is called as well, although I return "break" in the tags callback method (which should stop further event handling). <em>How can I stop this?</em></p> <p>To illustrate this specific problem, I wrote this working example (for Python 2 &amp; 3):</p> <pre><code>#!/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('&lt;Button-1&gt;', 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, '&lt;Button-1&gt;', lambda e, w=tag_name: on_click(e, w)) text.insert(CURRENT, tag_name + ' ', tag_name) root.mainloop() </code></pre> <p>Any help is appreciated!</p> <p><em>Edit: Tried Python 2 as well.</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload