Sliders for Python Mode

144 views
Skip to first unread message

Peter Farrell

unread,
Dec 31, 2016, 2:16:48 PM12/31/16
to Processing Educators
Hello!

To dynamically change variables, I love using the sliders that are provided in p5.js, but they're not available for the Python mode for Processing. Since I'm working on a book using the Python mode, I figured I'd have to write a Slider class. Turns out it wasn't that hard, and I thought others might be interested in using it, too.

You have to have slider.py in the same folder as your sketch and import the Slider class at the top of the sketch:

from slider import Slider

Outside the setup function I created the slider object:

slider1 = Slider(0,20,6)

You have to tell it the range of the slider (in this case 0 to 20) and the default value, when the program first runs (in this case, 6). Inside the setup function you give it a position on the screen:

slider1.position(20,20)

Finally in the draw function you assign the value of the slider to a variable and work with it interactively. I just wanted to show it was returning and updating the value, so I just printed it out in the console:

num = slider1.value()
println(num)

You can create more than one slider and I added a label that shows the current value of the slider. So it's better than the p5.js version. :-)

Please let me know what you think! All constructive input is welcome.

Peter Farrell
San Mateo, CA
Hacking Math Class with Python
slider.py
SliderTest.pyde

a...@akb.me.uk

unread,
Jan 2, 2017, 6:41:00 AM1/2/17
to processing...@googlegroups.com
Hi Peter,

That's nice work!  I think this sketch might also be relevant? - I assume there's some relationship between the controlP5 library and p5.js though I don't know my way around either.

https://github.com/jdf/processing.py/blob/master/examples.py/ThirdParty/controlp5-demo.py

Anyway, it runs fine for me in Python Mode PDE (after a certain amount file renaming and copying controlP5.jar).

In terms of dynamically changing variables in a Python sketch - isn't it a shame there's no interactive Python console?  Ironically I know how to bring up a Python console for a Java-mode Processing sketch, but fell over trying to make this work with a Python-mode sketch.

https://forum.processing.org/one/topic/can-anyone-run-processing-sketches-in-jython.html

   Best regards,

       Antony

Hello!

from slider import Slider

slider1 = Slider(0,20,6)

slider1.position(20,20)

num = slider1.value()
println(num)

*Hacking Math Class with Python*

--
You received this message because you are subscribed to the Google Groups "Processing Educators" group.
To unsubscribe from this group and stop receiving emails from it, send an email to processing-educa...@googlegroups.com.
To post to this group, send email to processing...@googlegroups.com.
Visit this group at https://groups.google.com/group/processing-educators.
For more options, visit https://groups.google.com/d/optout.


Peter Farrell

unread,
Jan 2, 2017, 10:45:42 AM1/2/17
to Processing Educators
Hi, Anthony,

Thanks for the info about ControlP5. Come to find you can just import the library using the dropdowns in Processing: Sketch --> Import Library --> Add Library. ControlP5 is one of the options.

Much more involved than my sliders, with colors and tick marks. I just wanted my sliders to look and behave like the p5.js ones! And IMO the ControlP5 commands get ugly.

You're right, we're spoiled by being able to use Python's interactive shell, which is unique as far as the few languages I know.

Thanks again for the heads up. I'm constantly surprised by what's out there, waiting to be discovered and used.

Peter

To unsubscribe from this group and stop receiving emails from it, send an email to processing-educators+unsub...@googlegroups.com.

a...@akb.me.uk

unread,
Jan 2, 2017, 11:38:10 AM1/2/17
to processing...@googlegroups.com
Hi Peter,

Interactive consoles aren't particularly rare.  JavaScript has them - just hit F12 in pretty much any web browser to bring up its debugger, and the interactive console shouldn't be far away - I have played with running Processing.js sketches that way.  Also the R programming language (very useful for the more statistical side of math).

   Best regards,

       Antony

Hi, Anthony,

> email to processing-educa...@googlegroups.com <javascript:>.


> To post to this group, send email to processing...@googlegroups.com

> <javascript:>.

--

You received this message because you are subscribed to the Google Groups "Processing Educators" group.

To unsubscribe from this group and stop receiving emails from it, send an email to processing-educa...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages