File: widgets.rst

package info (click to toggle)
neuron 8.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,760 kB
  • sloc: cpp: 149,571; python: 58,465; ansic: 50,329; sh: 3,510; xml: 213; pascal: 51; makefile: 35; sed: 5
file content (773 lines) | stat: -rwxr-xr-x 21,784 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
.. _panel:

         
GUI Widgets
-----------

The functions below are used to create 
panels of buttons, menus, and field editors. 

It is often convenient to encapsulate GUI elements and their state variables in
a class. This allows multiple independent instances to be created. For example:

.. code-block::
    python

    from neuron import h, gui

    class MyWindow:
        def __init__(self):
            self.mystate = 0
            self.myval = 3.14
            self.checkbox = 1
            h.xpanel('demo')
            h.xradiobutton('Click me', (self.clicked, 0), 1)
            h.xradiobutton('or me', (self.clicked, 1), 0)
            h.xstatebutton('press me', (self, 'mystate'), self.statepressed)
            h.xcheckbox('I am a checkbox', (self, 'checkbox'), self.checkboxpressed)
            h.xvalue('Type a number', (self, 'myval'), 1, self.numberset)
            h.xpanel()
        def clicked(self, choice):
            print('you switched the radio button! choice = %g' % choice)
        def statepressed(self):
            print('you pressed the state button. Value = %g' % self.mystate)
        def checkboxpressed(self):
            print('you clicked the checkbox. state = %g' % self.checkbox)
        def numberset(self):
            print('you set the number to: %g' % self.myval)

    window = MyWindow()

.. image:: ../../images/guiwidgets-example.png
    :align: center
            
.. note::
    
    Top-level variables can be accessed through ``this_module`` where that is defined via ``import sys; this_module = sys.modules[__name__]``; see examples below.

----



.. function:: xpanel


    Syntax:
        .. code-block::
            python
            
            h.xpanel('name')
            h.xpanel('name', [0-1])
            h.xpanel()
            h.xpanel(x, y)
            h.xpanel(scroll)
            h.xpanel(scroll, x, y)


    Description:
         
        ``h.xpanel("name")`` 

        ``h.xpanel("name", [0-1])`` 
            Title of a new panel. Every 
            button, menu, and value between this and a closing ``xpanel()`` command 
            with no arguments (or placement args) belongs to this panel. 
            If the form is used with a second argument equal to 1, then 
            the panel is laid out horizontally. Otherwise the default is vertically. 

        ``h.xpanel()`` 

        ``h.xpanel(x, y)`` 
            done constructing the panel. so map it to the screen with position 
            optionally specified. 

        ``h.xpanel(scroll)`` 

        ``h.xpanel(scroll, x, y)`` 
            as above but if the first arg is a number, then the value determines 
            whether the panel will be inside a scrollbox. Scroll = 0 means a scrollbox 
            will NOT be used. Scroll = 1 means the panel will be inside a scrollbox. 
            Scroll = -1 is the default value and whether or not a scrollbox is used 
            is determined by the number of panel items in comparison with the 
            value of the panel_scroll property in the nrn.defaults file. 


         
         

----



.. function:: xbutton


    Syntax:
        
        ``h.xbutton('prompt', py_callable)``

        ``h.xbutton('prompt', (py_callable, arg))``

        ``h.xbutton('prompt', (py_callable, (arg1, arg2, ...))``


    Description:

        Adds a button to the currently open :func:`xpanel`. The label on the
        button is *prompt*, and when the button is clicked, it calls the
        *py_callable*. If instead of just ``py_callable`` a tuple containing
        ``py_callable`` and either an arg or tuple of args is passed, then
        those are passed to the function when the button is clicked; this can
        be used to allow one function to handle mutliple buttons, or buttons
        created in unknown situations.
    
    Example:

        .. code-block::
            python

            from neuron import h, gui

            def on_press():
                print('You pressed the button.')

            h.xpanel('Button demo')
            h.xbutton('Press me', on_press)
            h.xpanel()


        .. image:: ../../images/xbutton.png
            :align: center         


    Example:

        .. code-block::
            python

            from neuron import h, gui

            def on_press(button_id):
                print('You clicked button: %d' % button_id)

            def on_press3(a, b):
                print('Button args: %s, %s' % (a, b))

            h.xpanel('Button demo 2')
            h.xbutton('Button 1', (on_press, 1))
            h.xbutton('Button 2', (on_press, 2))
            h.xbutton('Button 3', (on_press3, ('arg1', 'arg2')))
            h.xpanel()

        .. image:: ../../images/xbutton2.png
            :align: center         


----



.. function:: xstatebutton


    Syntax:
        .. code-block::
            python
            
            h.xstatebutton('prompt', (obj_or_module, 'varname') [, action_fn])


    Description:
        like :func:`xbutton`, but when pressed var is set to 0 or 1 so that it matches the 
        telltale state of the button. If the var is set by another way the 
        telltale state is updated to reflect the correct value. 
    
    Example:
        .. code-block::
            python

            from neuron import h, gui
            import sys; this_module = sys.modules[__name__]

            button_state = 0

            def on_press():
                print('You pressed the button. The state is now: %g' % button_state)

            h.xpanel('StateButton demo')
            h.xstatebutton('Press me', (this_module, 'button_state'), on_press)
            h.xpanel()
         
        .. image:: ../../images/xstatebutton.png
            :align: center       

----



.. function:: xcheckbox


    Syntax:
        .. code-block::
            python
            
            h.xcheckbox('prompt', (obj_or_module, 'varname') [, action_fn])


    Description:
        like :func:`xstatebutton`, but checkbox appearance. 

         

----



.. function:: xradiobutton


    Syntax:
        .. code-block::
            python
            
            h.xradiobutton('name', action_fn [, 0 or 1])


    Description:
        Like an :func:`xbutton` but highlights the most recently selected 
        button of a contiguous group (like a car radio, mutually exclusive 
        selection). 
        If the third argument is 1, then the button will be selected when the 
        panel is mapped onto the screen. However, in 
        this case the action should also be explicitly executed by the programmer. 
        That is not done automatically since it is often the case that the action 
        is invalid when the radio button is created. As with ``h.xbutton``, ``action_fn`` may
        be a Python function handle, a tuple containing a function handle and an argument, or
        a tuple containing a function handle and a tuple of arguments.

    Example:

        .. code-block::
            python
            
            from neuron import h, gui

            def a(n):
                """function to be called when a radio button is toggled"""
                print(n)

            h.xpanel('panel')
            h.xmenu('menu')
            for i in range(1, 11):
                h.xradiobutton('item %d' % i, (a, i))

            h.xmenu()
            h.xpanel()

         
        .. image:: ../../images/xradiobutton.png
            :align: center  
         

----



.. function:: xmenu


    Syntax:
        .. code-block::
            python
            
            h.xmenu('title')
            h.xmenu()
            h.xmenu('title', 1)
            h.xmenu(title, py_callable)
            h.xmenu(title, py_callable, 1)


    Description:


        ``h.xmenu("title")`` 
            create a button in the panel with label "title" which, when 
            pressed, pops up a menu containing buttons and other menus. Every 
            :func:`xbutton` and :func:`xmenu` command between this and the closing ``xmenu()`` 
            command with no arguments becomes the menu. 
            Don't put values into menus. 

        ``h.xmenu()`` 
            done defining the menu. Menus can be nested as in 

            .. code-block::
                python
                
                from neuron import h, gui

                def selected1():
                    print('you selected option 1')

                def selected2():
                    print('you selected option 2')

                h.xpanel('xmenu demo')
                h.xmenu('one')
                h.xmenu('two')
                h.xbutton('option 1', selected1)
                h.xbutton('option 2', selected2)
                h.xmenu()
                h.xmenu()
                h.xpanel()

            .. image:: ../../images/xmenu1.png
                :align: center 
            
        ``h.xmenu("title", 1)`` 
            adds the menu to the menubar. Note that a top level menu with no 
            second argument starts a new menubar. Normally these menubars have only 
            one top level item. 

            .. code-block::
                python

                from neuron import h, gui
                import sys

                def item_selected(n):
                    print('selected value %g' % n)

                h.xpanel("menubar") 
                h.xmenu("first") 
                h.xbutton("one", (item_selected, 1))
                h.xbutton("two", (item_selected, 2))
                h.xbutton("Exit", sys.exit)
                h.xmenu() 
                h.xmenu("second", 1) 
                h.xbutton("three", (item_selected, 3))
                h.xbutton("four", (item_selected, 4))
                h.xmenu("submenu") 
                h.xbutton("PI", (item_selected, h.PI))
                h.xmenu() 
                h.xmenu() 
                h.xmenu("third", 1) 
                h.xbutton("five", (item_selected, 5)) 
                h.xbutton("six", (item_selected, 6))
                h.xmenu() 
                h.xmenu("nextline") 
                h.xbutton("seven", (item_selected, 7))
                h.xbutton("eight", (item_selected, 8))
                h.xmenu() 
                h.xpanel() 

            .. image:: ../../images/xmenu2.png
                :align: center 

        ``h.xmenu("title", py_callable)`` and ``h.xmenu("title", py_callable, 1)`` 
            Dynamic menu added as item in panel or menu or (when third argument 
            is 1) to a menubar. An example of the first type is the 
            NEURONMainMenu/File/RecentDir and an example of the last type is the 
            NEURONMainMenu/Window 
             
            When the menu title button is selected, the stmt is executed in a context 
            like: 

            .. code-block::
                python

            	h.xmenu("title") 
            	py_callable()
            	h.xmenu() 

            which should normally build a menu list and then this list is mapped to 
            the screen as a normal walking menu. 
             

            .. code-block::
                python
                
                from neuron import h, gui

                def select(i):
                    print('you selected %d' % i)

                def make():
                    make.n += 1
                    for i in range(1, make.n + 1):
                        h.xbutton('label %d' % i, (select, i))

                make.n = 0

                h.xpanel("test") 
                h.xmenu("dynamic", make) 
                xpanel() 
            
            .. warning::
                
                The dynamic menu syntax is currently unsupported in Python, but
                the equivalent (passing a HOC command string) works in HOC.
                 



         

----



.. function:: xlabel


    Syntax:
        .. code-block::
            python
            
            h.xlabel('string')


    Description:
        Show the string as a fixed label. 

         

----



.. function:: xvarlabel


    Syntax:
        .. code-block::
            python
            
            h.xvarlabel(strref)


    Description:
        Show the string as its current value. 
    
    Example:
    
        .. code-block::
            python
            
            from neuron import h, gui

            mystr = h.ref('')
            h.xpanel('xvarlabel demo')
            h.xlabel('Dynamic text will appear below:')
            h.xvarlabel(mystr)
            h.xpanel()

            # change the text displayed by changing mystr
            mystr[0] = 'Hello world!'

        .. image:: ../../images/xvarlabel.png
            :align: center 
            
    .. warning::
    
        Python strings are immutable. Thus the text displayed will only automatically
        change if a strref is used, as in the example.
         

----



.. function:: xvalue


    Syntax:
    
        .. code-block::
            python

            h.xvalue("prompt", (obj_or_module, "varname") [, boolean_deflt, "action" [, boolean_canrun]])
	    h.xvalue("prompt", ref_var, [, boolean_deflt, "action" [, boolean_canrun]])
            h.xvalue("prompt", "variable", 2)


    Description:

        ``h.xvalue("prompt", (obj_or_module, "varname") [, boolean_deflt, "action" [, boolean_canrun]])`` 
            create field editor for variable with the button labeled with "*prompt*". 
            If *boolean_deflt* == True (or 1) then add a checkbox which is checked when the 
            value of the field editor is different that when the editor was 
            created. Execute "action" when user enters a new value. If 
            *boolean_canrun* == True (or 1) then use a default_button widget kit appearance 
            instead	of a push_button widget kit appearance. 


        ``h.xvalue("prompt", (obj_or_module, "varname"), 2)`` 
            a field editor that keeps getting updated every 10th :func:`doNotify`. 

        .. The domain of values that can be entered by the user into a field editor 
        .. may be limited to the domain specified by the 
        .. :func:`variable_domain` function , the domain specified for the variable in 
        .. a model description file, or a default domain that exists 
        .. for some special NEURON variables such as diam, Ra, L, etc. 
        .. For a field editor to check the domain, domain limits must be in effect 
        .. prior to creation of the field editor. 

    Example:
    
        .. code-block::
            python
            
            from neuron import h, gui
            import sys; this_module = sys.modules[__name__]

            val = 42

            h.xpanel('demo')
            h.xvalue('enter value', (this_module, 'val'))
            h.xpanel()

            # changing val in the dialog will change val as seen by the program
        
        .. image:: ../../images/xvalue.png
            :align: center 

    .. seealso::
    
        The example at the top of the file, which uses ``xvalue`` in an object.
         

----



.. function:: xpvalue


    Syntax:
        .. code-block::
            python
            
            h.xpvalue('prompt', ref, ...)
            


    Description:
        like :func:`xvalue` but uses a reference to the variable.
    
    Example:
    
        .. code-block::
            python
            
            from neuron import h, gui

            val = h.ref(42)

            def show_val():
                print('value is: %g' % val[0])

            h.xpanel('demo')
            h.xpvalue('enter value', val, 1)
            h.xbutton('show value', show_val)
            h.xpanel()

        .. image:: ../../images/xpvalue.png
            :align: center 
            
    .. seealso::
    
        :func:`units`
         

----



.. function:: xfixedvalue


    Syntax:
    
        .. code-block::
            python
            
            h.xfixedvalue("prompt", (obj_or_module, "varname"), boolean_deflt, boolean_usepointer)


    Description:
        like :func:`xvalue` but cannot be changed by the user except under 
        program control and there can be no action associated with it. 
        
    .. warning::
        
        This is not implemented. For now, try to do the same thing 
        with :func:`xvarlabel`. 

         

----



.. function:: xslider


    Syntax:
        .. code-block::
            python
            
            h.xslider((obj_or_module, "varname"), [low, high], [on_slide], [vert], [slow])
            h.xslider(ref_var, [low, high], [on_slide], [vert], [slow])


    Description:
        Slider which is attached to the variable var. Whenever the slider 
        is moved, the optional *on_slide* is executed. The default range is 
        0 to 100. Steppers increase or decrease the value by 1/10 of the range. 
        Resolution is .01 of the range. vert=True (or 1) makes a vertical slider and 
        if there is no *on_slide* may be the 4th arg. slow=True (or 1) removes the "repeat 
        key" functionality from the slider(and arrow steppers) and also 
        prevents recursive calls to the *on_slide*. This is necessary if 
        a slider action is longer than the timeout delay. Otherwise the 
        slider can get in a state that appears to be an infinite loop. 
        The downside of slow=1 is that the var may not get the last value 
        of the slider if one releases the button during an action.

    Examples:
    
        .. code-block::
            python
            
            from neuron import h, gui
            import sys; this_module = sys.modules[__name__]

            val = 42
            val_str = h.ref('Slider value:         ')
            def show_val():
                val_str[0] = 'Slider value: %g' % val

            h.xpanel('demo')
            h.xvarlabel(val_str)
            h.xslider((this_module, 'val'), 0, 100, show_val)
            h.xpanel()
            show_val()

        .. image:: ../../images/xslider.png		   
            :align: center 
    
        It is slightly more efficient to use an ``h.ref`` instead of a tuple.
        The above example is functionally equivalent to:
    
        .. code-block::
            python
            
            from neuron import h, gui

            val = h.ref(42)
            val_str = h.ref('Slider value:         ')
            def show_val():
                val_str[0] = 'Slider value: %g' % val[0]

            h.xpanel('demo')
            h.xvarlabel(val_str)
            h.xslider(val, 0, 100, show_val)
            h.xpanel()
            show_val()


    
        You can also combine xslider with xvalue to report and modify its value (below example):
    
        .. code-block::
            python
            
            from neuron import h, gui

	    class ValuePanel:

		def __init__(self, init_val=42, label='',lower_limit=0,upper_limit=100):
		    self._val = h.ref(init_val)
		    h.xpanel('demo')
		    h.xlabel(label)
		    h.xvalue('enter value', self._val, True, self._bounds_check)
		    self.__lower_limit = lower_limit
		    self.__upper_limit = upper_limit
		    h.xslider(self._val, self.__lower_limit, self.__upper_limit)
		    h.xpanel()

		def _bounds_check(self):
		    self.val = self.val

		@property
		def val(self):
		    return self._val[0]

		@val.setter
		def val(self, new_val):
		    new_val = max(self.__lower_limit, new_val)
		    self._val[0] = min(new_val, self.__upper_limit)

	    if __name__=="__main__":
		hbox=h.HBox()  # Horizontal box
		hbox.intercept(True)
		length=ValuePanel(label='length.val')
		diam=ValuePanel(init_val=2,label='diam.val',lower_limit=0, upper_limit=5)
		hbox.intercept(False)
		hbox.map()
		print('can read/change length.val, diam.val')


        .. image:: ../../images/value_panel.png
	    :align: center

----


.. function:: units

    Syntax:
        .. code-block::
            python
            
            on or off = h.units(1 or 0)
            current_units = h.units("varname" [, "units string"])
        

    Description:
        When units are on (default on) value editor buttons display the units 
        string (if it exists) along with the normal prompt string. Units for 
        L, diam, Ra, t, etc are built-in and units for membrane mechanism variables 
        are declared in the model description file. See modlunit . 
        Note that units are NOT saved in a session. Therefore, any user defined 
        variables must be given units before retrieving a session that shows them 
        in a panel. 
         
        The units display may be turned off with \ ``h.units(0)`` or by setting the 
        \ ``*units_on_flag: off`` in the nrn/lib/nrn.defaults file. 
                 
        If the first arg is a string, it is treated as the name of the variable. 
        This is restricted to hoc variable names of the style, "name", or "classname.name". 
        Apart from the circumstance that the string arg style must be used when 
        executed from Python, a benefit is that it can be used when an instance 
        does not exist (no pointer to a variable of that type). 
        If there are no units specified for the variable name, or the variable 
        name is not defined, the return value is the empty string. 

    Examples:

        .. code-block::
            python
            
            print(h.units('dt'))        # ms
            print(h.units('gna_hh'))    # S/cm2
            print(h.units('Ra'))        # ohm-cm
            print(h.units('L'))         # um
            print(h.units('ExpSyn.g'))  # uS

    .. warning::
    
        When passing a string to ``h.units``, note that the string must be the
        name of a HOC variable. Unfortunately, there is currently no way to declare
        the units of a Python variable.