[Tutor] tkinter event question
Michael P. Reilly
Fri, 22 Feb 2002 08:02:05 -0500
On Fri, Feb 22, 2002 at 11:24:34PM +1100, Glen Wheeler wrote:
> I've come to a little problem with tkinter events. What I want to do, is
> catch an event and stop the default action from going through while still
> letting my action happen.
> Case in point - I have a one line text field, and when the user presses
> <return> I want a specific function to be called. That all works fine - but
> after the function has returned from it's business, a '\n' is added to the
> end of the text field making it two lines.
> I think I can force the text field to only one line, but I'd rather just
> stop the default action (add a \n to the text) from happening. I did a few
> experiments, and found out that the \n was being placed after my function
> had executed. A nice side-effect of this is that I can't just go text =
> text[:-1] to chop off the \n - it's added after my function returns!
> Alright, I just figured out how to solve it... make it an entry widget
> instead of a text one. Can't believe I didn't think of that. But, still,
> I'd liek to ask the question - how to emulate the no-newline-char-generated
> effect of the entry eidget? Does it need to be hardcoded deep into tk?
You can also use unbind_class(). Unfortunately, it affects every Text
widget, but it's about the only way to handle the 'default' effect.
There are other advantages of using Entry over Text - if you want a Tab-
advancing form, Entry widgets will be more friendly.