[solved] How is button.on_mouse_click supposed to work?
I am trying to get the form buttons to set a specific experiment variable. My approach is to subclass the button class and override the on_mouse_click
method such that it sets the appropriate variable.
This is my code:
from libopensesame import widgets
form = widgets.form(self.experiment, cols = [1, 1], rows = [1, 1, 1, 1],
margins = (100, 100, 100, 100), spacing = 25)
lex = [
{
'id': 'en',
'language': 'English',
'instructions': 'Choose your native language for instruction:'
},
{
'id': 'zh',
'language': '中文',
'instructions': '(Translation of the above)'
}
]
class langButton(widgets.button):
def __init__(self, language):
super(langButton, self).__init__(form, text = language['language'])
self._lang = language['id']
def on_mouse_click(self, pos):
exp.set('lang', self._lang)
super(langButton, self).on_mouse_click(pos)
for i, lang in enumerate(lex):
form.set_widget(widgets.label(form, text = lang['instructions']),
(0, i), colspan = 2)
form.set_widget(langButton(lang), (i, 3))
form._exec()
While this executes, the buttons no longer work when you click them. What am I doing wrong?
Thanks;
C
Comments
Forget that! I was missing a
return
statement in my overriddenon_mouse_click
function. The following makes it all work again:Incidentally, is there an easier way to do this!?
Hi Christoph,
Normally the widget.button function has a parameter called "var", that you can for example name 'response' (var='response'), so that a click on this button will automatically set the experimental variable 'response', with the button name as its value.
You would thus only need three lines of code: one to create the form, one to create a button, and one to indicate the location of that button on the form.
I don't understand yet why you had to involve the on_mouse_click part; is there anything I'm missing?
Cheers,
Josh
Hi Josh,
I wanted the scenario where I have multiple buttons that set the value of the same experiment variable to something specific. It was also desirable for me for the value to be a code rather than the button text (i.e., it's arguably easier to analyse results that have
lang=zh
thanlang=中文
, etc.). When I tried this -- admittedly, usingset_var
, rather than in the constructor -- it seemed to set the variable for the class rather than the specific instance. Thus, every button gave the same value.My solution was thus to subclass the button and repurpose its click handler so that it sets the experiment variable appropriately when a click event is registered. This works fine and, while the code is actually quite straightforward, it seemed like a roundabout way to achieve what I wanted. This is either because I'm trying to achieve something the framework wasn't designed for -- in which case, I really appreciate being able to drop down into Python: that was a great design decision, btw -- or I'm missing something and overcomplicating things.
Anyway, this works, so consider it solved. However, if there is a better way of doing it, then I'd be interested to hear it.
Thanks;
Chris