471,602 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,602 software developers and data experts.

passing arguments from scale widget to function

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....
Aug 21 '05 #1
7 4052
On Sun, 21 Aug 2005 08:09:55 +0100, m7b52000 <oh*************@noSPAM.com>
wrote:
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
--

| Matt Hammond
| R&D Engineer, BBC Research and Development, Tadworth, Surrey, UK.
Aug 22 '05 #2
Matt Hammond wrote:
On Sun, 21 Aug 2005 08:09:55 +0100, m7b52000
<oh*************@noSPAM.com> wrote:
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

Thanks,

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

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

any other thoughts :)

Tony
Aug 22 '05 #3
On Mon, 22 Aug 2005 12:16:01 +0100, m7b52000 <tp*****@internode.on.net>
wrote:
command = lambda : Calc(a.get())
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

--

| Matt Hammond
| R&D Engineer, BBC Research and Development, Tadworth, Surrey, UK.
Aug 22 '05 #4
Matt Hammond wrote:
On Mon, 22 Aug 2005 12:16:01 +0100, m7b52000 <tp*****@internode.on.net>
wrote:
command = lambda : Calc(a.get())


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

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....
Aug 22 '05 #5
> 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
--

| Matt Hammond
| R&D Engineer, BBC Research and Development, Tadworth, Surrey, UK.
Aug 22 '05 #6
Matt Hammond wrote:
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

Have already started on the 2nd aproach. I might become a programmer
yet...............
Aug 22 '05 #7
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
Aug 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John Fouhy | last post: by
7 posts views Thread by Pavils Jurjans | last post: by
3 posts views Thread by domeceo | last post: by
1 post views Thread by Daniel Tscharnuter | last post: by
5 posts views Thread by Pelle Beckman | last post: by
reply views Thread by ch424 | last post: by
reply views Thread by WLigtenberg | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.