making radio buttons row

This is the place for queries about ThorPy issues : installation, coding, ...
buffalo
Posts: 44
Joined: Thu Feb 25, 2016 10:34 am

making radio buttons row

Postby buffalo » Thu Feb 25, 2016 10:40 am

hi,
here is my code

Code: Select all

rads = []
for rad in range(10):
   rad = thorpy.Checker.make("Radio", type_="radio")
   rads.append(rad)

box1 = thorpy.Box.make(rads[0:10])
thorpy.store(box1, mode="h")
box1.fit_children()


I would like only just one possibility for ticking the value.
Here is another failed test:

Code: Select all

rads = [thorpy.Checker.make(("radio" + str(i) for i in range(10)), type_="radio")]
---> AttributeError: 'generator' object has no attribute 'find'

Rudolfenstein
Posts: 3
Joined: Wed Sep 02, 2015 9:59 pm

Re: making radio buttons row

Postby Rudolfenstein » Fri Feb 26, 2016 3:28 pm

Hi,

Concerning the second problem you mention:

You wrote:

Code: Select all

rads = [thorpy.Checker.make(("radio" + str(i) for i in range(10)), type_="radio")]


,but didn't you mean:

Code: Select all

rads = [thorpy.Checker.make("radio" + str(i), type_="radio") for i in range(10)]

instead ?
Indeed, "make" method needs a string argument as first argument, while here you were giving a generator (i.e : ("radio" + str(i) for i in range(10)).

buffalo
Posts: 44
Joined: Thu Feb 25, 2016 10:34 am

Re: making radio buttons row

Postby buffalo » Fri Feb 26, 2016 7:25 pm

you're right.

Still asking myself why radio button have different behaviour than in HTML, i mean multi values.

ThorPy
Site Admin
Posts: 21
Joined: Wed May 13, 2015 9:44 pm

Re: making radio buttons row

Postby ThorPy » Fri Feb 26, 2016 10:07 pm

[Edit after ThorPy 1.5.2a update]

The last version of ThorPy now includes ready-to-run structures that do the logical job for what you ask.
See this example on the site :http://thorpy.org/examples/pools.html

-------------------------------------------------------------------------
The behaviour of radio buttons (let's call it "exclusive" behaviour) is not implemented yet, but will in the next version (released soon).

For the moment, you can simply do like in this example:

Code: Select all

import thorpy

def rad_func(event):
    """Function used for making radio exclusive"""
    for radio in rads:
        if event.el is not radio:
            if radio.get_value() is True:
                radio.set_value(False)
                radio.unblit_and_reblit()

ap = thorpy.Application((300,300))
rads = [thorpy.Checker.make("radio" + str(i), type_="radio") for i in range(10)]
bck = thorpy.Background.make((220,220,255), elements=rads)
#create and add reaction that make radios exclusive
reac = thorpy.Reaction(thorpy.THORPY_EVENT, rad_func,
                       event_args={"id": thorpy.constants.EVENT_PRESS})
bck.add_reaction(reac)
thorpy.store(bck)
menu = thorpy.Menu(bck)
menu.play()
ap.quit()


Tell me if something is not clear. Please note that this example assume you understand reactions mechanisms. Tutorial about reactions can be found here : http://thorpy.org/tutorials/reactions.html (and if you explore the site, you will find many other places where reactions are used).


Return to “General ThorPy help”

Who is online

Users browsing this forum: No registered users and 3 guests