TKinter, buttonwidget response problem(1) and all btns the same size(2)!

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Apr 5 09:18:12 CEST 2008


On Fri, 04 Apr 2008 20:26:13 -0700, 7stud wrote:

> However, there is another way to cause a function to execute when an
> event, like a button click, occurs on a widget: you use the widget's
> bind() function:
> 
> my_button.bind('<Button-1>', someFunc)

That's a bad idea because now the `Button` doesn't act like a typical
button anymore.  This "fires" *always* and as soon as you press the mouse
button on it.  Compare this with the usual and from end users point of
view expected behavior that pressing down the mouse button "presses" the
button widget visually but doesn't cause any action until you release the
button while still over it. This for instance makes it possible to change
your mind and move the mouse cursor out of the buttons area with a pressed
mouse button and prevent the action of that button.

The preferred way to bind actions to `Button`\s is the `command` argument.

The function is called with no arguments when the button is pressed, so it
has to know all data it needs to fulfill its task.  Usually this is done
with anonymous functions and bound default values for short pieces of
"variable" data::

    def __init__(self):
        # ...
        button = Button(self,
                        text='1',
                        command=lambda n=1: self.display(n))
        # ...

    def display(self, number):
        print number

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list