Howdy, Stranger!

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

Supported by

Slider: check that participants give a response

edited April 2021 in OpenSesame

Hi!!

I have a big problem.. I programmed a slider in Python inline code. I attached my script. But I didn't know how to check whether participants fill in the slider before go on with the experiment.

Thank you so much!!!!


from libopensesame.widgets._widget import Widget
from openexp.canvas_elements import Rect
from libopensesame.widgets import widget_factory
from libopensesame import widgets


class SliderWidget(Widget):
	
	"""A slider widget that integrates into an OpenSesame form."""
	
	def __init__(
		self,
		form,
		orientation=u'vertical',
		initial_fill=0.1,
		fill_color=u'green',
		var=None
	):
		
		Widget.__init__(self, form)
		self.frame = True
		self._orientation = orientation
		self._fill = initial_fill
		self._fill_element = None
		self._fill_color = fill_color
		self.var = var
	
	def coroutine(self):
		
		retval = None
		while True:
			d = yield retval			
			if u'pos' not in d:
				continue
			click_x, click_y = d[u'pos']
			x, y, w, h = self.rect
			if self._orientation == u'vertical':
				self._fill = 1. * (y + h - click_y) / h
			else:
				raise ValueError(u'Invalid orientation: %s' % self._orientation)
			self._set_fill()
			
	def _update_frame(self, rect=None, style=u'empty'):
		
		Widget._update_frame(self, rect=rect, style=style)
			
	def _update(self):
				
		Widget._update(self)
		if self._fill_element is None:
			self._fill_element = Rect(
				*self.rect, color=self._fill_color, fill=True
			).construct(self.form.canvas)
			self.form.canvas.add_element(self._fill_element)
		self._set_fill()
			
	def _set_fill(self):
		
		x, y, w, h = self.rect
		if self._orientation == u'vertical':
			self._fill_element.y = y + h * (1-self._fill)
			self._fill_element.h = h * self._fill
		else:
			raise ValueError(u'Invalid orientation: %s' % self._orientation)
		self.set_var(self._fill)
			
			
			
class Slider(widget_factory.WidgetFactory):
	
	"""A factory for a SliderWidget that provides the clean OpenSesame 3.2
	syntax.
	"""
	
	def construct(self, form):
		
		return SliderWidget(f, *self._args, **self._kwargs)
			
	
# Register the SliderWidget so that it can be used in a form_base
widgets.slider = SliderWidget


and then in a form base I have this script:
set timeout infinite
set spacing 10
set rows "1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1"
set only_render no
set margins "25;25;25;25"
set duration 3000
set description "A generic form plug-in"
set cols "1;1;1;1;1;1;1;1"
set _theme gray
widget 0 9.5 2 2 label text="<span style='font-size:28px;'>Quanto è vivida<br>questa immagine?</span>"
widget 3 0 1 1 label text="<span style='font-size:13px;'>Estremamente<br>vivida</span>"
widget 3 19.99 1 1 label text="<span style='font-size:13px;'>Per niente<br>vivida</span>"
widget 3 9.5 1 1 label text="<span style='font-size:13px;'>Mediamente<br>vivida</span>"
widget 4 0 1 20 slider fill_color=white orientation=vertical var=slider_var_int_imag
widget 7 18 8 19 button center=yes text="<span style='font-size:13px;'>Seguente</span>" var=response


Comments

  • Hi Lara,

    Have you seen the documentation on form validators? https://osdoc.cogsci.nl/3.3/manual/forms/validation/

    On github, you might also found a few examples on how validators can be implemented in your Slider widget. For example.

    By the way, if you feel like it, you can try to add your slider widget to Opensesame. You can open an issue on Github, and propose the additions you have made.

    Hope this helps a bit,

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.