This file is indexed.

/usr/share/doc/python-urwid/html/manual/widgets.html is in python-urwid 1.3.1-2+b1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Widgets &mdash; Urwid 1.3.1</title>
    
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.3.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="top" title="Urwid 1.3.1" href="../index.html" />
    <link rel="up" title="Urwid Manual" href="index.html" />
    <link rel="next" title="User Input" href="userinput.html" />
    <link rel="prev" title="Display Modules" href="displaymodules.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="userinput.html" title="User Input"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="displaymodules.html" title="Display Modules"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Urwid 1.3.1</a> &raquo;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Urwid Manual</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="widgets">
<h1>Widgets<a class="headerlink" href="#widgets" title="Permalink to this headline"></a></h1>
<div class="section" id="widget-layout">
<span id="id1"></span><h2>Widget Layout<a class="headerlink" href="#widget-layout" title="Permalink to this headline"></a></h2>
<p>Urwid uses widgets to divide up the available screen space. This makes it easy
to create a fluid interface that moves and changes with the user&#8217;s terminal and
font size.</p>
<img alt="../_images/widget_layout.png" src="../_images/widget_layout.png" />
<p>The result of rendering a widget is a canvas suitable for displaying on the
screen. When we render the topmost widget:</p>
<ol class="arabic simple">
<li>The topmost widget <em>(a)</em> is rendered the full size of the screen</li>
<li><em>(a)</em> renders <em>(b)</em> any size up to the full size of the screen</li>
<li><em>(b)</em> renders <em>(c)</em>, <em>(d)</em> and <em>(e)</em> dividing its available screen columns
between them</li>
<li><em>(e)</em> renders <em>(f)</em> and <em>(g)</em> dividing its available screen rows between
them</li>
<li><em>(e)</em> combines the canvases from <em>(f)</em> and <em>(g)</em> and returns them</li>
<li><em>(b)</em> combines the canvases from <em>(c)</em>, <em>(d)</em> and <em>(e)</em> and returns them</li>
<li><em>(a)</em> possibly modifies the canvas from <em>(b)</em> and returns it</li>
</ol>
<p>Widgets <em>(a)</em>, <em>(b)</em> and <em>(e)</em> are called container widgets because they
contain other widgets. Container widgets choose the size and position of their
contained widgets.</p>
<p>Container widgets must also keep track of which one of their contained widgets
is in focus. The focus is used when handling keyboard input. If in the above
example <em>(b)</em> &#8216;s focus widget is <em>(e)</em> and <em>(e)</em> &#8216;s focus widget is
<em>(f)</em> then keyboard input will be handled this way:</p>
<ol class="arabic simple">
<li>The keypress is passed to the topmost widget <em>(a)</em></li>
<li><em>(a)</em> passes the keypress to <em>(b)</em></li>
<li><em>(b)</em> passes the keypress to <em>(e)</em>, its focus widget</li>
<li><em>(e)</em> passes the keypress to <em>(f)</em>, its focus widget</li>
<li><em>(f)</em> either handles the keypress or returns it</li>
<li><em>(e)</em> has an opportunity to handle the keypress if it was returned from
<em>(f)</em></li>
<li><em>(b)</em> has an opportunity to handle the keypress if it was returned from
<em>(e)</em></li>
<li><em>(a)</em> has an opportunity to handle the keypress if it was returned from
<em>(b)</em></li>
</ol>
</div>
<div class="section" id="box-flow-and-fixed-widgets">
<h2>Box, Flow and Fixed Widgets<a class="headerlink" href="#box-flow-and-fixed-widgets" title="Permalink to this headline"></a></h2>
<p>The size of a widget is measured in screen columns and rows. Widgets that are
given an exact number of screen columns and rows are called box widgets. The
topmost widget is always a box widget.</p>
<p>Much of the information displayed in a console user interface is text and the
best way to display text is to have it flow from one screen row to the next.
Widgets like this that require a variable number of screen rows are called flow
widgets. Flow widgets are given a number of screen columns and can calculate
how many screen rows they need.</p>
<p>Occasionally it is also useful to have a widget that knows how many screen
columns and rows it requires, regardless of the space available. This is
called a fixed widget.</p>
<table border="1" class="docutils" id="id6">
<caption><span class="caption-text">How a Widget&#8217;s Size is Determined</span><a class="headerlink" href="#id6" title="Permalink to this table"></a></caption>
<colgroup>
<col width="20%" />
<col width="40%" />
<col width="40%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">sizing mode</th>
<th class="head">width</th>
<th class="head">height</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'box'</span></code></td>
<td>container decides</td>
<td>container decides</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'flow'</span></code></td>
<td>container decides</td>
<td>widget&#8217;s <a class="reference internal" href="../reference/widget.html#urwid.Widget.rows" title="urwid.Widget.rows"><code class="xref py py-meth docutils literal"><span class="pre">rows()</span></code></a> method</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'fixed'</span></code></td>
<td>widget&#8217;s <a class="reference internal" href="../reference/widget.html#urwid.Widget.pack" title="urwid.Widget.pack"><code class="xref py py-meth docutils literal"><span class="pre">pack()</span></code></a> method</td>
<td>widget&#8217;s <a class="reference internal" href="../reference/widget.html#urwid.Widget.pack" title="urwid.Widget.pack"><code class="xref py py-meth docutils literal"><span class="pre">pack()</span></code></a> method</td>
</tr>
</tbody>
</table>
<p>It is an Urwid convention to use the variables <code class="xref py py-attr docutils literal"><span class="pre">maxcol</span></code> and
<code class="xref py py-attr docutils literal"><span class="pre">maxrow</span></code> to store a widget&#8217;s size. Box widgets require both of <code class="docutils literal"><span class="pre">(maxcol,</span>
<span class="pre">maxrow)</span></code> to be specified.</p>
<p>Flow widgets expect a single-element tuple <code class="docutils literal"><span class="pre">(maxcol,)</span></code> instead because they
calculate their <code class="xref py py-attr docutils literal"><span class="pre">maxrow</span></code> based on the <code class="xref py py-attr docutils literal"><span class="pre">maxcol</span></code> value.</p>
<p>Fixed widgets expect the value <code class="docutils literal"><span class="pre">()</span></code> to be passed in to functions that take
a size because they know their <code class="xref py py-attr docutils literal"><span class="pre">maxcol</span></code> and <code class="xref py py-attr docutils literal"><span class="pre">maxrow</span></code> values.</p>
</div>
<div class="section" id="included-widgets">
<span id="basic-grafic-widgets"></span><h2>Included Widgets<a class="headerlink" href="#included-widgets" title="Permalink to this headline"></a></h2>
<p><a class="reference internal" href="../reference/widget.html#widget-classes"><span>Widget class reference</span></a></p>
<img alt="../_images/urwid_widgets_1.png" src="../_images/urwid_widgets_1.png" />
<p>Basic and graphic widgets are the content with which users interact. They may
also be used as part of custom widgets you create.</p>
<img alt="../_images/urwid_widgets_2.png" src="../_images/urwid_widgets_2.png" />
</div>
<div class="section" id="decoration-widgets">
<span id="id2"></span><h2>Decoration Widgets<a class="headerlink" href="#decoration-widgets" title="Permalink to this headline"></a></h2>
<p>Decoration widgets alter the appearance or position of a single other widget.
The widget they wrap is available as the
<code class="xref py py-attr docutils literal"><span class="pre">original_widget</span></code> property.
If you might be using more than one decoration widget you may use the
<a class="reference internal" href="../reference/widget.html#urwid.WidgetDecoration.base_widget" title="urwid.WidgetDecoration.base_widget"><code class="xref py py-attr docutils literal"><span class="pre">base_widget</span></code></a> property to access the
&#8220;most&#8221; original_widget.
<a class="reference internal" href="../reference/widget.html#urwid.Widget.base_widget" title="urwid.Widget.base_widget"><code class="xref py py-attr docutils literal"><span class="pre">Widget.base_widget</span></code></a> points to <code class="docutils literal"><span class="pre">self</span></code> on all non-decoration widgets, so
it is safe to use in any situation.</p>
</div>
<div class="section" id="container-widgets">
<span id="id3"></span><h2>Container Widgets<a class="headerlink" href="#container-widgets" title="Permalink to this headline"></a></h2>
<p>Container widgets divide their available space between their child widgets.
This is how widget layouts are defined. When handling selectable widgets
container widgets also keep track of which of their child widgets is in focus.
Container widgets may be nested, so the actual widget in focus may be many
levels below the topmost widget.</p>
<p>Urwid&#8217;s container widgets have a common API you can use, regardless of the
container type.  Backwards compatibility is still maintained for the old
container-specific ways of accessing and modifying contents, but this API
is now the preferred way of modifying and traversing containers.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">container</span><span class="o">.</span><span class="n">focus</span>
</pre></div>
</div>
<p>is a read-only property that returns the widget in focus for this container.
Empty containers and non-container widgets (that inherit from Widget)
return <code class="docutils literal"><span class="pre">None</span></code>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">container</span><span class="o">.</span><span class="n">focus_position</span>
</pre></div>
</div>
<p>is a read/write property that provides access to the position of the
container&#8217;s widget in focus.  This will often be a integer value but may be
any object.
<a class="reference internal" href="../reference/widget.html#urwid.Columns" title="urwid.Columns"><code class="xref py py-class docutils literal"><span class="pre">Columns</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.Pile" title="urwid.Pile"><code class="xref py py-class docutils literal"><span class="pre">Pile</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.GridFlow" title="urwid.GridFlow"><code class="xref py py-class docutils literal"><span class="pre">GridFlow</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.Overlay" title="urwid.Overlay"><code class="xref py py-class docutils literal"><span class="pre">Overlay</span></code></a> and
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> with a <a class="reference internal" href="../reference/list_walkers.html#urwid.SimpleListWalker" title="urwid.SimpleListWalker"><code class="xref py py-class docutils literal"><span class="pre">SimpleListWalker</span></code></a> or <a class="reference internal" href="../reference/list_walkers.html#urwid.SimpleFocusListWalker" title="urwid.SimpleFocusListWalker"><code class="xref py py-class docutils literal"><span class="pre">SimpleFocusListWalker</span></code></a>
as its body use integer positions.  <a class="reference internal" href="../reference/widget.html#urwid.Frame" title="urwid.Frame"><code class="xref py py-class docutils literal"><span class="pre">Frame</span></code></a> uses <code class="docutils literal"><span class="pre">'body'</span></code>, <code class="docutils literal"><span class="pre">'header'</span></code>
and <code class="docutils literal"><span class="pre">'footer'</span></code>;  <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> with a custom list walker will use the
positions the list walker returns.</p>
<p>Reading this value on an empty container or on any non-container widgets
(that inherit from Widget) raises an IndexError.  Writing to this property with
an invalid position will also raise an IndexError.  Writing a new value
automatically marks this widget to be redrawn and will be reflected in
<code class="docutils literal"><span class="pre">container.focus</span></code>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">container</span><span class="o">.</span><span class="n">contents</span>
</pre></div>
</div>
<p>is a read-only property (read/write in some cases) that provides access to a
mapping- or list-like object that contains the child widgets and the options
used for displaying those widgets in this container.  The mapping- or list-like
object always allows reading from positions with the usual <code class="docutils literal"><span class="pre">__getitem__()</span></code>
method and may support assignment and deletion with <code class="docutils literal"><span class="pre">__setitem__()</span></code> and
<code class="docutils literal"><span class="pre">__delitem__()</span></code> methods.  The values are <code class="docutils literal"><span class="pre">(child</span> <span class="pre">widget,</span> <span class="pre">option)</span></code> tuples.
When this object or its contents are modified the widget is automatically
flagged to be redrawn.</p>
<p><a class="reference internal" href="../reference/widget.html#urwid.Columns" title="urwid.Columns"><code class="xref py py-class docutils literal"><span class="pre">Columns</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.Pile" title="urwid.Pile"><code class="xref py py-class docutils literal"><span class="pre">Pile</span></code></a> and <a class="reference internal" href="../reference/widget.html#urwid.GridFlow" title="urwid.GridFlow"><code class="xref py py-class docutils literal"><span class="pre">GridFlow</span></code></a> allow assigning an
iterable to <code class="docutils literal"><span class="pre">container.contents</span></code> to overwrite the values in
with the ones provided.</p>
<p><a class="reference internal" href="../reference/widget.html#urwid.Columns" title="urwid.Columns"><code class="xref py py-class docutils literal"><span class="pre">Columns</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.Pile" title="urwid.Pile"><code class="xref py py-class docutils literal"><span class="pre">Pile</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.GridFlow" title="urwid.GridFlow"><code class="xref py py-class docutils literal"><span class="pre">GridFlow</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.Overlay" title="urwid.Overlay"><code class="xref py py-class docutils literal"><span class="pre">Overlay</span></code></a> and
<a class="reference internal" href="../reference/widget.html#urwid.Frame" title="urwid.Frame"><code class="xref py py-class docutils literal"><span class="pre">Frame</span></code></a> support <code class="docutils literal"><span class="pre">container.contents</span></code> item assignment and deletion.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">container</span><span class="o">.</span><span class="n">options</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>is a method that returns options objects for use in items added to
<code class="docutils literal"><span class="pre">container.contents</span></code>.  The arguments are specific to the container type,
and generally match the <code class="docutils literal"><span class="pre">__init__()</span></code> arguments for the container.
The objects returned are currently tuples of strings and integers or <code class="docutils literal"><span class="pre">None</span></code>
for containers without child widget options.  This method exists to allow
future versions of Urwid to add new options to existing containers.  Code
that expects the option tuples to remain the same size will fail when new
options are added, so defensive programming with options tuples is strongly
encouraged.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">container</span><span class="o">.</span><span class="n">__getitem__</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="c1"># a.k.a.</span>
<span class="n">container</span><span class="p">[</span><span class="n">x</span><span class="p">]</span>
</pre></div>
</div>
<p>is a short-cut method behaving identically to:
<code class="docutils literal"><span class="pre">container.contents[x][0].base_widget</span></code>.
Which means roughly &#8220;give me the child widget at position <em>x</em> and skip all
the decoration widgets wrapping it&#8221;.  Decoration widgets include
<a class="reference internal" href="../reference/widget.html#urwid.Padding" title="urwid.Padding"><code class="xref py py-class docutils literal"><span class="pre">Padding</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.Filler" title="urwid.Filler"><code class="xref py py-class docutils literal"><span class="pre">Filler</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.AttrMap" title="urwid.AttrMap"><code class="xref py py-class docutils literal"><span class="pre">AttrMap</span></code></a> etc.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">container</span><span class="o">.</span><span class="n">get_focus_path</span><span class="p">()</span>
</pre></div>
</div>
<p>is a method that returns the focus position for this container <em>and</em> all child
containers along the path defined by their focus settings.  This list of
positions is the closest thing we have to the singular widget-in-focus in
other UI frameworks, because the ultimate widget in focus in Urwid depends
on the focus setting of all its parent container widgets.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">container</span><span class="o">.</span><span class="n">set_focus_path</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
</pre></div>
</div>
<p>is a method that assigns to the focus_position property of each container
along the path given by the list of positions <em>p</em>.  It may be used to restore
focus to a widget as returned by a previous call to <code class="docutils literal"><span class="pre">container.get_focus_path()</span></code>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">container</span><span class="o">.</span><span class="n">get_focus_widgets</span><span class="p">()</span>
</pre></div>
</div>
<p>is a method that returns the <code class="docutils literal"><span class="pre">.focus</span></code> values starting from this container
and proceeding along each child widget until reaching a leaf
(non-container) widget.</p>
<p>Note that the list does not contain the topmost container widget
(i.e, on which this method is called), but does include the
lowest leaf widget.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">container</span><span class="o">.</span><span class="n">__iter__</span><span class="p">()</span>
<span class="c1"># typically</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">container</span><span class="p">:</span> <span class="o">...</span>

<span class="n">container</span><span class="o">.</span><span class="n">__reversed__</span><span class="p">()</span>
<span class="c1"># a.k.a</span>
<span class="nb">reversed</span><span class="p">(</span><span class="n">container</span><span class="p">)</span>
</pre></div>
</div>
<p>are methods that allow iteration over the <em>positions</em> of this container.
Normally the order of the positions generated by __reversed__() will be the
opposite of __iter__().  The exception is the case of <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> with
certain custom list walkers, and the reason goes back to the original way list
walker interface was defined.  Note that a custom list walker might also generate
an unbounded number of positions, so care should be used with this interface and
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>.</p>
<div class="section" id="pile-widgets">
<h3>Pile Widgets<a class="headerlink" href="#pile-widgets" title="Permalink to this headline"></a></h3>
<p><a class="reference internal" href="../reference/widget.html#urwid.Pile" title="urwid.Pile"><code class="xref py py-class docutils literal"><span class="pre">Pile</span></code></a> widgets are used to combine multiple widgets by
stacking them vertically. A Pile can manage selectable widgets by keeping track
of which widget is in focus and it can handle moving the focus between widgets
when the user presses the <em>UP</em> and <em>DOWN</em> keys. A Pile will also work well when
used within a <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>.</p>
<p>A Pile is selectable only if its focus widget is selectable. If you create a
Pile containing one Text widget and one Edit widget the Pile will choose the
Edit widget as its default focus widget.</p>
</div>
<div class="section" id="columns-widgets">
<h3>Columns Widgets<a class="headerlink" href="#columns-widgets" title="Permalink to this headline"></a></h3>
<p><a class="reference internal" href="../reference/widget.html#urwid.Columns" title="urwid.Columns"><code class="xref py py-class docutils literal"><span class="pre">Columns</span></code></a> widgets may be used to arrange either flow
widgets or box widgets horizontally into columns. Columns widgets will manage
selectable widgets by keeping track of which column is in focus and it can
handle moving the focus between columns when the user presses the <em>LEFT</em> and
<em>RIGHT</em> keys. Columns widgets also work well when used within a
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>.</p>
<p>Columns widgets are selectable only if the column in focus is selectable. If a
focus column is not specified the first selectable widget will be chosen as the
focus column.</p>
</div>
<div class="section" id="gridflow-widgets">
<h3>GridFlow Widgets<a class="headerlink" href="#gridflow-widgets" title="Permalink to this headline"></a></h3>
<p>The <a class="reference internal" href="../reference/widget.html#urwid.GridFlow" title="urwid.GridFlow"><code class="xref py py-class docutils literal"><span class="pre">GridFlow</span></code></a> widget is a flow widget designed for use
with <a class="reference internal" href="../reference/widget.html#urwid.Button" title="urwid.Button"><code class="xref py py-class docutils literal"><span class="pre">Button</span></code></a>, <a class="reference internal" href="../reference/widget.html#urwid.CheckBox" title="urwid.CheckBox"><code class="xref py py-class docutils literal"><span class="pre">CheckBox</span></code></a> and
<a class="reference internal" href="../reference/widget.html#urwid.RadioButton" title="urwid.RadioButton"><code class="xref py py-class docutils literal"><span class="pre">RadioButton</span></code></a> widgets. It renders all the widgets it
contains the same width and it arranges them from left to right and top to
bottom.</p>
<p>The GridFlow widget uses Pile, Columns, Padding and Divider widgets to build a
display widget that will handle the keyboard input and rendering. When the
GridFlow widget is resized it regenerates the display widget to accommodate the
new space.</p>
</div>
<div class="section" id="overlay-widgets">
<h3>Overlay Widgets<a class="headerlink" href="#overlay-widgets" title="Permalink to this headline"></a></h3>
<p>The <a class="reference internal" href="../reference/widget.html#urwid.Overlay" title="urwid.Overlay"><code class="xref py py-class docutils literal"><span class="pre">Overlay</span></code></a> widget is a box widget that contains two
other box widgets. The bottom widget is rendered the full size of the Overlay
widget and the top widget is placed on top, obscuring an area of the bottom
widget. This widget can be used to create effects such as overlapping &#8220;windows&#8221;
or pop-up menus.</p>
<p>The Overlay widget always treats the top widget as the one in focus. All
keyboard input will be passed to the top widget.</p>
<p>If you want to use a flow widget for the top widget, first wrap the flow
widget with a <a class="reference internal" href="../reference/widget.html#urwid.Filler" title="urwid.Filler"><code class="xref py py-class docutils literal"><span class="pre">Filler</span></code></a> widget.</p>
</div>
</div>
<div class="section" id="listbox-contents">
<span id="id4"></span><h2>ListBox Contents<a class="headerlink" href="#listbox-contents" title="Permalink to this headline"></a></h2>
<p><a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> is a box widget that contains flow widgets.
Its contents are displayed stacked vertically, and the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> allows the user to scroll through its content.
One of the flow widgets displayed in the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> is its
focus widget.</p>
<div class="section" id="listbox-focus-and-scrolling">
<h3>ListBox Focus and Scrolling<a class="headerlink" href="#listbox-focus-and-scrolling" title="Permalink to this headline"></a></h3>
<p>The <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> is a box widget that contains flow widgets.
Its contents are displayed stacked vertically, and the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> allows the user to scroll through its content.
One of the flow widgets displayed in the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> is the
focus widget. The <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> passes key presses to the
focus widget to allow the user to interact with it. If the focus widget does
not handle a keypress then the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> may handle the
keypress by scrolling and/or selecting another widget to become the focus
widget.</p>
<p>The <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> tries to do the most sensible thing when
scrolling and changing focus. When the widgets displayed are all
<a class="reference internal" href="../reference/widget.html#urwid.Text" title="urwid.Text"><code class="xref py py-class docutils literal"><span class="pre">Text</span></code></a> widgets or other unselectable widgets then the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> will behave like a web browser does when the
user presses <em>UP</em>, <em>DOWN</em>, <em>PAGE UP</em> and <em>PAGE DOWN</em>: new text is immediately
scrolled in from the top or bottom. The <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> chooses
one of the visible widgets as its focus widget when scrolling. When scrolling
up the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> chooses the topmost widget as the focus,
and when scrolling down the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> chooses the
bottommost widget as the focus.</p>
<p>The <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> remembers the location of the widget in
focus as either an &#8220;offset&#8221; or an &#8220;inset&#8221;. An offset is the number of rows
between the top of the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> and the beginning of the
focus widget. An offset of zero corresponds to a widget with its top aligned
with the top of the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>. An inset is the fraction
of rows of the focus widget that are &#8220;above&#8221; the top of the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> and not visible. The
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> uses this method of remembering the focus
widget location so that when the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> is resized the
text displayed will stay roughly aligned with the top of the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>.</p>
<p>When there are selectable widgets in the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> the
focus will move between the selectable widgets, skipping the unselectable
widgets. The <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> will try to scroll all the rows of
a selectable widget into view so that the user can see the new focus widget in
its entirety. This behavior can be used to bring more than a single widget into
view by using composite widgets to combine a selectable widget with other
widgets that should be displayed at the same time.</p>
</div>
<div class="section" id="dynamic-listbox-with-listwalker">
<h3>Dynamic ListBox with ListWalker<a class="headerlink" href="#dynamic-listbox-with-listwalker" title="Permalink to this headline"></a></h3>
<p>While the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> stores the location of its focus
widget, it does not directly store the actual focus widget or other contents of
the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>. The storage of a
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>&#8216;s content is delegated to a &#8220;List Walker&#8221;
object. If a list of widgets is passed to the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>
constructor then it creates a <a class="reference internal" href="../reference/list_walkers.html#urwid.SimpleListWalker" title="urwid.SimpleListWalker"><code class="xref py py-class docutils literal"><span class="pre">SimpleListWalker</span></code></a> object
to manage the list.</p>
<p>When the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> is <a class="reference external" href=":meth:`ListBox.render`">rendering a canvas</a> or <a class="reference external" href=":meth:`ListBox.keypress`">handling
input</a> it will:</p>
<ol class="arabic simple">
<li>Call the <code class="xref py py-meth docutils literal"><span class="pre">get_focus()</span></code> method of its list walker object. This method
will return the focus widget and a position object.</li>
<li>Optionally call the <code class="xref py py-meth docutils literal"><span class="pre">get_prev()</span></code> method of its List Walker object one or
more times, initially passing the focus position and then passing the new
position returned on each successive call. This method will return the
widget and position object &#8220;above&#8221; the position passed.</li>
<li>Optionally call the <code class="xref py py-meth docutils literal"><span class="pre">get_next()</span></code> method of its List Walker object one or
more times, similarly, to collect widgets and position objects &#8220;below&#8221; the
focus position.</li>
<li>Optionally call the <code class="xref py py-meth docutils literal"><span class="pre">set_focus()</span></code> method passing one of the position
objects returned in the previous steps.</li>
</ol>
<p>This is the only way the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> accesses its contents,
and it will not store copies of any of the widgets or position objects beyond
the current rendering or input handling operation.</p>
<p>The <a class="reference internal" href="../reference/list_walkers.html#urwid.SimpleListWalker" title="urwid.SimpleListWalker"><code class="xref py py-class docutils literal"><span class="pre">SimpleListWalker</span></code></a> stores a list of widgets, and uses
integer indexes into this list as its position objects. It stores the focus
position as an integer, so if you insert a widget into the list above the focus
position then you need to remember to increment the focus position in the
<a class="reference internal" href="../reference/list_walkers.html#urwid.SimpleListWalker" title="urwid.SimpleListWalker"><code class="xref py py-class docutils literal"><span class="pre">SimpleListWalker</span></code></a> object or the contents of the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> will shift.</p>
<p>A custom List Walker object may be passed to the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> constructor instead of a plain list of widgets.
List Walker objects must implement the <a class="reference internal" href="#list-walker-interface"><span>List Walker Interface</span></a>.</p>
<p>The <a class="reference external" href="http://excess.org/urwid/browser/examples/fib.py">fib.py</a> example program demonstrates a custom list walker that doesn&#8217;t
store any widgets. It uses a tuple of two successive Fibonacci numbers as its
position objects and it generates Text widgets to display the numbers on the
fly. The result is a <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> that can scroll through an
unending list of widgets.</p>
<p>The <a class="reference external" href="http://excess.org/urwid/browser/examples/edit.py">edit.py</a> example program demonstrates a custom list walker that loads lines
from a text file only as the user scrolls them into view. This allows even
huge files to be opened almost instantly.</p>
<p>The <a class="reference external" href="http://excess.org/urwid/browser/examples/browse.py">browse.py</a> example program demonstrates a custom list walker that uses a
tuple of strings as position objects, one for the parent directory and one for
the file selected. The widgets are cached in a separate class that is accessed
using a dictionary indexed by parent directory names. This allows the
directories to be read only as required. The custom list walker also allows
directories to be hidden from view when they are &#8220;collapsed&#8221;.</p>
</div>
<div class="section" id="setting-the-focus">
<h3>Setting the Focus<a class="headerlink" href="#setting-the-focus" title="Permalink to this headline"></a></h3>
<p>The easiest way to change the current <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> focus is
to call the <a class="reference internal" href="../reference/widget.html#urwid.ListBox.set_focus" title="urwid.ListBox.set_focus"><code class="xref py py-meth docutils literal"><span class="pre">ListBox.set_focus()</span></code></a> method. This method doesn&#8217;t
require that you know the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>&#8216;s current dimensions
<code class="docutils literal"><span class="pre">(maxcol,</span> <span class="pre">maxrow)</span></code>. It will wait until the next call to either keypress or
render to complete setting the offset and inset values using the dimensions
passed to that method.</p>
<p>The position object passed to <code class="xref py py-meth docutils literal"><span class="pre">set_focus()</span></code> must be compatible with the
List Walker object that the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> is using. For
<a class="reference internal" href="../reference/list_walkers.html#urwid.SimpleListWalker" title="urwid.SimpleListWalker"><code class="xref py py-class docutils literal"><span class="pre">SimpleListWalker</span></code></a> the position is the integer index of
the widget within the list.</p>
<p>The <em>coming_from</em> parameter should be set if you know that the old position is
&#8220;above&#8221; or &#8220;below&#8221; the previous position. When the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> completes setting the offset and inset values
it tries to find the old widget among the visible widgets. If the old widget is
still visible, if will try to avoid causing the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>
contents to scroll up or down from its previous position. If the widget is not
visible, then the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> will:</p>
<ul class="simple">
<li>Display the new focus at the bottom of the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> if
<em>coming_from</em> is &#8220;above&#8221;.</li>
<li>Display the new focus at the top of the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> if
<em>coming_from</em> is &#8220;below&#8221;.</li>
<li>Display the new focus in the middle of the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> if
coming_from is <code class="docutils literal"><span class="pre">None</span></code>.</li>
</ul>
<p>If you know exactly where you want to display the new focus widget within the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> you may call
<a class="reference internal" href="../reference/widget.html#urwid.ListBox.set_focus_valign" title="urwid.ListBox.set_focus_valign"><code class="xref py py-meth docutils literal"><span class="pre">ListBox.set_focus_valign()</span></code></a>.  This method lets you specify
the <em>top</em>, <em>bottom</em>, <em>middle</em>, a relative position or the exact number of rows
from the top or bottom of the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>.</p>
</div>
<div class="section" id="list-walkers">
<h3>List Walkers<a class="headerlink" href="#list-walkers" title="Permalink to this headline"></a></h3>
<p><a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> does not manage the widgets it displays
directly, instead it passes that task to a class called a &#8220;list walker&#8221;. List
walkers keep track of the widget in focus and provide an opaque position object
that the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> may use to iterate through widgets
above and below the focus widget.</p>
<p>A <a class="reference internal" href="../reference/list_walkers.html#urwid.SimpleFocusListWalker" title="urwid.SimpleFocusListWalker"><code class="xref py py-class docutils literal"><span class="pre">SimpleFocusListWalker</span></code></a>
is a list walker that behaves like a normal Python list. It may be used any
time you will be displaying a moderate number of widgets.</p>
<p>If you need to display a large number of widgets you should implement your own
list walker that manages creating widgets as they are requested and destroying
them later to avoid excessive memory use.</p>
<p>List walkers may also be used to display tree or other structures within a
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>. A number of the <a class="reference external" href="http://excess.org/urwid/examples.html">example programs</a> demonstrate the use of custom list
walker classes.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="../reference/list_walkers.html#urwid.ListWalker" title="urwid.ListWalker"><code class="xref py py-class docutils literal"><span class="pre">ListWalker</span> <span class="pre">base</span> <span class="pre">class</span> <span class="pre">reference</span></code></a></p>
</div>
</div>
<div class="section" id="list-walker-interface">
<span id="id5"></span><h3>List Walker Interface<a class="headerlink" href="#list-walker-interface" title="Permalink to this headline"></a></h3>
<div class="section" id="list-walker-api-version-1">
<h4>List Walker API Version 1<a class="headerlink" href="#list-walker-api-version-1" title="Permalink to this headline"></a></h4>
<p>This API will remain available and is still the least restrictive option for
the programmer.  Your class should subclass <a class="reference internal" href="../reference/list_walkers.html#urwid.ListWalker" title="urwid.ListWalker"><code class="xref py py-class docutils literal"><span class="pre">ListWalker</span></code></a>.
Whenever the focus or content changes you are responsible for
calling <code class="xref py py-meth docutils literal"><span class="pre">ListWalker._modified()</span></code>.</p>
<dl class="method">
<dt id="MyV1ListWalker.get_focus">
<code class="descclassname">MyV1ListWalker.</code><code class="descname">get_focus</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#MyV1ListWalker.get_focus" title="Permalink to this definition"></a></dt>
<dd><p>return a <code class="docutils literal"><span class="pre">(widget,</span> <span class="pre">position)</span></code> tuple or <code class="docutils literal"><span class="pre">(None,</span> <span class="pre">None)</span></code> if empty</p>
</dd></dl>

<dl class="method">
<dt id="MyV1ListWalker.set_focus">
<code class="descclassname">MyV1ListWalker.</code><code class="descname">set_focus</code><span class="sig-paren">(</span><em>position</em><span class="sig-paren">)</span><a class="headerlink" href="#MyV1ListWalker.set_focus" title="Permalink to this definition"></a></dt>
<dd><p>set the focus and call <code class="docutils literal"><span class="pre">self._modified()</span></code> or raise an <code class="xref py py-exc docutils literal"><span class="pre">IndexError</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="MyV1ListWalker.get_next">
<code class="descclassname">MyV1ListWalker.</code><code class="descname">get_next</code><span class="sig-paren">(</span><em>position</em><span class="sig-paren">)</span><a class="headerlink" href="#MyV1ListWalker.get_next" title="Permalink to this definition"></a></dt>
<dd><p>return the <code class="docutils literal"><span class="pre">(widget,</span> <span class="pre">position)</span></code> tuple below <em>position</em> passed
or <code class="docutils literal"><span class="pre">(None,</span> <span class="pre">None)</span></code> if there is none.</p>
</dd></dl>

<dl class="method">
<dt id="MyV1ListWalker.get_prev">
<code class="descclassname">MyV1ListWalker.</code><code class="descname">get_prev</code><span class="sig-paren">(</span><em>position</em><span class="sig-paren">)</span><a class="headerlink" href="#MyV1ListWalker.get_prev" title="Permalink to this definition"></a></dt>
<dd><p>return the <code class="docutils literal"><span class="pre">(widget,</span> <span class="pre">position)</span></code> tuple above <em>position</em> passed
or <code class="docutils literal"><span class="pre">(None,</span> <span class="pre">None)</span></code> if there is none.</p>
</dd></dl>

</div>
<div class="section" id="list-walker-api-version-2">
<h4>List Walker API Version 2<a class="headerlink" href="#list-walker-api-version-2" title="Permalink to this headline"></a></h4>
<p>This API is an attempt to remove some of the duplicate code that V1 requires for
many users.  List walker API V1 will be implemented automatically by
subclassing <code class="xref py py-class docutils literal"><span class="pre">ListWalker</span></code> and implementing the V2 methods.
Whenever the focus or content changes you are responsible for
calling <code class="xref py py-meth docutils literal"><span class="pre">ListWalker._modified()</span></code>.</p>
<dl class="method">
<dt id="MyV2ListWalker.__getitem__">
<code class="descclassname">MyV2ListWalker.</code><code class="descname">__getitem__</code><span class="sig-paren">(</span><em>position</em><span class="sig-paren">)</span><a class="headerlink" href="#MyV2ListWalker.__getitem__" title="Permalink to this definition"></a></dt>
<dd><p>return widget at <em>position</em> or raise an <code class="xref py py-exc docutils literal"><span class="pre">IndexError</span></code> or <code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code></p>
</dd></dl>

<dl class="method">
<dt id="MyV2ListWalker.next_position">
<code class="descclassname">MyV2ListWalker.</code><code class="descname">next_position</code><span class="sig-paren">(</span><em>position</em><span class="sig-paren">)</span><a class="headerlink" href="#MyV2ListWalker.next_position" title="Permalink to this definition"></a></dt>
<dd><p>return the position below passed <em>position</em> or raise an <code class="xref py py-exc docutils literal"><span class="pre">IndexError</span></code> or <code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code></p>
</dd></dl>

<dl class="method">
<dt id="MyV2ListWalker.prev_position">
<code class="descclassname">MyV2ListWalker.</code><code class="descname">prev_position</code><span class="sig-paren">(</span><em>position</em><span class="sig-paren">)</span><a class="headerlink" href="#MyV2ListWalker.prev_position" title="Permalink to this definition"></a></dt>
<dd><p>return the position above passed <em>position</em> or raise an <code class="xref py py-exc docutils literal"><span class="pre">IndexError</span></code> or <code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code></p>
</dd></dl>

<dl class="method">
<dt id="MyV2ListWalker.set_focus">
<code class="descclassname">MyV2ListWalker.</code><code class="descname">set_focus</code><span class="sig-paren">(</span><em>position</em><span class="sig-paren">)</span><a class="headerlink" href="#MyV2ListWalker.set_focus" title="Permalink to this definition"></a></dt>
<dd><p>set the focus and call <code class="docutils literal"><span class="pre">self._modified()</span></code> or raise an <code class="xref py py-exc docutils literal"><span class="pre">IndexError</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="MyV2ListWalker.focus">
<code class="descclassname">MyV2ListWalker.</code><code class="descname">focus</code><a class="headerlink" href="#MyV2ListWalker.focus" title="Permalink to this definition"></a></dt>
<dd><p>attribute or property containing the focus position, or define
<a class="reference internal" href="#MyV1ListWalker.get_focus" title="MyV1ListWalker.get_focus"><code class="xref py py-meth docutils literal"><span class="pre">MyV1ListWalker.get_focus()</span></code></a> as above</p>
</dd></dl>

</div>
<div class="section" id="list-walker-iteration">
<h4>List Walker Iteration<a class="headerlink" href="#list-walker-iteration" title="Permalink to this headline"></a></h4>
<p>There is an optional iteration helper method that may be defined in any list walker.
When this is defined it will be used by <code class="xref py py-meth docutils literal"><span class="pre">ListBox.__iter__()</span></code> and
<code class="xref py py-meth docutils literal"><span class="pre">ListBox.__reversed__()</span></code>:</p>
<dl class="method">
<dt id="MyV2ListWalker.positions">
<code class="descclassname">MyV2ListWalker.</code><code class="descname">positions</code><span class="sig-paren">(</span><em>reverse=False</em><span class="sig-paren">)</span><a class="headerlink" href="#MyV2ListWalker.positions" title="Permalink to this definition"></a></dt>
<dd><p>return a forward or reverse iterable of positions</p>
</dd></dl>

</div>
</div>
</div>
<div class="section" id="custom-widgets">
<h2>Custom Widgets<a class="headerlink" href="#custom-widgets" title="Permalink to this headline"></a></h2>
<p>Widgets in Urwid are easiest to create by extending other widgets. If you are
making a new type of widget that can use other widgets to display its content,
like a new type of button or control, then you should start by extending
<a class="reference internal" href="../reference/widget.html#urwid.WidgetWrap" title="urwid.WidgetWrap"><code class="xref py py-class docutils literal"><span class="pre">WidgetWrap</span></code></a> and passing the display widget to its constructor.</p>
<p>The <a class="reference internal" href="../reference/widget.html#urwid.Widget" title="urwid.Widget"><code class="xref py py-class docutils literal"><span class="pre">Widget</span></code></a> interface is described in detail in the
<a class="reference internal" href="../reference/widget.html#urwid.Widget" title="urwid.Widget"><code class="xref py py-class docutils literal"><span class="pre">Widget</span> <span class="pre">base</span> <span class="pre">class</span> <span class="pre">reference</span></code></a> and is useful if you&#8217;re looking to modify
the behavior of an existing widget,
build a new widget class from scratch or just want a better understanding of
the library.</p>
<p>One Urwid design choice that stands out is that widgets typically have no
size. Widgets don&#8217;t store their size on screen, and instead are
passed that information when they need it.</p>
<p>This choice has some advantages:</p>
<ul class="simple">
<li>widgets may be reused in different locations</li>
<li>reused widgets only need to be rendered once per size displayed</li>
<li>widgets don&#8217;t need to know their parents</li>
<li>less data to store and update</li>
<li>no worrying about widgets that haven&#8217;t received their size yet</li>
<li>same widgets could be displayed at different sizes to different users
simultaneously</li>
</ul>
<p>It also has disadvantages:</p>
<ul class="simple">
<li>difficult to determine a widget&#8217;s size on screen</li>
<li>more parameters to parse</li>
<li>duplicated size calculations across methods</li>
</ul>
<p>For determining a widget&#8217;s size on screen it is possible to look up the size(s)
it was rendered at in the <a class="reference internal" href="../reference/canvas.html#urwid.CanvasCache" title="urwid.CanvasCache"><code class="xref py py-class docutils literal"><span class="pre">CanvasCache</span></code></a>. There are plans
to address some of the duplicated size handling code in the container widgets
in a future Urwid release.</p>
<p>The same holds true for a widget&#8217;s focus state, so that too is passed in to
functions that need it.</p>
<div class="section" id="modifying-existing-widgets">
<h3>Modifying Existing Widgets<a class="headerlink" href="#modifying-existing-widgets" title="Permalink to this headline"></a></h3>
<p>The easiest way to create a custom widget is to modify an existing widget.
This can be done by either subclassing the original widget or by wrapping it.
Subclassing is appropriate when you need to interact at a very low level with
the original widget, such as if you are creating a custom edit widget with
different behavior than the usual Edit widgets. If you are creating a custom
widget that doesn&#8217;t need tight coupling with the original widget then
wrapping is more appropriate.</p>
<p>The <a class="reference internal" href="../reference/widget.html#urwid.WidgetWrap" title="urwid.WidgetWrap"><code class="xref py py-class docutils literal"><span class="pre">WidgetWrap</span></code></a> class simplifies wrapping existing
widgets. You can create a custom widget simply by creating a subclass of
WidgetWrap and passing a widget into WidgetWrap&#8217;s constructor.</p>
<p>This is an example of a custom widget that uses WidgetWrap:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urwid</span>

<span class="k">class</span> <span class="nc">QuestionnaireItem</span><span class="p">(</span><span class="n">urwid</span><span class="o">.</span><span class="n">WidgetWrap</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">options</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">unsure</span> <span class="o">=</span> <span class="n">urwid</span><span class="o">.</span><span class="n">RadioButton</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span> <span class="s2">u&quot;Unsure&quot;</span><span class="p">)</span>
        <span class="n">yes</span> <span class="o">=</span> <span class="n">urwid</span><span class="o">.</span><span class="n">RadioButton</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span> <span class="s2">u&quot;Yes&quot;</span><span class="p">)</span>
        <span class="n">no</span> <span class="o">=</span> <span class="n">urwid</span><span class="o">.</span><span class="n">RadioButton</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span> <span class="s2">u&quot;No&quot;</span><span class="p">)</span>
        <span class="n">display_widget</span> <span class="o">=</span> <span class="n">urwid</span><span class="o">.</span><span class="n">GridFlow</span><span class="p">([</span><span class="n">unsure</span><span class="p">,</span> <span class="n">yes</span><span class="p">,</span> <span class="n">no</span><span class="p">],</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;left&#39;</span><span class="p">)</span>
        <span class="n">urwid</span><span class="o">.</span><span class="n">WidgetWrap</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">display_widget</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_state</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">o</span><span class="o">.</span><span class="n">get_state</span><span class="p">()</span> <span class="ow">is</span> <span class="bp">True</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">o</span><span class="o">.</span><span class="n">get_label</span><span class="p">()</span>
</pre></div>
</td></tr></table></div>
<p>The above code creates a group of RadioButtons and provides a method to
query the state of the buttons.</p>
</div>
<div class="section" id="widgets-from-scratch">
<h3>Widgets from Scratch<a class="headerlink" href="#widgets-from-scratch" title="Permalink to this headline"></a></h3>
<p>Widgets must inherit from <a class="reference internal" href="../reference/widget.html#urwid.Widget" title="urwid.Widget"><code class="xref py py-class docutils literal"><span class="pre">Widget</span></code></a>.
Box widgets must implement <a class="reference internal" href="../reference/widget.html#urwid.Widget.selectable" title="urwid.Widget.selectable"><code class="xref py py-meth docutils literal"><span class="pre">Widget.selectable()</span></code></a> and <a class="reference internal" href="../reference/widget.html#urwid.Widget.render" title="urwid.Widget.render"><code class="xref py py-meth docutils literal"><span class="pre">Widget.render()</span></code></a>
methods, and flow widgets must implement <a class="reference internal" href="../reference/widget.html#urwid.Widget.selectable" title="urwid.Widget.selectable"><code class="xref py py-meth docutils literal"><span class="pre">Widget.selectable()</span></code></a>,
<a class="reference internal" href="../reference/widget.html#urwid.Widget.render" title="urwid.Widget.render"><code class="xref py py-meth docutils literal"><span class="pre">Widget.render()</span></code></a> and <a class="reference internal" href="../reference/widget.html#urwid.Widget.rows" title="urwid.Widget.rows"><code class="xref py py-meth docutils literal"><span class="pre">Widget.rows()</span></code></a> methods.</p>
<p>The default <a class="reference internal" href="../reference/widget.html#urwid.Widget.sizing" title="urwid.Widget.sizing"><code class="xref py py-meth docutils literal"><span class="pre">Widget.sizing()</span></code></a> method returns a set of sizing modes supported
from <code class="docutils literal"><span class="pre">self._sizing</span></code>, so we define <code class="docutils literal"><span class="pre">_sizing</span></code> attributes for our flow and
box widgets below.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urwid</span>

<span class="k">class</span> <span class="nc">Pudding</span><span class="p">(</span><span class="n">urwid</span><span class="o">.</span><span class="n">Widget</span><span class="p">):</span>
    <span class="n">_sizing</span> <span class="o">=</span> <span class="nb">frozenset</span><span class="p">([</span><span class="s1">&#39;flow&#39;</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">rows</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">focus</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">focus</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="p">(</span><span class="n">maxcol</span><span class="p">,)</span> <span class="o">=</span> <span class="n">size</span>
        <span class="n">num_pudding</span> <span class="o">=</span> <span class="n">maxcol</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="s2">&quot;Pudding&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">urwid</span><span class="o">.</span><span class="n">TextCanvas</span><span class="p">([</span><span class="s2">&quot;Pudding&quot;</span> <span class="o">*</span> <span class="n">num_pudding</span><span class="p">],</span> <span class="n">maxcol</span><span class="o">=</span><span class="n">maxcol</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">BoxPudding</span><span class="p">(</span><span class="n">urwid</span><span class="o">.</span><span class="n">Widget</span><span class="p">):</span>
    <span class="n">_sizing</span> <span class="o">=</span> <span class="nb">frozenset</span><span class="p">([</span><span class="s1">&#39;box&#39;</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">focus</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="p">(</span><span class="n">maxcol</span><span class="p">,</span> <span class="n">maxrow</span><span class="p">)</span> <span class="o">=</span> <span class="n">size</span>
        <span class="n">num_pudding</span> <span class="o">=</span> <span class="n">maxcol</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="s2">&quot;Pudding&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">urwid</span><span class="o">.</span><span class="n">TextCanvas</span><span class="p">([</span><span class="s2">&quot;Pudding&quot;</span> <span class="o">*</span> <span class="n">num_pudding</span><span class="p">]</span> <span class="o">*</span> <span class="n">maxrow</span><span class="p">,</span>
                                <span class="n">maxcol</span><span class="o">=</span><span class="n">maxcol</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>The above code implements two widget classes. Pudding is a flow widget and
BoxPudding is a box widget. Pudding will render as much &#8220;Pudding&#8221; as will fit
in a single row, and BoxPudding will render as much &#8220;Pudding&#8221; as will fit into
the entire area given.</p>
<p>Note that the rows and render methods&#8217; focus parameter must have a default
value of False. Also note that for flow widgets the number of rows returned by
the rows method must match the number of rows rendered by the render method.</p>
<p>To improve the efficiency of your Urwid application you should be careful of
how long your <code class="docutils literal"><span class="pre">rows()</span></code> methods take to execute. The <code class="docutils literal"><span class="pre">rows()</span></code> methods may be called many
times as part of input handling and rendering operations. If you are using a
display widget that is time consuming to create you should consider caching it
to reduce its impact on performance.</p>
<p>It is possible to create a widget that will behave as either a flow widget or
box widget depending on what is required:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urwid</span>

<span class="k">class</span> <span class="nc">MultiPudding</span><span class="p">(</span><span class="n">urwid</span><span class="o">.</span><span class="n">Widget</span><span class="p">):</span>
    <span class="n">_sizing</span> <span class="o">=</span> <span class="nb">frozenset</span><span class="p">([</span><span class="s1">&#39;flow&#39;</span><span class="p">,</span> <span class="s1">&#39;box&#39;</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">rows</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">focus</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">focus</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">size</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="p">(</span><span class="n">maxcol</span><span class="p">,)</span> <span class="o">=</span> <span class="n">size</span>
            <span class="n">maxrow</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="p">(</span><span class="n">maxcol</span><span class="p">,</span> <span class="n">maxrow</span><span class="p">)</span> <span class="o">=</span> <span class="n">size</span>
        <span class="n">num_pudding</span> <span class="o">=</span> <span class="n">maxcol</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="s2">&quot;Pudding&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">urwid</span><span class="o">.</span><span class="n">TextCanvas</span><span class="p">([</span><span class="s2">&quot;Pudding&quot;</span> <span class="o">*</span> <span class="n">num_pudding</span><span class="p">]</span> <span class="o">*</span> <span class="n">maxrow</span><span class="p">,</span>
                                <span class="n">maxcol</span><span class="o">=</span><span class="n">maxcol</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>MultiPudding will work in place of either Pudding or BoxPudding above. The
number of elements in the size tuple determines whether the containing widget
is expecting a flow widget or a box widget.</p>
</div>
<div class="section" id="selectable-widgets">
<h3>Selectable Widgets<a class="headerlink" href="#selectable-widgets" title="Permalink to this headline"></a></h3>
<p>Selectable widgets such as Edit and Button widgets allow the user to interact
with the application. A widget is selectable if its selectable method returns
True. Selectable widgets must implement the <a class="reference internal" href="../reference/widget.html#urwid.Widget.keypress" title="urwid.Widget.keypress"><code class="xref py py-meth docutils literal"><span class="pre">Widget.keypress()</span></code></a> method to
handle keyboard input.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urwid</span>

<span class="k">class</span> <span class="nc">SelectablePudding</span><span class="p">(</span><span class="n">urwid</span><span class="o">.</span><span class="n">Widget</span><span class="p">):</span>
    <span class="n">_sizing</span> <span class="o">=</span> <span class="nb">frozenset</span><span class="p">([</span><span class="s1">&#39;flow&#39;</span><span class="p">])</span>
    <span class="n">_selectable</span> <span class="o">=</span> <span class="bp">True</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pudding</span> <span class="o">=</span> <span class="s2">&quot;pudding&quot;</span>

    <span class="k">def</span> <span class="nf">rows</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">focus</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">focus</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="p">(</span><span class="n">maxcol</span><span class="p">,)</span> <span class="o">=</span> <span class="n">size</span>
        <span class="n">num_pudding</span> <span class="o">=</span> <span class="n">maxcol</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pudding</span><span class="p">)</span>
        <span class="n">pudding</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pudding</span>
        <span class="k">if</span> <span class="n">focus</span><span class="p">:</span>
            <span class="n">pudding</span> <span class="o">=</span> <span class="n">pudding</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">urwid</span><span class="o">.</span><span class="n">TextCanvas</span><span class="p">([</span><span class="n">pudding</span> <span class="o">*</span> <span class="n">num_pudding</span><span class="p">],</span>
            <span class="n">maxcol</span><span class="o">=</span><span class="n">maxcol</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">keypress</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="p">(</span><span class="n">maxcol</span><span class="p">,)</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">key</span>
        <span class="k">if</span> <span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">pudding</span><span class="p">:</span>
            <span class="c1"># remove letter from pudding</span>
            <span class="n">n</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pudding</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pudding</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pudding</span><span class="p">[:</span><span class="n">n</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">pudding</span><span class="p">[</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">pudding</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pudding</span> <span class="o">=</span> <span class="s2">&quot;pudding&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_invalidate</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">key</span>
</pre></div>
</div>
<p>The SelectablePudding widget will display its contents in uppercase when it is
in focus, and it allows the user to &#8220;eat&#8221; the pudding by pressing each of the
letters <em>P</em>, <em>U</em>, <em>D</em>, <em>D</em>, <em>I</em>, <em>N</em> and <em>G</em> on the keyboard. When the user has
&#8220;eaten&#8221; all the pudding the widget will reset to its initial state.</p>
<p>Note that keys that are unhandled in the keypress method are returned so that
another widget may be able to handle them. This is a good convention to follow
unless you have a very good reason not to. In this case the <em>UP</em> and <em>DOWN</em>
keys are returned so that if this widget is in a
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> the <a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> will behave
as the user expects and change the focus or scroll the
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a>.</p>
</div>
<div class="section" id="widget-displaying-the-cursor">
<h3>Widget Displaying the Cursor<a class="headerlink" href="#widget-displaying-the-cursor" title="Permalink to this headline"></a></h3>
<p>Widgets that display the cursor must implement the
<a class="reference internal" href="../reference/widget.html#urwid.Widget.get_cursor_coords" title="urwid.Widget.get_cursor_coords"><code class="xref py py-meth docutils literal"><span class="pre">Widget.get_cursor_coords()</span></code></a> method.
Similar to the rows method for flow widgets, this method lets other widgets
make layout decisions without rendering the entire widget. The
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> widget in particular uses get_cursor_coords to
make sure that the cursor is visible within its focus widget.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 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</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urwid</span>

<span class="k">class</span> <span class="nc">CursorPudding</span><span class="p">(</span><span class="n">urwid</span><span class="o">.</span><span class="n">Widget</span><span class="p">):</span>
    <span class="n">_sizing</span> <span class="o">=</span> <span class="nb">frozenset</span><span class="p">([</span><span class="s1">&#39;flow&#39;</span><span class="p">])</span>
    <span class="n">_selectable</span> <span class="o">=</span> <span class="bp">True</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cursor_col</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">rows</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">focus</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">focus</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="p">(</span><span class="n">maxcol</span><span class="p">,)</span> <span class="o">=</span> <span class="n">size</span>
        <span class="n">num_pudding</span> <span class="o">=</span> <span class="n">maxcol</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="s2">&quot;Pudding&quot;</span><span class="p">)</span>
        <span class="n">cursor</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">if</span> <span class="n">focus</span><span class="p">:</span>
            <span class="n">cursor</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cursor_coords</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">urwid</span><span class="o">.</span><span class="n">TextCanvas</span><span class="p">([</span><span class="s2">&quot;Pudding&quot;</span> <span class="o">*</span> <span class="n">num_pudding</span><span class="p">],</span> <span class="p">[],</span> <span class="n">cursor</span><span class="p">,</span> <span class="n">maxcol</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_cursor_coords</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
        <span class="p">(</span><span class="n">maxcol</span><span class="p">,)</span> <span class="o">=</span> <span class="n">size</span>
        <span class="n">col</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cursor_col</span><span class="p">,</span> <span class="n">maxcol</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">col</span><span class="p">,</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">keypress</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="p">(</span><span class="n">maxcol</span><span class="p">,</span> <span class="p">)</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="s1">&#39;left&#39;</span><span class="p">:</span>
            <span class="n">col</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cursor_col</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="k">elif</span> <span class="n">key</span> <span class="o">==</span> <span class="s1">&#39;right&#39;</span><span class="p">:</span>
            <span class="n">col</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cursor_col</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">key</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cursor_x</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">maxcol</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">col</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_invalidate</span><span class="p">()</span>
</pre></div>
</td></tr></table></div>
<p>CursorPudding will let the user move the cursor through the widget by pressing
<em>LEFT</em> and <em>RIGHT</em>. The cursor must only be added to the canvas when the widget
is in focus. The get_cursor_coords method must always return the same cursor
coordinates that render does.</p>
<p>A widget displaying a cursor may choose to implement <a class="reference internal" href="../reference/widget.html#urwid.Widget.get_pref_col" title="urwid.Widget.get_pref_col"><code class="xref py py-meth docutils literal"><span class="pre">Widget.get_pref_col()</span></code></a>.
This method
returns the preferred column for the cursor, and is called when the focus is
moving up or down off this widget.</p>
<p>Another optional method is <a class="reference internal" href="../reference/widget.html#urwid.Widget.move_cursor_to_coords" title="urwid.Widget.move_cursor_to_coords"><code class="xref py py-meth docutils literal"><span class="pre">Widget.move_cursor_to_coords()</span></code></a>. This method allows other
widgets to try to position the cursor within this widget. The
<a class="reference internal" href="../reference/widget.html#urwid.ListBox" title="urwid.ListBox"><code class="xref py py-class docutils literal"><span class="pre">ListBox</span></code></a> widget uses <a class="reference internal" href="../reference/widget.html#urwid.Widget.move_cursor_to_coords" title="urwid.Widget.move_cursor_to_coords"><code class="xref py py-meth docutils literal"><span class="pre">Widget.move_cursor_to_coords()</span></code></a> when
changing focus and when the user pressed <em>PAGE UP</em> or <em>PAGE DOWN</em>. This method
must return <code class="docutils literal"><span class="pre">True</span></code> on success and <code class="docutils literal"><span class="pre">False</span></code> on failure. If the cursor may be
placed at any position within the row specified (not only at the exact column
specified) then this method must move the cursor to that position and return
<code class="docutils literal"><span class="pre">True</span></code>.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7</pre></div></td><td class="code"><div class="highlight"><pre>    <span class="k">def</span> <span class="nf">get_pref_col</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="p">(</span><span class="n">maxcol</span><span class="p">,)):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cursor_x</span>

    <span class="k">def</span> <span class="nf">move_cursor_to_coords</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="p">(</span><span class="n">maxcol</span><span class="p">,),</span> <span class="n">col</span><span class="p">,</span> <span class="n">row</span><span class="p">):</span>
        <span class="k">assert</span> <span class="n">row</span> <span class="o">==</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cursor_x</span> <span class="o">=</span> <span class="n">col</span>
        <span class="k">return</span> <span class="bp">True</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="widget-metaclass">
<h2>Widget Metaclass<a class="headerlink" href="#widget-metaclass" title="Permalink to this headline"></a></h2>
<p>The <a class="reference internal" href="../reference/widget.html#urwid.Widget" title="urwid.Widget"><code class="xref py py-class docutils literal"><span class="pre">Widget</span></code></a> base class has a metaclass defined that
creates a <code class="docutils literal"><span class="pre">__super</span></code> attribute for calling your superclass:
<code class="docutils literal"><span class="pre">self.__super</span></code> is the same as the usual <code class="docutils literal"><span class="pre">super(MyClassName,</span> <span class="pre">self)</span></code>.
This shortcut is of little use with Python 3&#8217;s new <code class="docutils literal"><span class="pre">super()</span></code> syntax, but
will likely be retained for backwards compatibility in future versions.</p>
<p>This metaclass also uses <code class="xref py py-class docutils literal"><span class="pre">MetaSignal</span></code>
to allow signals to be defined as a list of signal names
in a <code class="docutils literal"><span class="pre">signals</span></code> class attribute.  This is equivalent to calling
<a class="reference internal" href="../reference/signals.html#urwid.register_signal" title="urwid.register_signal"><code class="xref py py-func docutils literal"><span class="pre">register_signal()</span></code></a> with the class name and list of signals and all those
defined in superclasses after the class definition.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="../reference/meta.html#urwid.WidgetMeta" title="urwid.WidgetMeta"><code class="xref py py-class docutils literal"><span class="pre">Widget</span> <span class="pre">metaclass</span> <span class="pre">WidgetMeta</span></code></a></p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/urwid-logo.png" alt="Logo"/>
            </a></p><h3>Table of contents</a></h3>
    <ul><li><a href="index.html">Manual</a></li><ul>
<li><a class="reference internal" href="#">Widgets</a><ul>
<li><a class="reference internal" href="#widget-layout">Widget Layout</a></li>
<li><a class="reference internal" href="#box-flow-and-fixed-widgets">Box, Flow and Fixed Widgets</a></li>
<li><a class="reference internal" href="#included-widgets">Included Widgets</a></li>
<li><a class="reference internal" href="#decoration-widgets">Decoration Widgets</a></li>
<li><a class="reference internal" href="#container-widgets">Container Widgets</a><ul>
<li><a class="reference internal" href="#pile-widgets">Pile Widgets</a></li>
<li><a class="reference internal" href="#columns-widgets">Columns Widgets</a></li>
<li><a class="reference internal" href="#gridflow-widgets">GridFlow Widgets</a></li>
<li><a class="reference internal" href="#overlay-widgets">Overlay Widgets</a></li>
</ul>
</li>
<li><a class="reference internal" href="#listbox-contents">ListBox Contents</a><ul>
<li><a class="reference internal" href="#listbox-focus-and-scrolling">ListBox Focus and Scrolling</a></li>
<li><a class="reference internal" href="#dynamic-listbox-with-listwalker">Dynamic ListBox with ListWalker</a></li>
<li><a class="reference internal" href="#setting-the-focus">Setting the Focus</a></li>
<li><a class="reference internal" href="#list-walkers">List Walkers</a></li>
<li><a class="reference internal" href="#list-walker-interface">List Walker Interface</a><ul>
<li><a class="reference internal" href="#list-walker-api-version-1">List Walker API Version 1</a></li>
<li><a class="reference internal" href="#list-walker-api-version-2">List Walker API Version 2</a></li>
<li><a class="reference internal" href="#list-walker-iteration">List Walker Iteration</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#custom-widgets">Custom Widgets</a><ul>
<li><a class="reference internal" href="#modifying-existing-widgets">Modifying Existing Widgets</a></li>
<li><a class="reference internal" href="#widgets-from-scratch">Widgets from Scratch</a></li>
<li><a class="reference internal" href="#selectable-widgets">Selectable Widgets</a></li>
<li><a class="reference internal" href="#widget-displaying-the-cursor">Widget Displaying the Cursor</a></li>
</ul>
</li>
<li><a class="reference internal" href="#widget-metaclass">Widget Metaclass</a></li>
</ul>
</li>
</ul>

    </ul>
  <h4>Previous topic</h4>
  <p class="topless"><a href="displaymodules.html"
                        title="previous chapter">Display Modules</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="userinput.html"
                        title="next chapter">User Input</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/manual/widgets.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="userinput.html" title="User Input"
             >next</a> |</li>
        <li class="right" >
          <a href="displaymodules.html" title="Display Modules"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Urwid 1.3.1</a> &raquo;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >Urwid Manual</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2014, Ian Ward et al.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.6.
    </div>
  </body>
</html>