⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tmpl_tutorial.html

📁 Urwid is a Python library for making text console applications. It has many features including fluid
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<pre class="code">%example[0]%</pre><ul><li>An <a href="reference.html#Edit">Edit</a> widget is created with thecaption "What is your name?". A newline at the end of the caption makesthe user input start on the next row.<li>A <a href="reference.html#Filler">Filler</a> widget is created to wrapthe edit widget. Its <a href="reference.html#Filler-render">render</a>function is called to create the canvas. The render function is called withthe optional parameter "focus" set to True. This parameter allows thewrapped Edit widget to render its cursor.<li>Keys are processed one at a time. Most keys are sent to the Filler widget's<a href="reference.html#Filler-keypress">keypress</a> function which willcall the Edit widget's <a href="reference.html#Edit-keypress">keypress</a>function to handle the key.<li>Once the ENTER key is pressed the wrapped object in the Filler widgetis changed to a reply text.<li>Any keypress then causes the program to exit.</ul>The Edit widget has many capabilities. It lets you make corrections and movethe cursor around with the HOME, END and arrow keys. It is based on the Textwidget so it supports the same wrapping and alignment modes.<div class="shot">%result[0]%</div><div class="shot">%result[1]%</div><div class="shot">%result[2]%</div>{/body[edit]}<br clear="left"><hr>{body[frlb]}This program asks for your name and responds "Nice to meet you, (your name)"<i>while</i> you type your name.  F8 exits.<pre class="code">%example[0]%</pre><ul><li>In the __init__ function a list called self.items is created. It contains an Edit widget with the caption "What is your name?".<li>A <a href="reference.html#ListBox">ListBox</a> widget called self.listbox is created that is passed the self.items list.This ListBox widget will display and scroll through the widgets in that list.ListBox widgets default to using the first item in the list as the focus.<li>A <a href="reference.html#Frame">Frame</a> widget called self.topis created that contains self.listbox and some header text. Frame widgetswrap around a box widget and may have header and footer flow widgets.The header and footer are always displayed. The contained box widget uses theremaining space in between.<li>When a key is pressed the reply text is inserted or updated in self.items.  This updated text will be displayed by self.listbox.</ul>When changing the contents of ListBox widgets remember to use in-place editing operations on the list, eg. "list = list + [something]" will not work,use "list += [something]" instead.  The former code will create a new listbut the ListBox will still be displaying the old list.<div class="shot">%result[0]%</div><div class="shot">%result[1]%</div><div class="shot">%result[2]%</div><div class="shot">%result[3]%</div>{/body[frlb]}<br clear="left"><hr>{body[lbcont]}This program asks for your name and responds "Nice to meet you, (your name)."It then asks again, and again. Old values may be changed and the responseswill be updated when you press ENTER. F8 exits.<br><br>Update the <a href="#frlb">previous program</a> with this code:<pre class="code">%example[0]%</pre><ul><li>In this version only the ENTER key receives special attention. When theuser presses ENTER:  <ul>  <li>The widget in focus and its current position is retrieved by calling the  <a href="reference.html#ListBox-get_focus">get_focus</a> function.  <li>If the widget in focus does not have an edit_text attribute, then it  is not one of the Edit widgets we are interested in.   One of the Text widgets might receive focus  if it covers the entire visible area of the ListBox widget and there is  no Edit widget to take focus. While this is unlikely, it should be handled  or the program will fail when trying to call   <a href="reference.html#Edit-get_edit_text">get_edit_text</a>.  <li>If the current position is at the end of the list, a response is   appended followed by a new question. If the current position is not at the  end then a previous response is replaced with an updated one.  <li>The cursor position is moved to the far left by calling   <a href="reference.html#Edit-set_edit_pos">set_edit_pos</a>.  <li>Finally, the focus is moved down two positions to the next question   by calling  <a href="reference.html#ListBox-set_focus">set_focus</a>.  </ul><li>All other keys are passed to the top widget to handle. The ListBox widgetdoes most of the hard work:  <ul>  <li>UP and DOWN will change the focus and/or scroll the widgets in the list  box.  <li>PAGE UP and PAGE DOWN will try to move the focus one screen up or down.  <li>The cursor's column is maintained as best as possible when moving  from one Edit widget to another.  </ul></ul><div class="shot">%result[0]%</div><div class="shot">%result[1]%</div><div class="shot">%result[2]%</div>{/body[lbcont]}<br clear="left"><hr>{body[lbscr]}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">%example[0]%</pre><div class="shot">%result[0]%</div><div class="shot">%result[1]%</div><div class="shot">%result[2]%</div><div class="shot">%result[3]%</div><div class="shot">%result[4]%</div><div class="shot">%result[5]%</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">%result[6]%</div><div class="shot">%result[7]%</div><div class="shot">%result[8]%</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.{/body[lbscr]}<br clear="left"><hr>{body[lbdyn]}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".{/body[lbdyn]}<br clear="left"><hr>{body[lbfocus]}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -