Howdy, Stranger!

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

Supported by

[solved] How is button.on_mouse_click supposed to work?

edited July 2015 in OpenSesame

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

  • edited 1:54AM

    Forget that! I was missing a return statement in my overridden on_mouse_click function. The following makes it all work again:

    def on_mouse_click(self, pos):
        exp.set('lang', self._lang)
        return super(langButton, self).on_mouse_click(pos)
    

    Incidentally, is there an easier way to do this!?

  • edited 1:54AM

    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

  • edited 1:54AM

    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 than lang=中文, etc.). When I tried this -- admittedly, using set_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

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games