Updating only part of a form
Hello!
I'm trying to make a form, with many buttons on it (see below). The first 15 buttons become active when they are clicked. Then there is the 'Incorrect' button and the 'Dubbel' button. If they are clicked, a value of 1 is added to the variables 'incorrect' and 'double', which are presented below on the left.
What I am trying to do, is updating only the variables shown below on the left (incorrect and dubbel) without reloading the whole form, because in that case you reset everything and the buttons you clicked before become inactive again.
Does anyone know a way to fix this?
Thank you in advance!
Jenna
My script in inline.script:
from libopensesame import widgets
class touch_button(widgets.button):
def __init__(self, *arglist, **kwdict):
widgets.button.__init__(self, *arglist, **kwdict)
self._active = False
def on_mouse_click(self, pos):
# When the touch_button is active already, deactivate it
if self._active == True:
self._active = False
var.correct -= 1
# Otherwise, activate touch_button
else:
self._active = True
var.correct+=1
def draw_frame(self, rect=None, style='normal'):
"""Draw a normal frame when active, and a plain frame otherwise."""
if self._active:
widgets.button.draw_frame(self, rect, style='normal')
else:
widgets.button.draw_frame(self, rect, style='plain')
class incorrect_button(widgets.button):
def on_mouse_click(self,pos):
var.incorrect+=1
def draw_frame(self, rect=None, style='normal'):
widgets.button.draw_frame(self, rect, style='plain')
class double_button(widgets.button):
def on_mouse_click(self,pos):
var.double+=1
def draw_frame(self, rect=None, style='normal'):
widgets.button.draw_frame(self, rect, style='plain')
## Create a form
form = widgets.form(exp, cols=[1,1,1,1], rows=[1,1,1,1,1], margins=(50,50,50,50), spacing=10)
b1 = touch_button(form, text='Woord1')
b2 = touch_button(form, text='Woord2')
b3 = touch_button(form, text='Woord3')
b4 = touch_button(form, text='Woord4')
b5 = touch_button(form, text='Woord5')
b6 = touch_button(form, text='Woord6')
b7 = touch_button(form, text='Woord7')
b8 = touch_button(form, text='Woord8')
b9 = touch_button(form, text='Woord9')
b10 = touch_button(form, text='Woord10')
b11 = touch_button(form, text='Woord11')
b12 = touch_button(form, text='Woord12')
b13 = touch_button(form, text='Woord13')
b14 = touch_button(form, text='Woord14')
b15 = touch_button(form, text='Woord15')
tekst = widgets.label(form, text=tekst_incorrect)
incorrect = incorrect_button(form, text='Incorrect')
dubbel = double_button(form, text='Dubbel')
nextButton = widgets.button(form, text='Volgende')
form.set_widget(b1, (0,0))
form.set_widget(b2, (1,0))
form.set_widget(b3, (2,0))
form.set_widget(b4, (3,0))
form.set_widget(b5, (0,1))
form.set_widget(b6, (1,1))
form.set_widget(b7, (2,1))
form.set_widget(b8, (3,1))
form.set_widget(b9, (0,2))
form.set_widget(b10, (1,2))
form.set_widget(b11, (2,2))
form.set_widget(b12, (3,2))
form.set_widget(b13, (0,3))
form.set_widget(b14, (1,3))
form.set_widget(b15, (2,3))
form.set_widget(tekst, (0,4))
form.set_widget(incorrect, (1,4))
form.set_widget(dubbel, (2,4))
form.set_widget(nextButton, (3,4))
form._exec()
Comments
Hi,
That's nicely done. So basically, you want to dynamically update the part that says 'Incorrect: 0 …', right? To do so, I would create another subclass (based on a
label
) that changes its text each time that it's rendered:And then you use this custom widget instead of the regular
label
.I think that's all. Does that make sense?
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
Thank you for your answer!! It totally works now
Jenna