Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

functions in checkbox

Is it possible to change the text_color in a checkbox item?
Or is it possible to log something different from the content of the text in the checkbox?
My text is quite long. I'd prefer to log numbers for the different options in the group (as almost always in groups).
I now made a workaround, by putting numbers in the text of the checkboxes and the real text into a labels.
It would be nice to set the numbers 'white', so you don't see them anymore.

Here is my code.

var.Phase='education'
var.Stim='edu'

def edu_val():
    """Checks whether at least one field has been filled out"""
    return var.Resp in [1,2,3,4,5,6] 

edu = Form(validator=edu_val, cols=[4,1,12,4], rows=[3,1,1,1,1,1,1,1,1,3], spacing=10, margins=(100, 100, 100, 100))
labelx = Label(text=u'Höchster Bildungsabschluss', frame=False, center=False)
label1 = Label(text=u'Pflichtschule', center=False)
label2 = Label(text=u'Sekundärabschluss (mittlere Reife)', center=False)
label3 = Label(text=u'Sekundärabschluss (Lehre)', center=False)
label4 = Label(text=u'Sekundärabschluss (Matura/Abitur)', center=False)
label5 = Label(text=u'Tertiärabschluss (Akademie/Kolleg)', center=False)
label6 = Label(text=u'Tertiärabschluss (Universität/Hochschule)', center=False)

checkbox1 = Checkbox(text='1', group='education', var='Resp')
checkbox2 = Checkbox(text='2', group='education', var='Resp')
checkbox3 = Checkbox(text='3', group='education', var='Resp')
checkbox4 = Checkbox(text='4', group='education', var='Resp')
checkbox5 = Checkbox(text='5', group='education', var='Resp')
checkbox6 = Checkbox(text='6', group='education', var='Resp')
button = Button(text='Ok', frame=True, center=True)
edu.set_widget(labelx   , (2, 1))
edu.set_widget(checkbox1, (1, 2))
edu.set_widget(checkbox2, (1, 3))
edu.set_widget(checkbox3, (1, 4))
edu.set_widget(checkbox4, (1, 5))
edu.set_widget(checkbox5, (1, 6))
edu.set_widget(checkbox6, (1, 7))
edu.set_widget(label1   , (2, 2))
edu.set_widget(label2   , (2, 3))
edu.set_widget(label3   , (2, 4))
edu.set_widget(label4   , (2, 5))
edu.set_widget(label5   , (2, 6))
edu.set_widget(label6   , (2, 7))
edu.set_widget(button   , (2, 8))
edu._exec()

Comments

  • Is it possible to change the text_color in a checkbox item?

    Yes, that's quite easy because you can use css styling, like so:

    checkbox1 = Checkbox(
        text='<span style="color:red;">1</span>',
        group='education',
        var='Resp'
    )
    

    Or is it possible to log something different from the content of the text in the checkbox?

    Not really, although you can monkey-patch the CheckBox widget, such as with the code below. (Personally, I think your solution of using numbers as the checkbox text is quite ok, though.)

    def wrap_checkbox(form, checkbox, new_value):
    
        """Monkey-patches a checkbox so that it uses a custom value for
        the variable, rather than the text on the checkbox.
        """
    
        def wrap_set_var(fnc):
    
            def inner(val, var=None):
    
                orig_text = checkbox.text
                checkbox.text = new_value
                fnc(val, var)
                checkbox.text = orig_text
    
            return inner
    
        checkbox = checkbox.construct(form)
        checkbox.set_var = wrap_set_var(checkbox.set_var)
        return checkbox
    
    
    checkbox1 = Checkbox(
        text='<span style="color:red;">1</span>',
        group='education',
        var='Resp'
    )
    checkbox1 = wrap_checkbox(edu, checkbox1, new_value='this will be used for the variable')
    

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • Thank you for that solution. If you change the color, my workaround does not work anymore, because it logs var.Resp= 5

    So i used your monkey-patch. :)
    Maybe it helps someone else who wants to assess educational levels.

    var.Phase='education'
    var.Stim='edu'
    
    def edu_val():
        """Checks whether at least one field has been filled out"""
        return var.Resp in [1,2,3,4,5,6] 
    
    edu = Form(validator=edu_val, cols=[4,1,12,4], rows=[3,1,1,1,1,1,1,1,1,3], spacing=10, margins=(100, 100, 100, 100))
    labelx = Label(text=u'Höchster Bildungsabschluss', frame=False, center=False)
    
    list_checkboxes = ['checkbox1','checkbox2','checkbox3','checkbox4','checkbox5','checkbox6']
    edu_levels = ['Pflichtschule','Sekundärabschluss (mittlere Reife)','Sekundärabschluss (Lehre)','Sekundärabschluss (Matura/Abitur)','Tertiärabschluss (Akademie/Kolleg)','Tertiärabschluss (Universität/Hochschule)']
    
    for i in range(6):
        def wrap_checkbox(edu, checkbox, new_value):
            """Monkey-patches a checkbox so that it uses a custom value for
            the variable, rather than the text on the checkbox.
            """
            def wrap_set_var(fnc):
                def inner(val, var=None):
                    orig_text = checkbox.text
                    checkbox.text = new_value
                    fnc(val, var)
                    checkbox.text = orig_text
                return inner
            checkbox = checkbox.construct(edu)
            checkbox.set_var = wrap_set_var(checkbox.set_var)
            return checkbox
        checkbox = Checkbox(
            text=edu_levels[i],
            group='education',
            var='Resp'
        )
        list_checkboxes[i] = wrap_checkbox(edu, checkbox, new_value=i+1)
        edu.set_widget(list_checkboxes[i], (1, i+2))
    
    button = Button(text='Ok', frame=True, center=True)
    edu.set_widget(labelx   , (2, 1))
    edu.set_widget(button   , (2, 8))
    edu._exec()
    
Sign In or Register to comment.