Tcl vs. Python or Re: Question: event.widget, event.type

Chuck Meyers chuck.meyers at lmco.com
Fri Aug 18 12:57:12 EDT 2000


I prefer Python to Tcl, it has many advantages I'm sure you are aware of,
but I have come across a case where it seems that Tcl has a much cleaner
solution. I want to add a context sensative HELP to my GUIs. One way to do
this is to generate a binding to all widgets that calls a HELP procedure
passing the calling widget name. In Tcl you have the %W variable to pass
as a parameter, thus you only need to add a few lines of code and all
widgets have HELP. You need to maintain the procedure that gets called,
but when making widgets you can ignore the fact that HELP exists. In
Python it seems the best solution is to have a global variable that
contains the calling widget name, but setting it would mean you would have
to explicitly add code to each widget binding.

Another Tcl advantage is the naming convention. The %W name tells you the
parents name, so if help is not available for the widget, you could search
for help for the parent widget.

Is there a clean way to get the widget name and parents names in Python,
or does Tcl win this one?

Matthew Dixon Cowles wrote:

> On Mon, 14 Aug 2000 15:37:11 -0700, Chuck Meyers
> <chuck.meyers at lmco.com> wrote:
> >"event.widget", and "event.type" return numerical values.  How can I
> >relate the numerical value for "event.widget" to the name of the
> >widget?  How can I relate "event.type" to a name for the event?
>
> Chuck,
> event.widget isn't really a numerical value, it just prints that
> way. Fredrik Lundh's excellent Introduction to Tkinter explains that
> in the event attributes table at (wrapped for line length):
>
> http://www.pythonware.com/library/tkinter/
>   introduction/events-and-bindings.htm
>
> So you can do things like:
>
> if event.widget.widgetName=="text":
>
> or:
>
> if event.widget==self.textArea:
>
> or:
>
> event.widget.selection_clear()
>
> As for event.type, I'm sure that you could find a list or figure them
> out for yourself, but on the same page Fredrik says:
>
> For portability reasons, you should stick to char, height, width, x,
> y, x_root, y_root, and widget unless you know exactly what you're
> doing...
>
> And since I never know exactly what I'm doing <0.1 wink>, I follow his
> advice carefully. But really, I've never needed the event type in
> practice since I've always known what the event was since I specified
> it when I bound the callback to that event. Can you give us an example
> of the problem that you're trying to solve that way? There might be a
> better way to solve it.
>
> Regards,
> Matt




More information about the Python-list mailing list