📄 tutorial.html
字号:
<html><head><title>Urwid 0.9.8.4 Tutorial</title><style type="text/css"> h1 { text-align: center; } h2 { margin: 40px 0 0 0; padding: 10px; background: #6d96e8;} h3 { margin: 0 0 3px 0; padding: 12px 6px 6px 6px; background: #efef96;} .code { background: #dddddd; padding: 5px; margin: 7px 20px; } .l1 { margin: 12px 0 0 0; } .l2 { margin-left: 20px; } .shot { padding: 5px 20px 5px 0px; float: left; } .back { font-size:small; padding-left: 20px; }</style><body><a name="top"></a><h1>Urwid 0.9.8.4 Tutorial</h1><div style="text-align: center;"><a href="http://excess.org/urwid/">Urwid Home Page</a> /<a href="http://excess.org/urwid/examples.html">Example Screenshots</a> /<a href="http://excess.org/urwid/utf8examples.html">UTF-8 Screenshots</a> /Tutorial /<a href="reference.html">Reference</a></div><br><table width="100%"><tr><td width="50%" valign="top"><div class="l1">1. Hello World Example</div><div class="l2"><a href="#min">1.1. Minimal Urwid Application</a></div><div class="l2"><a href="#text">1.2. Text and Filler Widgets</a></div><div class="l2"><a href="#attr">1.3. AttrWrap Widgets and Text Attributes</a></div><div class="l2"><a href="#resize">1.4. Live Resizing</a></div><div class="l1">2. Conversation Example</div><div class="l2"><a href="#edit">2.1. Edit Widgets</a></div><div class="l2"><a href="#frlb">2.2. Frame and ListBox Widgets</a></div><div class="l2"><a href="#lbcont">2.3. Modifying ListBox Content</a></div><div class="l1">3. Zen of ListBox</div><div class="l2"><a href="#lbscr">3.1. ListBox Focus and Scrolling</a></div><div class="l2"><a href="#lbdyn">3.2. Dynamic ListBox with List Walker</a></div><div class="l2"><a href="#lbfocus">3.3. Setting the Focus</a></div></td><td width="50%" valign="top"><div class="l1">4. Combining Widgets</div><div class="l2"><a href="#pile">4.1. Piling Widgets</a></div><div class="l2"><a href="#cols">4.2. Dividing into Columns</a></div><div class="l2"><a href="#grid">4.3. GridFlow Arrangement</a></div><div class="l2"><a href="#overlay">4.4. Overlay Widgets</a></div><div class="l1">5. Creating Custom Widgets</div><div class="l2"><a href="#wmod">5.1. Modifying Existing Widgets</a></div><div class="l2"><a href="#wanat">5.2. Anatomy of a Widget</a></div><div class="l2"><a href="#wsel">5.3. Creating Selectable Widgets</a></div><div class="l2"><a href="#wcur">5.4. Widgets Displaying the Cursor</a></div></td></tr></table><h2>1. Hello World Example</h2><h3><a name="min">1.1. Minimal Urwid Application</a><span class="back">[<a href="#top">back to top</a>]</span></h3>This program displays the string "Hello World" in the top left cornerof the screen and waits for a keypress before exiting.<pre class="code">import urwid.curses_displayimport urwidui = urwid.curses_display.Screen()def run(): canvas = urwid.TextCanvas(["Hello World"]) ui.draw_screen( (20, 1), canvas ) while not ui.get_input(): passui.run_wrapper( run )</pre><ul><li>The <a href="reference.html#curses_display.Screen">curses_display.Screen</a>class provides access to the curses library. Its member function<a href="reference.html#Screen-run_wrapper">run_wrapper</a> initializescurses full-screen mode and then calls the "run" function passed. It willalso take care of restoring the screen when the "run" function exits.<li>A <a href="reference.html#TextCanvas">TextCanvas</a> is createdcontaining one row with the string "Hello World".<li>The canvas is passed to the <a href="reference.html#Screen-draw_screen">draw_screen</a> function alongwith a fixed screen size of 20 columns and 1 row. If theterminal window this program is run from is larger than 20 by 1 the text will appear in the top left corner.<li>The <a href="reference.html#Screen-get_input">get_input</a> functionis then called until it returns something. It must be called in a loopbecause by default it will time out after one second and return an empty list when there is no input.</ul>Creating canvases directly in this way is generally only done whenwriting custom widget classes. Note that the draw_screenfunction must be passed a canvas and a screen size that matches it,for backwards compatibility.<div align="center"><pre><span style="color:black;background:silver">Hello World</span></pre></div><br clear="left"><br><h3><a name="text">1.2. Text and Filler Widgets</a><span class="back">[<a href="#top">back to top</a>]</span></h3>This program displays the string "Hello World" in the center of the screenand waits for a keypress before exiting.<pre class="code">import urwid.curses_displayimport urwidui = urwid.curses_display.Screen()def run(): cols, rows = ui.get_cols_rows() txt = urwid.Text("Hello World", align="center") fill = urwid.Filler( txt ) canvas = fill.render( (cols, rows) ) ui.draw_screen( (cols, rows), canvas ) while not ui.get_input(): passui.run_wrapper( run )</pre><ul><li><a href="reference.html#Screen-get_cols_rows">get_cols_rows</a>is used to get the dimensions from the terminal and store them as "cols"and "rows".<li>A <a href="reference.html#Text">Text</a> widget is created containingthe string "Hello World". It is set to display with "center" alignment. Text widgets are a kind of <a href="reference.html#FlowWidget">FlowWidget</a>.Flow widgets can fill one or more rows, depending on their content and the number of columns available. Text widgets use more than one row whenthey contain newline characters or when the text must be split across rows.<li>A <a href="reference.html#Filler">Filler</a> widget is created towrap the text widget. Filler widgets are a kind of <a href="reference.html#BoxWidget">BoxWidget</a>. Box widgets have a fixednumber of columns and rows displayed. This widget will pad the "Hello World" text widget until it fills the required number of rows.<li>A canvas is created by calling the <a href="reference.html#Filler-render">render</a> function on the topmostwidget. The filler render function will call the render function of the "Hello World" text widget and combine its canvas withpadding rows to fill the terminal window.</ul>Flow widgets and box widgets are not interchangeable. The first parameterof the render function of a box widget is a two-element tuple (columns,rows) and the first parameter of the render function of a flow widget is a one-element tuple (columns, ). This difference makes sure that when the wrong type of widget is used,such as a box widget inside a filler widget, a ValueError exception will be thrown.<div align="center"><pre><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"> Hello World </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span><span style="color:black;background:silver"> </span></pre></div><br clear="left"><br><h3><a name="attr">1.3. AttrWrap Widgets and Text Attributes</a><span class="back">[<a href="#top">back to top</a>]</span></h3>This program displays the string "Hello World" in the center of the screen.It uses different attributes for the text, the space on either sideof the text and the space above and below the text. It waits for a keypress before exiting.<pre class="code">import urwid.curses_displayimport urwidui = urwid.curses_display.Screen()ui.register_palette( [ ('banner', 'black', 'light gray', ('standout', 'underline')), ('streak', 'black', 'dark red', 'standout'), ('bg', 'black', 'dark blue'), ] )def run(): cols, rows = ui.get_cols_rows() txt = urwid.Text(('banner', " Hello World "), align="center") wrap1 = urwid.AttrWrap( txt, 'streak' ) fill = urwid.Filler( wrap1 ) wrap2 = urwid.AttrWrap( fill, 'bg' ) canvas = wrap2.render( (cols, rows) ) ui.draw_screen( (cols, rows), canvas ) while not ui.get_input(): passui.run_wrapper( run )</pre><ul><li>After creating the <a href="reference.html#curses_display.Screen">curses_display.Screen</a> objectand before calling <a href="reference.html#Screen-run_wrapper">run_wrapper</a>,<a href="reference.html#Screen-register_palette">register_palette</a> is calledto set up some attributes: <ul> <li>"banner" is black text on a light gray background, or reversed attributes and underlined in monochrome mode <li>"streak" is black text on a dark red background, or reversed attributes in monochrome mode <li>"bg" is black text on a dark blue background, or normal in monochrome mode </ul><li>A <a href="reference.html#Text">Text</a> widget is created containingthe string " Hello World " with attribute "banner". The attributes of textin a Text widget is set by using a (attribute, text) tuple instead of asimple text string.<li>An <a href="reference.html#AttrWrap">AttrWrap</a> widget is created towrap the text widget with attribute "streak". AttrWrap widgets will set the attribute of everything that they wrap that does not already have anattribute set. In this case the text has an attribute, so only the areasaround the text used for alignment will be have the new attribute.<li>A <a href="reference.html#Filler">Filler</a> widget is created towrap the AttrWrap widget and fill the rows above and below it.<li>A second <a href="reference.html#AttrWrap">AttrWrap</a> widget is created towrap the filler widget with attribute "bg".<li>A canvas is created by calling the <a href="reference.html#AttrWrap-render">render</a> function on the topmostwidget. </ul>AttrWrap widgets will behave like flow widgets or box widgets depending onhow they are called. The filler widget treats the first AttrWrap widget asa flow widget when calling its render function, so the AttrWrap widget calls the text widget's render function the same way. The second AttrWrap isused as the topmost widget and treated as a box widget, so it calls the filler render function in the same way.<div align="center"><pre><span style="color:black;background:#0000c0"> </span><span style="color:black;background:#0000c0"> </span><span style="color:black;background:#0000c0"> </span><span style="color:black;background:#c00000"> </span><span style="color:black;background:silver"> Hello World </span><span style="color:black;background:#c00000"> </span><span style="color:black;background:#0000c0"> </span><span style="color:black;background:#0000c0"> </span><span style="color:black;background:#0000c0"> </span></pre></div><br clear="left"><br><h3><a name="resize">1.4. Live Resizing</a><span class="back">[<a href="#top">back to top</a>]</span></h3>This program displays the string "Hello World" in the center of the screen.It uses different attributes for the text, the space on either sideof the text and the space above and below the text. When the window isresized it will repaint the screen, and it will exit when Q is pressed.<pre class="code">import urwid.curses_displayimport urwidui = urwid.curses_display.Screen()ui.register_palette( [ ('banner', 'black', 'light gray', ('standout', 'underline')), ('streak', 'black', 'dark red', 'standout'), ('bg', 'black', 'dark blue'), ] )def run(): cols, rows = ui.get_cols_rows() txt = urwid.Text(('banner', " Hello World "), align="center") wrap1 = urwid.AttrWrap( txt, 'streak' ) fill = urwid.Filler( wrap1 ) wrap2 = urwid.AttrWrap( fill, 'bg' ) while True: canvas = wrap2.render( (cols, rows) ) ui.draw_screen( (cols, rows), canvas ) keys = ui.get_input() if "q" in keys or "Q" in keys: break if "window resize" in keys:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -