passing arguments from scale widget to function

M

m7b52000

Some time ago I wrote a little program in Tcl/Tk that took the values
from 3 sliders and performed a calculation using these values. The
calculation was of course automatically repeated each time a slider was
moved.

It is proving most difficult in Python. How do I pass the .get() values
to my calculating function? Do I use the command option for each
slider? e.g command = Calc(a.get()). Obviously not cos it doesn't work.

I am not a real programmer and do not wish to get involved in classes
and objects....
 
M

Matt Hammond

It is proving most difficult in Python. How do I pass the .get() values
to my calculating function? Do I use the command option for each slider?
e.g command = Calc(a.get()). Obviously not cos it doesn't work.

It should work if you wrap it as a lambda function. As it is, you're
assigning the
result of the Calc() function to the 'command' argument/parameter of the
slider widget. What you actually want is to assign just the function:

command = lambda : Calc(a.get())
I am not a real programmer and do not wish to get involved in classes
and objects....

If you're using Tcl/Tk you're already using them ... what do you think
your Slider widget is? :)

Hope this helps!

regards


Matt
 
M

m7b52000

Matt said:
It should work if you wrap it as a lambda function. As it is, you're
assigning the
result of the Calc() function to the 'command' argument/parameter of
the slider widget. What you actually want is to assign just the function:

command = lambda : Calc(a.get())



If you're using Tcl/Tk you're already using them ... what do you think
your Slider widget is? :)

Hope this helps!

regards


Matt
Thanks,

I get the following message when I use lambda as above:

TypeError: <lambda>() takes no arguments (1 given)

any other thoughts :)

Tony
 
M

Matt Hammond

I get the following message when I use lambda as above:

TypeError: <lambda>() takes no arguments (1 given)

Oops, forgot! The Scale widget outputs a single argument - the value of
the slider. You therefore could write:

command = lambda value : Calc(value)

or even simpler:

command = Calc

In the latter case, you're supplying your "calc" function as the function
to be called. Calc takes a single argument - the value of the slider, so
it fulfills the task. Note that we're passing "Calc" (the function
itself), not "Calc(...)" (the result of calling the function)

regards


Matt
 
M

m7b52000

Matt said:
Oops, forgot! The Scale widget outputs a single argument - the value of
the slider. You therefore could write:

command = lambda value : Calc(value)

or even simpler:

command = Calc

In the latter case, you're supplying your "calc" function as the
function to be called. Calc takes a single argument - the value of the
slider, so it fulfills the task. Note that we're passing "Calc" (the
function itself), not "Calc(...)" (the result of calling the function)

regards


Matt
Ahhhh. Partial success with : command = Calc. A slider will now pass its
argument to a function without problem. My Calc function however is
expecting 3 arguments - 1 from each slider i.e moving any of the 3
sliders should cause a recalculation. I am now getting the following error:
TypeError: Calc() takes exactly 3 arguments (1 given)

p.s your help is much appreciated....
 
M

Matt Hammond

Ahhhh. Partial success with : command = Calc. A slider will now pass its
argument to a function without problem. My Calc function however is
expecting 3 arguments - 1 from each slider i.e moving any of the 3
sliders should cause a recalculation. I am now getting the following
error:
TypeError: Calc() takes exactly 3 arguments (1 given)

I'm sure you can figure this out now! Either:
use lambda functions, as described, to call Calc with the correct
arguments
or:
write three functions (one for each slider) that call Calc with all 3
arguments it requires

regards



Matt
 
M

m7b52000

Matt said:
I'm sure you can figure this out now! Either:
use lambda functions, as described, to call Calc with the correct
arguments
or:
write three functions (one for each slider) that call Calc with all 3
arguments it requires

regards



Matt
Have already started on the 2nd aproach. I might become a programmer
yet...............
 
M

m7b52000

Problem solved. use 'command = Calc' to call my Calc function but ignore
the argument passed to it. Create a list with the following elements -
a.get(), b.get() and c.get(). Every time a scale slider is moved, Calc
will retrieve the current scale values and I can do with them whatever I
want...

Now to work out how to write the answer to a text box or similar.......

Thanks for your help, Matt

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top