Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79770 > unrolled thread

Callback functions arguments

Started by"ast" <nomail@invalid.com>
First post2014-10-27 09:16 +0100
Last post2014-10-28 12:14 +0100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Callback functions arguments "ast" <nomail@invalid.com> - 2014-10-27 09:16 +0100
    Re: Callback functions arguments Peter Otten <__peter__@web.de> - 2014-10-27 09:52 +0100
      Re: Callback functions arguments "ast" <nomail@invalid.com> - 2014-10-28 08:35 +0100
        Re: Callback functions arguments Chris Angelico <rosuav@gmail.com> - 2014-10-28 18:51 +1100
          Re: Callback functions arguments "ast" <nomail@invalid.com> - 2014-10-28 12:14 +0100

#79770 — Callback functions arguments

From"ast" <nomail@invalid.com>
Date2014-10-27 09:16 +0100
SubjectCallback functions arguments
Message-ID<544dff5d$0$2073$426a74cc@news.free.fr>
Hi

In this web site at example n°5
http://fsincere.free.fr/isn/python/cours_python_tkinter.php

A program is using the "Scale" widget from tkinter module.
Here is a piece of code:

Valeur = StringVar()

echelle = Scale(Mafenetre, from_=-100, to=100, resolution=10, \
orient=HORIZONTAL, length=300, width=20, label="Offset", \
tickinterval=20, variable=Valeur, command=maj)

The "maj" callback function is:

def maj(nouvelleValeur):
    print(nouvelleValeur)

When the user move the scale with the mouse, the new position
is supposed to be printed on the python shell.

The "maj" function has an argument "nouvelleValeur" but no
argument is passed through the Scale widget.

So how the hell Python knows that it has to pass parameter
"Valeur" to the "maj" function ?

thx

[toc] | [next] | [standalone]


#79772

FromPeter Otten <__peter__@web.de>
Date2014-10-27 09:52 +0100
Message-ID<mailman.15231.1414399974.18130.python-list@python.org>
In reply to#79770
ast wrote:

> Hi
> 
> In this web site at example n°5
> http://fsincere.free.fr/isn/python/cours_python_tkinter.php
> 
> A program is using the "Scale" widget from tkinter module.
> Here is a piece of code:
> 
> Valeur = StringVar()
> 
> echelle = Scale(Mafenetre, from_=-100, to=100, resolution=10, \
> orient=HORIZONTAL, length=300, width=20, label="Offset", \
> tickinterval=20, variable=Valeur, command=maj)
> 
> The "maj" callback function is:
> 
> def maj(nouvelleValeur):
>     print(nouvelleValeur)
> 
> When the user move the scale with the mouse, the new position
> is supposed to be printed on the python shell.
> 
> The "maj" function has an argument "nouvelleValeur" but no
> argument is passed through the Scale widget.
> 
> So how the hell Python knows that it has to pass parameter
> "Valeur" to the "maj" function ?

Python doesn't "know" it has to pass an argument, it just does it. Change 
the callback to

def maj():
    print("no args")

and you'll get an error. If I were to guess

> echelle = Scale(Mafenetre, from_=-100, to=100, resolution=10, \
> orient=HORIZONTAL, length=300, width=20, label="Offset", \
> tickinterval=20, variable=Valeur, command=maj)

you probably are misled by the 'command=maj' part in the above line. This 
means that the function is passed and is different from command=maj() where 
the *result* of the function is passed. 

Here's a self-contained example that may clear things up for you:

>>> def call_them(one, two):
...     one(1)
...     two(2, 3)
... 
>>> def square(a):
...     print(a, "*", a, "=", a*a)
... 
>>> def product(a, b):
...     print(a, "*", b, "=", a*b)
... 
>>> call_them(square, product)
1 * 1 = 1
2 * 3 = 6
>>> call_them(product, product)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in call_them
TypeError: product() missing 1 required positional argument: 'b'

call_them() expects that one() takes 1 argument and two() takes 2 arguments. 
If the user passes a function that expects a different number of arguments a 
TypeError is raised.

[toc] | [prev] | [next] | [standalone]


#79826

From"ast" <nomail@invalid.com>
Date2014-10-28 08:35 +0100
Message-ID<544f4729$0$2399$426a74cc@news.free.fr>
In reply to#79772
"Peter Otten" <__peter__@web.de> a écrit dans le message de 
news:mailman.15231.1414399974.18130.python-list@python.org...


Tanks for you answer

> Python doesn't "know" it has to pass an argument, it just does it. Change
> the callback to
>
> def maj():
>    print("no args")
>
> and you'll get an error. If I were to guess

Yes you are right. I got an error:

TypeError: maj() takes 0 positional arguments but 1 was given

OK, but i still find very strange the choice of Python's designers to
make the Scale behaves like that.
The position of the scale is in variable Valeur  (StringVar()) which
cold be read/write from anywhere, so it is not necessary to pass
this variable as an argument to the call back function.

If you are looking at the SpinBox widget, in example 4 in the
same web site:
http://fsincere.free.fr/isn/python/cours_python_tkinter.php

boite = Spinbox(Mafenetre,from_=0,to=10,increment=0.5, \
textvariable=Valeur,width=5,command=carre)

with the callback function "carre"

def carre():
    """ Calcul du carré """
    Resultat.set("Carré = "+str(float(Valeur.get())**2))

you can notice that carre function has no arguments. The value
in the SpinBox is catched with Valeur variable.

So SpinBox and Scale behaves differently. It is strange.


> you probably are misled by the 'command=maj' part in the above line.

no, I understood this mechanism 

[toc] | [prev] | [next] | [standalone]


#79827

FromChris Angelico <rosuav@gmail.com>
Date2014-10-28 18:51 +1100
Message-ID<mailman.15254.1414482690.18130.python-list@python.org>
In reply to#79826
On Tue, Oct 28, 2014 at 6:35 PM, ast <nomail@invalid.com> wrote:
> OK, but i still find very strange the choice of Python's designers to
> make the Scale behaves like that.

That's nothing to do with Python's design. That's all about Tkinter,
which presumably is imitating Tk. Python allows the callback function
to be called with whatever arguments the caller wishes; you can look
at the docs for the different objects to see what args they'll be
giving their callbacks.

ChrisA

[toc] | [prev] | [next] | [standalone]


#79834

From"ast" <nomail@invalid.com>
Date2014-10-28 12:14 +0100
Message-ID<544f7a9a$0$5115$426a74cc@news.free.fr>
In reply to#79827
"Chris Angelico" <rosuav@gmail.com> a écrit dans le message de 
news:mailman.15254.1414482690.18130.python-list@python.org...
> On Tue, Oct 28, 2014 at 6:35 PM, ast <nomail@invalid.com> wrote:

That's clear to me now. Spinbox and Scale widgets behave
differently, that's all.

Command on Scale widget:

A procedure to be called every time the slider is moved. This
procedure will be passed one argument, the new scale value.
If the slider is moved rapidly, you may not get a callback for
every possible position, but you'll certainly get a callback when
it settles

Command on Spinbox widget

A procedure to be called whenever the scrollbar is moved.
No argument is passed.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web