📄 tutorial.html
字号:
<span style="color:black;background:silver">Bob</span><span style="color:silver;background:black"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span></pre></div><div class="shot"><pre><span style="color:black;background:teal">Press F8 to exit. </span><span style="color:black;background:silver">Nice to meet you, Abe</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">What is your name?</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">Bob </span><span style="color:black;background:silver">Nice to meet you, Bob</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">What is your name?</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">Carl </span><span style="color:black;background:silver">Nice to meet you, Carl</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">What is your name?</span><span style="color:black;background:silver"> </span><span style="color:silver;background:black"> </span><span style="color:black;background:silver"> </span></pre></div><div class="shot"><pre><span style="color:black;background:teal">Press F8 to exit. </span><span style="color:black;background:silver">Nice to meet you, Bob</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">What is your name?</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">Carl </span><span style="color:black;background:silver">Nice to meet you, Carl</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">What is your name?</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">Dave </span><span style="color:black;background:silver">Nice to meet you, Dave</span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver">What is your name?</span><span style="color:black;background:silver"> </span><span style="color:silver;background:black"> </span><span style="color:black;background:silver"> </span></pre></div><br clear="left"><br><h2>3. Zen of ListBox</h2><h3><a name="lbscr">3.1. ListBox Focus and Scrolling</a><span class="back">[<a href="#top">back to top</a>]</span></h3>The <a href="reference.html#ListBox">ListBox</a> is a box widget that contains flow widgets. Its contentsare displayed stacked vertically, and the ListBox allows the user to scrollthrough its content. One of the flow widgets displayedin the ListBox is the focus widget. The ListBox passes key presses to thefocus widget to allow the user to interact with it. If thefocus widget does not handle a keypress then the ListBox may handle thekeypress by scrolling and/or selecting another widget to become the focuswidget.<br><br>The ListBox tries to do the most sensible thing when scrolling andchanging focus. When the widgets displayed are all Text widgets orother unselectable widgets then the ListBox will behave like a web browserdoes when the user presses UP, DOWN, PAGE UP and PAGE DOWN: new text isimmediately scrolled in from the top or bottom. The ListBox chooses one of thevisible widgets as its focus widget when scrolling. When scrolling up theListBox chooses the topmost widget as the focus, and when scrolling down the ListBox chooses the bottommost widget as the focus.<br><br>When all the widgets displayed are not selectable the user would typicallyhave no way to tell which widget is in focus, but if we wrap the widgets withAttrWrap we can see what is happening while the focus changes:<pre class="code">CONTENT = [ urwid.AttrWrap( w, None, 'reveal focus' ) for w in [ urwid.Text("This is a text string that is fairly long"), urwid.Divider("-"), urwid.Text("Short one"), urwid.Text("Another"), urwid.Divider("-"), urwid.Text("What could be after this?"), urwid.Text("The end."),] ]</pre><div class="shot"><pre><span style="color:white;background:black">Pressed: </span><span style="color:black;background:teal">This is a text </span><span style="color:black;background:teal">string that is </span><span style="color:black;background:teal">fairly long </span><span style="color:black;background:silver">---------------</span><span style="color:black;background:silver">Short one </span><span style="color:black;background:silver">Another </span></pre></div><div class="shot"><pre><span style="color:white;background:black">Pressed: down </span><span style="color:black;background:silver">string that is </span><span style="color:black;background:silver">fairly long </span><span style="color:black;background:silver">---------------</span><span style="color:black;background:silver">Short one </span><span style="color:black;background:silver">Another </span><span style="color:black;background:teal">---------------</span></pre></div><div class="shot"><pre><span style="color:white;background:black">Pressed: down </span><span style="color:black;background:silver">fairly long </span><span style="color:black;background:silver">---------------</span><span style="color:black;background:silver">Short one </span><span style="color:black;background:silver">Another </span><span style="color:black;background:silver">---------------</span><span style="color:black;background:teal">What could be </span></pre></div><div class="shot"><pre><span style="color:white;background:black">Pressed: down </span><span style="color:black;background:silver">---------------</span><span style="color:black;background:silver">Short one </span><span style="color:black;background:silver">Another </span><span style="color:black;background:silver">---------------</span><span style="color:black;background:teal">What could be </span><span style="color:black;background:teal">after this? </span></pre></div><div class="shot"><pre><span style="color:white;background:black">Pressed: up </span><span style="color:black;background:teal">fairly long </span><span style="color:black;background:silver">---------------</span><span style="color:black;background:silver">Short one </span><span style="color:black;background:silver">Another </span><span style="color:black;background:silver">---------------</span><span style="color:black;background:silver">What could be </span></pre></div><div class="shot"><pre><span style="color:white;background:black">Pressed: up </span><span style="color:black;background:teal">string that is </span><span style="color:black;background:teal">fairly long </span><span style="color:black;background:silver">---------------</span><span style="color:black;background:silver">Short one </span><span style="color:black;background:silver">Another </span><span style="color:black;background:silver">---------------</span></pre></div><br clear="left">The ListBox remembers the location of the widget in focus as either an"offset" or an "inset". An offset is the number of rows between the topof the ListBox and the beginning of the focus widget. An offset of zerocorresponds to a widget with its top aligned with the top of the ListBox.An inset is the fraction of rows of the focus widgetthat are "above" the top of the ListBox and not visible. The ListBox uses this method of remembering the focus widget locationso that when the ListBox is resized the text displayed will stay roughly aligned with the top of the ListBox.<br><br><div class="shot"><pre><span style="color:white;background:black">Pressed: </span><span style="color:black;background:teal">string that is </span><span style="color:black;background:teal">fairly long </span><span style="color:black;background:silver">--------------------</span><span style="color:black;background:silver">Short one </span><span style="color:black;background:silver">Another </span><span style="color:black;background:silver">--------------------</span><span style="color:black;background:silver">What could be after </span><span style="color:black;background:silver">this? </span></pre></div><div class="shot"><pre><span style="color:white;background:black">Pressed: </span><span style="color:black;background:teal">This is a text string </span><span style="color:black;background:teal">that is fairly long </span><span style="color:black;background:silver">-------------------------</span><span style="color:black;background:silver">Short one </span><span style="color:black;background:silver">Another </span><span style="color:black;background:silver">-------------------------</span></pre></div><div class="shot"><pre><span style="color:white;background:black">Pressed: </span><span style="color:black;background:teal">This is a </span><span style="color:black;background:teal">text string</span><span style="color:black;background:teal">that is </span><span style="color:black;background:teal">fairly long</span><span style="color:black;background:silver">-----------</span><span style="color:black;background:silver">Short one </span><span style="color:black;background:silver">Another </span><span style="color:black;background:silver">-----------</span><span style="color:black;background:silver">What could </span><span style="color:black;background:silver">be after </span><span style="color:black;background:silver">this? </span><span style="color:black;background:silver">The end. </span></pre></div><br clear="left"><br><br>When there are selectable widgets in the ListBox the focus will movebetween the selectable widgets, skipping the unselectable widgets.The ListBox will try to scroll all the rows of a selectable widget intoview 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 viewby using composite widgets to combine a selectable widget with otherwidgets that should be displayed at the same time.<br clear="left"><br><h3><a name="lbdyn">3.2. Dynamic ListBox with List Walker</a><span class="back">[<a href="#top">back to top</a>]</span></h3>While the ListBox stores the location of its focus widget, itdoes not directly store the actual focus widget or other contents of theListBox. The storage of a ListBox's content is delegated to a "List Walker" object. If a list of widgets is passed to the ListBox constructor then itcreates a <a href="reference.html#SimpleListWalker">SimpleListWalker</a> object to manage the list.<br><br>When the ListBox is <a href="reference.html#ListBox-render">rendering a canvas</a> or <a href="reference.html#ListBox-keypress">handling input</a> it will:<ol><li>Call the <a href="reference.html#List_Walker_interface_definition-get_focus">get_focus</a> method of its list walker object. This method will return the focus widget and a position object. <li>Optionally call the <a href="reference.html#List_Walker_interface_definition-get_prev">get_prev</a> 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"above" the position passed.<li>Optionally call the <a href="reference.html#List_Walker_interface_definition-get_next">get_next</a> method of its List Walker object one or more times, similarly, to collect widgets and position objects "below" the focus position.<li>Optionally call the <a href="reference.html#List_Walker_interface_definition-set_focus">set_focus</a> method passing one of the position objects returned in the previous steps.</ol>This is the only way the ListBox accesses its contents, and it will notstore copies of any of the widgets or position objects beyond the currentrendering or input handling operation.<br><br>The SimpleListWalker stores a list of widgets, and uses integer indexes intothis list as its position objects. It stores the focusposition as an integer, so if you insert a widget into the list above thefocus position then you need to remember to increment the focus position inthe SimpleListWalker object or the contents of the ListBox will shift.<br><br>A custom List Walker object may be passed to the ListBox constructor insteadof a plain list of widgets. List Walker objects must implement the <a href="reference.html#List_Walker_interface_definition">List Walker interface</a>.<br><br>The <a href="fib.py.html">fib.py</a> example program demonstrates a custom list walker that doesn'tstore any widgets. It uses a tuple of two successive Fibonacci numbersas its position objects and it generates Text widgets to display the numberson the fly. The result is a ListBox that can scroll through an unending list of widgets.<br><br>The <a href="edit.py.html">edit.py</a> example program demonstrates a custom list walker thatloads lines from a text file only as the user scrolls them into view. This allows even huge files to be opened almost instantly.<br><br>The <a href="browse.py.html">browse.py</a>example program demonstrates a custom list walker thatuses a tuple of strings as position objects, one for the parent directoryand one for the file selected. The widgets are cached in a separate classthat is accessed using a dictionary indexed by parent directory names.This allows the directories to be read only as required. The custom listwalker also allows directories to be hidden from view when they are"collapsed".<br clear="left"><br><h3><a name="lbfocus">3.3. Setting the Focus</a><span class="back">[<a href="#top">back to top</a>]</span></h3>The easiest way to change the current ListBox focus is to call the<a href="reference.html#ListBox-set_focus">set_focus</a> method.This method doesn't require that you know the ListBox'scurrent dimensions (maxcol, maxrow). It will wait until the next call toeither keypress or render to complete setting the offset and inset values using the dimensions passed to that method.<br><br>The position object passed to set_focus must be compatible with the ListWalker object that the ListBox is using. For SimpleListWalker the positionis the integer index of the widget within the list.<br><br>The coming_from parameter should be set if you know that the old positionis "above" or "below" the previous position. When the ListBox completessetting the offset and inset values it tries to find the old widget amongthe visible widgets. If the old widget is still visible, if will try toavoid causing the ListBox contents to scroll up or down from its previousposition. If the widget is not visible, then the ListBox will:<ul><li>Display the new focus at the bottom of the ListBox if coming_from is"above".<li>Display the new focus at the top of the ListBox if coming_from is "below".<li>Display the new focus in the middle of the ListBox if coming_from is None.</ul>If you know exactly where you want to display the new focus widget withinthe ListBox you may call <a href="reference.html#ListBox-set_focus_valign">set_focus_valign</a>. This method lets you specify the"top", "bottom", "middle", a relative position or the exact number of rowsfrom the top or bottom of the ListBox.<br clear="left"><br><h2>4. Combining Widgets</h2><h3><a name="pile">4.1. Piling Widgets</a><span class="back">[<a href="#top">back to top</a>]</span></h3><a href="reference.html#Pile">Pile</a> widgets are used to combine multiple widgets by stacking themvertically. A Pile can manage selectable widgets by keeping track of whichwidget is in focus and it can handle moving the focus between widgets whenthe user presses the UP and DOWN keys. A Pile will also work well when used within a ListBox.<br><br>A Pile is selectable only if its focus widget is selectable. If you createa Pile containing one Text widget and one Edit widget the Pile willchoose the Edit widget as its default focus widget.To change the pile's focus widget you can call<a href="reference.html#Pile-set_focus">set_focus</a>.<br clear="left"><br><h3><a name="cols">4.2. Dividing into Columns</a><span class="back">[<a href="#top">back to top</a>]</span></h3>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -