Re: Callback functions arguments

2014-10-28 Thread ast


Peter Otten __pete...@web.de a écrit dans le message de 
news:mailman.15231.1414399974.18130.python-l...@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 


--
https://mail.python.org/mailman/listinfo/python-list


Re: Callback functions arguments

2014-10-28 Thread Chris Angelico
On Tue, Oct 28, 2014 at 6:35 PM, ast nom...@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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Callback functions arguments

2014-10-28 Thread ast


Chris Angelico ros...@gmail.com a écrit dans le message de 
news:mailman.15254.1414482690.18130.python-l...@python.org...

On Tue, Oct 28, 2014 at 6:35 PM, ast nom...@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.


--
https://mail.python.org/mailman/listinfo/python-list


Callback functions arguments

2014-10-27 Thread ast

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

--
https://mail.python.org/mailman/listinfo/python-list


Re: Callback functions arguments

2014-10-27 Thread Peter Otten
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.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Callback functions arguments

2014-10-27 Thread Jean-Michel Pichavant
- Original Message -
 From: ast nom...@invalid.com
 To: python-list@python.org
 Sent: Monday, 27 October, 2014 9:16:26 AM
 Subject: Callback functions arguments
 
 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

The Scale object is performing the call, hence it will be the Scale object that 
will call your maj function with a nouvelleValeur parameter.

When you write command=maj, you pass the function, but you don't call it. 
That's the purpose of a callback. You provide a function and it get called by 
the object you've been giving the function to. The Scale object should be 
documented and should provide with the callback signature.

See http://effbot.org/zone/tkinter-callbacks.htm

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list