curses.html 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="Curses Programming with Python" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/howto/curses.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Author, A.M. Kuchling, Eric S. Raymond,, Release, 2.04,. Abstract: This document describes how to use the curses extension module to control text-mode displays. What is curses?: The curses library ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Author, A.M. Kuchling, Eric S. Raymond,, Release, 2.04,. Abstract: This document describes how to use the curses extension module to control text-mode displays. What is curses?: The curses library ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Curses Programming with Python &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="Descriptor HowTo Guide" href="descriptor.html" />
  34. <link rel="prev" title="Porting Extension Modules to Python 3" href="cporting.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/howto/curses.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Curses Programming with Python</a><ul>
  86. <li><a class="reference internal" href="#what-is-curses">What is curses?</a><ul>
  87. <li><a class="reference internal" href="#the-python-curses-module">The Python curses module</a></li>
  88. </ul>
  89. </li>
  90. <li><a class="reference internal" href="#starting-and-ending-a-curses-application">Starting and ending a curses application</a></li>
  91. <li><a class="reference internal" href="#windows-and-pads">Windows and Pads</a></li>
  92. <li><a class="reference internal" href="#displaying-text">Displaying Text</a><ul>
  93. <li><a class="reference internal" href="#attributes-and-color">Attributes and Color</a></li>
  94. </ul>
  95. </li>
  96. <li><a class="reference internal" href="#user-input">User Input</a></li>
  97. <li><a class="reference internal" href="#for-more-information">For More Information</a></li>
  98. </ul>
  99. </li>
  100. </ul>
  101. </div>
  102. <div>
  103. <h4>Previous topic</h4>
  104. <p class="topless"><a href="cporting.html"
  105. title="previous chapter">Porting Extension Modules to Python 3</a></p>
  106. </div>
  107. <div>
  108. <h4>Next topic</h4>
  109. <p class="topless"><a href="descriptor.html"
  110. title="next chapter">Descriptor HowTo Guide</a></p>
  111. </div>
  112. <div role="note" aria-label="source link">
  113. <h3>This Page</h3>
  114. <ul class="this-page-menu">
  115. <li><a href="../bugs.html">Report a Bug</a></li>
  116. <li>
  117. <a href="https://github.com/python/cpython/blob/main/Doc/howto/curses.rst"
  118. rel="nofollow">Show Source
  119. </a>
  120. </li>
  121. </ul>
  122. </div>
  123. </nav>
  124. </div>
  125. </div>
  126. <div class="related" role="navigation" aria-label="related navigation">
  127. <h3>Navigation</h3>
  128. <ul>
  129. <li class="right" style="margin-right: 10px">
  130. <a href="../genindex.html" title="General Index"
  131. accesskey="I">index</a></li>
  132. <li class="right" >
  133. <a href="../py-modindex.html" title="Python Module Index"
  134. >modules</a> |</li>
  135. <li class="right" >
  136. <a href="descriptor.html" title="Descriptor HowTo Guide"
  137. accesskey="N">next</a> |</li>
  138. <li class="right" >
  139. <a href="cporting.html" title="Porting Extension Modules to Python 3"
  140. accesskey="P">previous</a> |</li>
  141. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  142. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  143. <li class="switchers">
  144. <div class="language_switcher_placeholder"></div>
  145. <div class="version_switcher_placeholder"></div>
  146. </li>
  147. <li>
  148. </li>
  149. <li id="cpython-language-and-version">
  150. <a href="../index.html">3.12.0 Documentation</a> &#187;
  151. </li>
  152. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python HOWTOs</a> &#187;</li>
  153. <li class="nav-item nav-item-this"><a href="">Curses Programming with Python</a></li>
  154. <li class="right">
  155. <div class="inline-search" role="search">
  156. <form class="inline-search" action="../search.html" method="get">
  157. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  158. <input type="submit" value="Go" />
  159. </form>
  160. </div>
  161. |
  162. </li>
  163. <li class="right">
  164. <label class="theme-selector-label">
  165. Theme
  166. <select class="theme-selector" oninput="activateTheme(this.value)">
  167. <option value="auto" selected>Auto</option>
  168. <option value="light">Light</option>
  169. <option value="dark">Dark</option>
  170. </select>
  171. </label> |</li>
  172. </ul>
  173. </div>
  174. <div class="document">
  175. <div class="documentwrapper">
  176. <div class="bodywrapper">
  177. <div class="body" role="main">
  178. <section id="curses-programming-with-python">
  179. <span id="curses-howto"></span><h1>Curses Programming with Python<a class="headerlink" href="#curses-programming-with-python" title="Permalink to this headline">¶</a></h1>
  180. <dl class="field-list simple">
  181. <dt class="field-odd">Author</dt>
  182. <dd class="field-odd"><p>A.M. Kuchling, Eric S. Raymond</p>
  183. </dd>
  184. <dt class="field-even">Release</dt>
  185. <dd class="field-even"><p>2.04</p>
  186. </dd>
  187. </dl>
  188. <div class="topic">
  189. <p class="topic-title">Abstract</p>
  190. <p>This document describes how to use the <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> extension
  191. module to control text-mode displays.</p>
  192. </div>
  193. <section id="what-is-curses">
  194. <h2>What is curses?<a class="headerlink" href="#what-is-curses" title="Permalink to this headline">¶</a></h2>
  195. <p>The curses library supplies a terminal-independent screen-painting and
  196. keyboard-handling facility for text-based terminals; such terminals
  197. include VT100s, the Linux console, and the simulated terminal provided
  198. by various programs. Display terminals support various control codes
  199. to perform common operations such as moving the cursor, scrolling the
  200. screen, and erasing areas. Different terminals use widely differing
  201. codes, and often have their own minor quirks.</p>
  202. <p>In a world of graphical displays, one might ask “why bother”? It’s
  203. true that character-cell display terminals are an obsolete technology,
  204. but there are niches in which being able to do fancy things with them
  205. are still valuable. One niche is on small-footprint or embedded
  206. Unixes that don’t run an X server. Another is tools such as OS
  207. installers and kernel configurators that may have to run before any
  208. graphical support is available.</p>
  209. <p>The curses library provides fairly basic functionality, providing the
  210. programmer with an abstraction of a display containing multiple
  211. non-overlapping windows of text. The contents of a window can be
  212. changed in various ways—adding text, erasing it, changing its
  213. appearance—and the curses library will figure out what control codes
  214. need to be sent to the terminal to produce the right output. curses
  215. doesn’t provide many user-interface concepts such as buttons, checkboxes,
  216. or dialogs; if you need such features, consider a user interface library such as
  217. <a class="reference external" href="https://pypi.org/project/urwid/">Urwid</a>.</p>
  218. <p>The curses library was originally written for BSD Unix; the later System V
  219. versions of Unix from AT&amp;T added many enhancements and new functions. BSD curses
  220. is no longer maintained, having been replaced by ncurses, which is an
  221. open-source implementation of the AT&amp;T interface. If you’re using an
  222. open-source Unix such as Linux or FreeBSD, your system almost certainly uses
  223. ncurses. Since most current commercial Unix versions are based on System V
  224. code, all the functions described here will probably be available. The older
  225. versions of curses carried by some proprietary Unixes may not support
  226. everything, though.</p>
  227. <p>The Windows version of Python doesn’t include the <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a>
  228. module. A ported version called <a class="reference external" href="https://pypi.org/project/UniCurses">UniCurses</a> is available.</p>
  229. <section id="the-python-curses-module">
  230. <h3>The Python curses module<a class="headerlink" href="#the-python-curses-module" title="Permalink to this headline">¶</a></h3>
  231. <p>The Python module is a fairly simple wrapper over the C functions provided by
  232. curses; if you’re already familiar with curses programming in C, it’s really
  233. easy to transfer that knowledge to Python. The biggest difference is that the
  234. Python interface makes things simpler by merging different C functions such as
  235. <code class="xref c c-func docutils literal notranslate"><span class="pre">addstr()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">mvaddstr()</span></code>, and <code class="xref c c-func docutils literal notranslate"><span class="pre">mvwaddstr()</span></code> into a single
  236. <a class="reference internal" href="../library/curses.html#curses.window.addstr" title="curses.window.addstr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addstr()</span></code></a> method. You’ll see this covered in more
  237. detail later.</p>
  238. <p>This HOWTO is an introduction to writing text-mode programs with curses
  239. and Python. It doesn’t attempt to be a complete guide to the curses API; for
  240. that, see the Python library guide’s section on ncurses, and the C manual pages
  241. for ncurses. It will, however, give you the basic ideas.</p>
  242. </section>
  243. </section>
  244. <section id="starting-and-ending-a-curses-application">
  245. <h2>Starting and ending a curses application<a class="headerlink" href="#starting-and-ending-a-curses-application" title="Permalink to this headline">¶</a></h2>
  246. <p>Before doing anything, curses must be initialized. This is done by
  247. calling the <a class="reference internal" href="../library/curses.html#curses.initscr" title="curses.initscr"><code class="xref py py-func docutils literal notranslate"><span class="pre">initscr()</span></code></a> function, which will determine the
  248. terminal type, send any required setup codes to the terminal, and
  249. create various internal data structures. If successful,
  250. <code class="xref py py-func docutils literal notranslate"><span class="pre">initscr()</span></code> returns a window object representing the entire
  251. screen; this is usually called <code class="docutils literal notranslate"><span class="pre">stdscr</span></code> after the name of the
  252. corresponding C variable.</p>
  253. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">curses</span>
  254. <span class="n">stdscr</span> <span class="o">=</span> <span class="n">curses</span><span class="o">.</span><span class="n">initscr</span><span class="p">()</span>
  255. </pre></div>
  256. </div>
  257. <p>Usually curses applications turn off automatic echoing of keys to the
  258. screen, in order to be able to read keys and only display them under
  259. certain circumstances. This requires calling the
  260. <a class="reference internal" href="../library/curses.html#curses.noecho" title="curses.noecho"><code class="xref py py-func docutils literal notranslate"><span class="pre">noecho()</span></code></a> function.</p>
  261. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">curses</span><span class="o">.</span><span class="n">noecho</span><span class="p">()</span>
  262. </pre></div>
  263. </div>
  264. <p>Applications will also commonly need to react to keys instantly,
  265. without requiring the Enter key to be pressed; this is called cbreak
  266. mode, as opposed to the usual buffered input mode.</p>
  267. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">curses</span><span class="o">.</span><span class="n">cbreak</span><span class="p">()</span>
  268. </pre></div>
  269. </div>
  270. <p>Terminals usually return special keys, such as the cursor keys or navigation
  271. keys such as Page Up and Home, as a multibyte escape sequence. While you could
  272. write your application to expect such sequences and process them accordingly,
  273. curses can do it for you, returning a special value such as
  274. <a class="reference internal" href="../library/curses.html#curses.KEY_LEFT" title="curses.KEY_LEFT"><code class="xref py py-const docutils literal notranslate"><span class="pre">curses.KEY_LEFT</span></code></a>. To get curses to do the job, you’ll have to enable
  275. keypad mode.</p>
  276. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">stdscr</span><span class="o">.</span><span class="n">keypad</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
  277. </pre></div>
  278. </div>
  279. <p>Terminating a curses application is much easier than starting one. You’ll need
  280. to call:</p>
  281. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">curses</span><span class="o">.</span><span class="n">nocbreak</span><span class="p">()</span>
  282. <span class="n">stdscr</span><span class="o">.</span><span class="n">keypad</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
  283. <span class="n">curses</span><span class="o">.</span><span class="n">echo</span><span class="p">()</span>
  284. </pre></div>
  285. </div>
  286. <p>to reverse the curses-friendly terminal settings. Then call the
  287. <a class="reference internal" href="../library/curses.html#curses.endwin" title="curses.endwin"><code class="xref py py-func docutils literal notranslate"><span class="pre">endwin()</span></code></a> function to restore the terminal to its original
  288. operating mode.</p>
  289. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">curses</span><span class="o">.</span><span class="n">endwin</span><span class="p">()</span>
  290. </pre></div>
  291. </div>
  292. <p>A common problem when debugging a curses application is to get your terminal
  293. messed up when the application dies without restoring the terminal to its
  294. previous state. In Python this commonly happens when your code is buggy and
  295. raises an uncaught exception. Keys are no longer echoed to the screen when
  296. you type them, for example, which makes using the shell difficult.</p>
  297. <p>In Python you can avoid these complications and make debugging much easier by
  298. importing the <a class="reference internal" href="../library/curses.html#curses.wrapper" title="curses.wrapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">curses.wrapper()</span></code></a> function and using it like this:</p>
  299. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">curses</span> <span class="kn">import</span> <span class="n">wrapper</span>
  300. <span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">stdscr</span><span class="p">):</span>
  301. <span class="c1"># Clear screen</span>
  302. <span class="n">stdscr</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
  303. <span class="c1"># This raises ZeroDivisionError when i == 10.</span>
  304. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">11</span><span class="p">):</span>
  305. <span class="n">v</span> <span class="o">=</span> <span class="n">i</span><span class="o">-</span><span class="mi">10</span>
  306. <span class="n">stdscr</span><span class="o">.</span><span class="n">addstr</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;10 divided by </span><span class="si">{}</span><span class="s1"> is </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="mi">10</span><span class="o">/</span><span class="n">v</span><span class="p">))</span>
  307. <span class="n">stdscr</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
  308. <span class="n">stdscr</span><span class="o">.</span><span class="n">getkey</span><span class="p">()</span>
  309. <span class="n">wrapper</span><span class="p">(</span><span class="n">main</span><span class="p">)</span>
  310. </pre></div>
  311. </div>
  312. <p>The <a class="reference internal" href="../library/curses.html#curses.wrapper" title="curses.wrapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">wrapper()</span></code></a> function takes a callable object and does the
  313. initializations described above, also initializing colors if color
  314. support is present. <code class="xref py py-func docutils literal notranslate"><span class="pre">wrapper()</span></code> then runs your provided callable.
  315. Once the callable returns, <code class="xref py py-func docutils literal notranslate"><span class="pre">wrapper()</span></code> will restore the original
  316. state of the terminal. The callable is called inside a
  317. <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a>…<a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> that catches exceptions, restores
  318. the state of the terminal, and then re-raises the exception. Therefore
  319. your terminal won’t be left in a funny state on exception and you’ll be
  320. able to read the exception’s message and traceback.</p>
  321. </section>
  322. <section id="windows-and-pads">
  323. <h2>Windows and Pads<a class="headerlink" href="#windows-and-pads" title="Permalink to this headline">¶</a></h2>
  324. <p>Windows are the basic abstraction in curses. A window object represents a
  325. rectangular area of the screen, and supports methods to display text,
  326. erase it, allow the user to input strings, and so forth.</p>
  327. <p>The <code class="docutils literal notranslate"><span class="pre">stdscr</span></code> object returned by the <a class="reference internal" href="../library/curses.html#curses.initscr" title="curses.initscr"><code class="xref py py-func docutils literal notranslate"><span class="pre">initscr()</span></code></a> function is a
  328. window object that covers the entire screen. Many programs may need
  329. only this single window, but you might wish to divide the screen into
  330. smaller windows, in order to redraw or clear them separately. The
  331. <a class="reference internal" href="../library/curses.html#curses.newwin" title="curses.newwin"><code class="xref py py-func docutils literal notranslate"><span class="pre">newwin()</span></code></a> function creates a new window of a given size,
  332. returning the new window object.</p>
  333. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">begin_x</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span> <span class="n">begin_y</span> <span class="o">=</span> <span class="mi">7</span>
  334. <span class="n">height</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span> <span class="n">width</span> <span class="o">=</span> <span class="mi">40</span>
  335. <span class="n">win</span> <span class="o">=</span> <span class="n">curses</span><span class="o">.</span><span class="n">newwin</span><span class="p">(</span><span class="n">height</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">begin_y</span><span class="p">,</span> <span class="n">begin_x</span><span class="p">)</span>
  336. </pre></div>
  337. </div>
  338. <p>Note that the coordinate system used in curses is unusual.
  339. Coordinates are always passed in the order <em>y,x</em>, and the top-left
  340. corner of a window is coordinate (0,0). This breaks the normal
  341. convention for handling coordinates where the <em>x</em> coordinate comes
  342. first. This is an unfortunate difference from most other computer
  343. applications, but it’s been part of curses since it was first written,
  344. and it’s too late to change things now.</p>
  345. <p>Your application can determine the size of the screen by using the
  346. <a class="reference internal" href="../library/curses.html#curses.LINES" title="curses.LINES"><code class="xref py py-data docutils literal notranslate"><span class="pre">curses.LINES</span></code></a> and <a class="reference internal" href="../library/curses.html#curses.COLS" title="curses.COLS"><code class="xref py py-data docutils literal notranslate"><span class="pre">curses.COLS</span></code></a> variables to obtain the <em>y</em> and
  347. <em>x</em> sizes. Legal coordinates will then extend from <code class="docutils literal notranslate"><span class="pre">(0,0)</span></code> to
  348. <code class="docutils literal notranslate"><span class="pre">(curses.LINES</span> <span class="pre">-</span> <span class="pre">1,</span> <span class="pre">curses.COLS</span> <span class="pre">-</span> <span class="pre">1)</span></code>.</p>
  349. <p>When you call a method to display or erase text, the effect doesn’t
  350. immediately show up on the display. Instead you must call the
  351. <a class="reference internal" href="../library/curses.html#curses.window.refresh" title="curses.window.refresh"><code class="xref py py-meth docutils literal notranslate"><span class="pre">refresh()</span></code></a> method of window objects to update the
  352. screen.</p>
  353. <p>This is because curses was originally written with slow 300-baud
  354. terminal connections in mind; with these terminals, minimizing the
  355. time required to redraw the screen was very important. Instead curses
  356. accumulates changes to the screen and displays them in the most
  357. efficient manner when you call <code class="xref py py-meth docutils literal notranslate"><span class="pre">refresh()</span></code>. For example, if your
  358. program displays some text in a window and then clears the window,
  359. there’s no need to send the original text because they’re never
  360. visible.</p>
  361. <p>In practice, explicitly telling curses to redraw a window doesn’t
  362. really complicate programming with curses much. Most programs go into a flurry
  363. of activity, and then pause waiting for a keypress or some other action on the
  364. part of the user. All you have to do is to be sure that the screen has been
  365. redrawn before pausing to wait for user input, by first calling
  366. <code class="xref py py-meth docutils literal notranslate"><span class="pre">stdscr.refresh()</span></code> or the <code class="xref py py-meth docutils literal notranslate"><span class="pre">refresh()</span></code> method of some other relevant
  367. window.</p>
  368. <p>A pad is a special case of a window; it can be larger than the actual display
  369. screen, and only a portion of the pad displayed at a time. Creating a pad
  370. requires the pad’s height and width, while refreshing a pad requires giving the
  371. coordinates of the on-screen area where a subsection of the pad will be
  372. displayed.</p>
  373. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">pad</span> <span class="o">=</span> <span class="n">curses</span><span class="o">.</span><span class="n">newpad</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
  374. <span class="c1"># These loops fill the pad with letters; addch() is</span>
  375. <span class="c1"># explained in the next section</span>
  376. <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">99</span><span class="p">):</span>
  377. <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">99</span><span class="p">):</span>
  378. <span class="n">pad</span><span class="o">.</span><span class="n">addch</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="n">x</span><span class="p">,</span> <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">*</span><span class="n">y</span><span class="p">)</span> <span class="o">%</span> <span class="mi">26</span><span class="p">)</span>
  379. <span class="c1"># Displays a section of the pad in the middle of the screen.</span>
  380. <span class="c1"># (0,0) : coordinate of upper-left corner of pad area to display.</span>
  381. <span class="c1"># (5,5) : coordinate of upper-left corner of window area to be filled</span>
  382. <span class="c1"># with pad content.</span>
  383. <span class="c1"># (20, 75) : coordinate of lower-right corner of window area to be</span>
  384. <span class="c1"># : filled with pad content.</span>
  385. <span class="n">pad</span><span class="o">.</span><span class="n">refresh</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span><span class="mi">75</span><span class="p">)</span>
  386. </pre></div>
  387. </div>
  388. <p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">refresh()</span></code> call displays a section of the pad in the rectangle
  389. extending from coordinate (5,5) to coordinate (20,75) on the screen; the upper
  390. left corner of the displayed section is coordinate (0,0) on the pad. Beyond
  391. that difference, pads are exactly like ordinary windows and support the same
  392. methods.</p>
  393. <p>If you have multiple windows and pads on screen there is a more
  394. efficient way to update the screen and prevent annoying screen flicker
  395. as each part of the screen gets updated. <code class="xref py py-meth docutils literal notranslate"><span class="pre">refresh()</span></code> actually
  396. does two things:</p>
  397. <ol class="arabic simple">
  398. <li><p>Calls the <a class="reference internal" href="../library/curses.html#curses.window.noutrefresh" title="curses.window.noutrefresh"><code class="xref py py-meth docutils literal notranslate"><span class="pre">noutrefresh()</span></code></a> method of each window
  399. to update an underlying data structure representing the desired
  400. state of the screen.</p></li>
  401. <li><p>Calls the function <a class="reference internal" href="../library/curses.html#curses.doupdate" title="curses.doupdate"><code class="xref py py-func docutils literal notranslate"><span class="pre">doupdate()</span></code></a> function to change the
  402. physical screen to match the desired state recorded in the data structure.</p></li>
  403. </ol>
  404. <p>Instead you can call <code class="xref py py-meth docutils literal notranslate"><span class="pre">noutrefresh()</span></code> on a number of windows to
  405. update the data structure, and then call <code class="xref py py-func docutils literal notranslate"><span class="pre">doupdate()</span></code> to update
  406. the screen.</p>
  407. </section>
  408. <section id="displaying-text">
  409. <h2>Displaying Text<a class="headerlink" href="#displaying-text" title="Permalink to this headline">¶</a></h2>
  410. <p>From a C programmer’s point of view, curses may sometimes look like a
  411. twisty maze of functions, all subtly different. For example,
  412. <code class="xref c c-func docutils literal notranslate"><span class="pre">addstr()</span></code> displays a string at the current cursor location in
  413. the <code class="docutils literal notranslate"><span class="pre">stdscr</span></code> window, while <code class="xref c c-func docutils literal notranslate"><span class="pre">mvaddstr()</span></code> moves to a given y,x
  414. coordinate first before displaying the string. <code class="xref c c-func docutils literal notranslate"><span class="pre">waddstr()</span></code> is just
  415. like <code class="xref c c-func docutils literal notranslate"><span class="pre">addstr()</span></code>, but allows specifying a window to use instead of
  416. using <code class="docutils literal notranslate"><span class="pre">stdscr</span></code> by default. <code class="xref c c-func docutils literal notranslate"><span class="pre">mvwaddstr()</span></code> allows specifying both
  417. a window and a coordinate.</p>
  418. <p>Fortunately the Python interface hides all these details. <code class="docutils literal notranslate"><span class="pre">stdscr</span></code>
  419. is a window object like any other, and methods such as
  420. <a class="reference internal" href="../library/curses.html#curses.window.addstr" title="curses.window.addstr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addstr()</span></code></a> accept multiple argument forms. Usually there
  421. are four different forms.</p>
  422. <table class="docutils align-default">
  423. <colgroup>
  424. <col style="width: 41%" />
  425. <col style="width: 59%" />
  426. </colgroup>
  427. <thead>
  428. <tr class="row-odd"><th class="head"><p>Form</p></th>
  429. <th class="head"><p>Description</p></th>
  430. </tr>
  431. </thead>
  432. <tbody>
  433. <tr class="row-even"><td><p><em>str</em> or <em>ch</em></p></td>
  434. <td><p>Display the string <em>str</em> or character <em>ch</em> at
  435. the current position</p></td>
  436. </tr>
  437. <tr class="row-odd"><td><p><em>str</em> or <em>ch</em>, <em>attr</em></p></td>
  438. <td><p>Display the string <em>str</em> or character <em>ch</em>,
  439. using attribute <em>attr</em> at the current
  440. position</p></td>
  441. </tr>
  442. <tr class="row-even"><td><p><em>y</em>, <em>x</em>, <em>str</em> or <em>ch</em></p></td>
  443. <td><p>Move to position <em>y,x</em> within the window, and
  444. display <em>str</em> or <em>ch</em></p></td>
  445. </tr>
  446. <tr class="row-odd"><td><p><em>y</em>, <em>x</em>, <em>str</em> or <em>ch</em>, <em>attr</em></p></td>
  447. <td><p>Move to position <em>y,x</em> within the window, and
  448. display <em>str</em> or <em>ch</em>, using attribute <em>attr</em></p></td>
  449. </tr>
  450. </tbody>
  451. </table>
  452. <p>Attributes allow displaying text in highlighted forms such as boldface,
  453. underline, reverse code, or in color. They’ll be explained in more detail in
  454. the next subsection.</p>
  455. <p>The <a class="reference internal" href="../library/curses.html#curses.window.addstr" title="curses.window.addstr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addstr()</span></code></a> method takes a Python string or
  456. bytestring as the value to be displayed. The contents of bytestrings
  457. are sent to the terminal as-is. Strings are encoded to bytes using
  458. the value of the window’s <a class="reference internal" href="../library/curses.html#curses.window.encoding" title="curses.window.encoding"><code class="xref py py-attr docutils literal notranslate"><span class="pre">encoding</span></code></a> attribute; this defaults to
  459. the default system encoding as returned by <a class="reference internal" href="../library/locale.html#locale.getencoding" title="locale.getencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getencoding()</span></code></a>.</p>
  460. <p>The <a class="reference internal" href="../library/curses.html#curses.window.addch" title="curses.window.addch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addch()</span></code></a> methods take a character, which can be
  461. either a string of length 1, a bytestring of length 1, or an integer.</p>
  462. <p>Constants are provided for extension characters; these constants are
  463. integers greater than 255. For example, <a class="reference internal" href="../library/curses.html#curses.ACS_PLMINUS" title="curses.ACS_PLMINUS"><code class="xref py py-const docutils literal notranslate"><span class="pre">ACS_PLMINUS</span></code></a> is a +/-
  464. symbol, and <a class="reference internal" href="../library/curses.html#curses.ACS_ULCORNER" title="curses.ACS_ULCORNER"><code class="xref py py-const docutils literal notranslate"><span class="pre">ACS_ULCORNER</span></code></a> is the upper left corner of a box
  465. (handy for drawing borders). You can also use the appropriate Unicode
  466. character.</p>
  467. <p>Windows remember where the cursor was left after the last operation, so if you
  468. leave out the <em>y,x</em> coordinates, the string or character will be displayed
  469. wherever the last operation left off. You can also move the cursor with the
  470. <code class="docutils literal notranslate"><span class="pre">move(y,x)</span></code> method. Because some terminals always display a flashing cursor,
  471. you may want to ensure that the cursor is positioned in some location where it
  472. won’t be distracting; it can be confusing to have the cursor blinking at some
  473. apparently random location.</p>
  474. <p>If your application doesn’t need a blinking cursor at all, you can
  475. call <code class="docutils literal notranslate"><span class="pre">curs_set(False)</span></code> to make it invisible. For compatibility
  476. with older curses versions, there’s a <code class="docutils literal notranslate"><span class="pre">leaveok(bool)</span></code> function
  477. that’s a synonym for <a class="reference internal" href="../library/curses.html#curses.curs_set" title="curses.curs_set"><code class="xref py py-func docutils literal notranslate"><span class="pre">curs_set()</span></code></a>. When <em>bool</em> is true, the
  478. curses library will attempt to suppress the flashing cursor, and you
  479. won’t need to worry about leaving it in odd locations.</p>
  480. <section id="attributes-and-color">
  481. <h3>Attributes and Color<a class="headerlink" href="#attributes-and-color" title="Permalink to this headline">¶</a></h3>
  482. <p>Characters can be displayed in different ways. Status lines in a text-based
  483. application are commonly shown in reverse video, or a text viewer may need to
  484. highlight certain words. curses supports this by allowing you to specify an
  485. attribute for each cell on the screen.</p>
  486. <p>An attribute is an integer, each bit representing a different
  487. attribute. You can try to display text with multiple attribute bits
  488. set, but curses doesn’t guarantee that all the possible combinations
  489. are available, or that they’re all visually distinct. That depends on
  490. the ability of the terminal being used, so it’s safest to stick to the
  491. most commonly available attributes, listed here.</p>
  492. <table class="docutils align-default">
  493. <colgroup>
  494. <col style="width: 37%" />
  495. <col style="width: 63%" />
  496. </colgroup>
  497. <thead>
  498. <tr class="row-odd"><th class="head"><p>Attribute</p></th>
  499. <th class="head"><p>Description</p></th>
  500. </tr>
  501. </thead>
  502. <tbody>
  503. <tr class="row-even"><td><p><a class="reference internal" href="../library/curses.html#curses.A_BLINK" title="curses.A_BLINK"><code class="xref py py-const docutils literal notranslate"><span class="pre">A_BLINK</span></code></a></p></td>
  504. <td><p>Blinking text</p></td>
  505. </tr>
  506. <tr class="row-odd"><td><p><a class="reference internal" href="../library/curses.html#curses.A_BOLD" title="curses.A_BOLD"><code class="xref py py-const docutils literal notranslate"><span class="pre">A_BOLD</span></code></a></p></td>
  507. <td><p>Extra bright or bold text</p></td>
  508. </tr>
  509. <tr class="row-even"><td><p><a class="reference internal" href="../library/curses.html#curses.A_DIM" title="curses.A_DIM"><code class="xref py py-const docutils literal notranslate"><span class="pre">A_DIM</span></code></a></p></td>
  510. <td><p>Half bright text</p></td>
  511. </tr>
  512. <tr class="row-odd"><td><p><a class="reference internal" href="../library/curses.html#curses.A_REVERSE" title="curses.A_REVERSE"><code class="xref py py-const docutils literal notranslate"><span class="pre">A_REVERSE</span></code></a></p></td>
  513. <td><p>Reverse-video text</p></td>
  514. </tr>
  515. <tr class="row-even"><td><p><a class="reference internal" href="../library/curses.html#curses.A_STANDOUT" title="curses.A_STANDOUT"><code class="xref py py-const docutils literal notranslate"><span class="pre">A_STANDOUT</span></code></a></p></td>
  516. <td><p>The best highlighting mode available</p></td>
  517. </tr>
  518. <tr class="row-odd"><td><p><a class="reference internal" href="../library/curses.html#curses.A_UNDERLINE" title="curses.A_UNDERLINE"><code class="xref py py-const docutils literal notranslate"><span class="pre">A_UNDERLINE</span></code></a></p></td>
  519. <td><p>Underlined text</p></td>
  520. </tr>
  521. </tbody>
  522. </table>
  523. <p>So, to display a reverse-video status line on the top line of the screen, you
  524. could code:</p>
  525. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">stdscr</span><span class="o">.</span><span class="n">addstr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;Current mode: Typing mode&quot;</span><span class="p">,</span>
  526. <span class="n">curses</span><span class="o">.</span><span class="n">A_REVERSE</span><span class="p">)</span>
  527. <span class="n">stdscr</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
  528. </pre></div>
  529. </div>
  530. <p>The curses library also supports color on those terminals that provide it. The
  531. most common such terminal is probably the Linux console, followed by color
  532. xterms.</p>
  533. <p>To use color, you must call the <a class="reference internal" href="../library/curses.html#curses.start_color" title="curses.start_color"><code class="xref py py-func docutils literal notranslate"><span class="pre">start_color()</span></code></a> function soon
  534. after calling <a class="reference internal" href="../library/curses.html#curses.initscr" title="curses.initscr"><code class="xref py py-func docutils literal notranslate"><span class="pre">initscr()</span></code></a>, to initialize the default color set
  535. (the <a class="reference internal" href="../library/curses.html#curses.wrapper" title="curses.wrapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">curses.wrapper()</span></code></a> function does this automatically). Once that’s
  536. done, the <a class="reference internal" href="../library/curses.html#curses.has_colors" title="curses.has_colors"><code class="xref py py-func docutils literal notranslate"><span class="pre">has_colors()</span></code></a> function returns TRUE if the terminal
  537. in use can
  538. actually display color. (Note: curses uses the American spelling ‘color’,
  539. instead of the Canadian/British spelling ‘colour’. If you’re used to the
  540. British spelling, you’ll have to resign yourself to misspelling it for the sake
  541. of these functions.)</p>
  542. <p>The curses library maintains a finite number of color pairs, containing a
  543. foreground (or text) color and a background color. You can get the attribute
  544. value corresponding to a color pair with the <a class="reference internal" href="../library/curses.html#curses.color_pair" title="curses.color_pair"><code class="xref py py-func docutils literal notranslate"><span class="pre">color_pair()</span></code></a>
  545. function; this can be bitwise-OR’ed with other attributes such as
  546. <a class="reference internal" href="../library/curses.html#curses.A_REVERSE" title="curses.A_REVERSE"><code class="xref py py-const docutils literal notranslate"><span class="pre">A_REVERSE</span></code></a>, but again, such combinations are not guaranteed to work
  547. on all terminals.</p>
  548. <p>An example, which displays a line of text using color pair 1:</p>
  549. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">stdscr</span><span class="o">.</span><span class="n">addstr</span><span class="p">(</span><span class="s2">&quot;Pretty text&quot;</span><span class="p">,</span> <span class="n">curses</span><span class="o">.</span><span class="n">color_pair</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
  550. <span class="n">stdscr</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
  551. </pre></div>
  552. </div>
  553. <p>As I said before, a color pair consists of a foreground and background color.
  554. The <code class="docutils literal notranslate"><span class="pre">init_pair(n,</span> <span class="pre">f,</span> <span class="pre">b)</span></code> function changes the definition of color pair <em>n</em>, to
  555. foreground color f and background color b. Color pair 0 is hard-wired to white
  556. on black, and cannot be changed.</p>
  557. <p>Colors are numbered, and <a class="reference internal" href="../library/curses.html#curses.start_color" title="curses.start_color"><code class="xref py py-func docutils literal notranslate"><span class="pre">start_color()</span></code></a> initializes 8 basic
  558. colors when it activates color mode. They are: 0:black, 1:red,
  559. 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a>
  560. module defines named constants for each of these colors:
  561. <a class="reference internal" href="../library/curses.html#curses.COLOR_BLACK" title="curses.COLOR_BLACK"><code class="xref py py-const docutils literal notranslate"><span class="pre">curses.COLOR_BLACK</span></code></a>, <a class="reference internal" href="../library/curses.html#curses.COLOR_RED" title="curses.COLOR_RED"><code class="xref py py-const docutils literal notranslate"><span class="pre">curses.COLOR_RED</span></code></a>, and so forth.</p>
  562. <p>Let’s put all this together. To change color 1 to red text on a white
  563. background, you would call:</p>
  564. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">curses</span><span class="o">.</span><span class="n">init_pair</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">curses</span><span class="o">.</span><span class="n">COLOR_RED</span><span class="p">,</span> <span class="n">curses</span><span class="o">.</span><span class="n">COLOR_WHITE</span><span class="p">)</span>
  565. </pre></div>
  566. </div>
  567. <p>When you change a color pair, any text already displayed using that color pair
  568. will change to the new colors. You can also display new text in this color
  569. with:</p>
  570. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">stdscr</span><span class="o">.</span><span class="n">addstr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;RED ALERT!&quot;</span><span class="p">,</span> <span class="n">curses</span><span class="o">.</span><span class="n">color_pair</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
  571. </pre></div>
  572. </div>
  573. <p>Very fancy terminals can change the definitions of the actual colors to a given
  574. RGB value. This lets you change color 1, which is usually red, to purple or
  575. blue or any other color you like. Unfortunately, the Linux console doesn’t
  576. support this, so I’m unable to try it out, and can’t provide any examples. You
  577. can check if your terminal can do this by calling
  578. <a class="reference internal" href="../library/curses.html#curses.can_change_color" title="curses.can_change_color"><code class="xref py py-func docutils literal notranslate"><span class="pre">can_change_color()</span></code></a>, which returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the capability is
  579. there. If you’re lucky enough to have such a talented terminal, consult your
  580. system’s man pages for more information.</p>
  581. </section>
  582. </section>
  583. <section id="user-input">
  584. <h2>User Input<a class="headerlink" href="#user-input" title="Permalink to this headline">¶</a></h2>
  585. <p>The C curses library offers only very simple input mechanisms. Python’s
  586. <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> module adds a basic text-input widget. (Other libraries
  587. such as <a class="reference external" href="https://pypi.org/project/urwid/">Urwid</a> have more extensive
  588. collections of widgets.)</p>
  589. <p>There are two methods for getting input from a window:</p>
  590. <ul class="simple">
  591. <li><p><a class="reference internal" href="../library/curses.html#curses.window.getch" title="curses.window.getch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getch()</span></code></a> refreshes the screen and then waits for
  592. the user to hit a key, displaying the key if <a class="reference internal" href="../library/curses.html#curses.echo" title="curses.echo"><code class="xref py py-func docutils literal notranslate"><span class="pre">echo()</span></code></a> has been
  593. called earlier. You can optionally specify a coordinate to which
  594. the cursor should be moved before pausing.</p></li>
  595. <li><p><a class="reference internal" href="../library/curses.html#curses.window.getkey" title="curses.window.getkey"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getkey()</span></code></a> does the same thing but converts the
  596. integer to a string. Individual characters are returned as
  597. 1-character strings, and special keys such as function keys return
  598. longer strings containing a key name such as <code class="docutils literal notranslate"><span class="pre">KEY_UP</span></code> or <code class="docutils literal notranslate"><span class="pre">^G</span></code>.</p></li>
  599. </ul>
  600. <p>It’s possible to not wait for the user using the
  601. <a class="reference internal" href="../library/curses.html#curses.window.nodelay" title="curses.window.nodelay"><code class="xref py py-meth docutils literal notranslate"><span class="pre">nodelay()</span></code></a> window method. After <code class="docutils literal notranslate"><span class="pre">nodelay(True)</span></code>,
  602. <code class="xref py py-meth docutils literal notranslate"><span class="pre">getch()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">getkey()</span></code> for the window become
  603. non-blocking. To signal that no input is ready, <code class="xref py py-meth docutils literal notranslate"><span class="pre">getch()</span></code> returns
  604. <code class="docutils literal notranslate"><span class="pre">curses.ERR</span></code> (a value of -1) and <code class="xref py py-meth docutils literal notranslate"><span class="pre">getkey()</span></code> raises an exception.
  605. There’s also a <a class="reference internal" href="../library/curses.html#curses.halfdelay" title="curses.halfdelay"><code class="xref py py-func docutils literal notranslate"><span class="pre">halfdelay()</span></code></a> function, which can be used to (in
  606. effect) set a timer on each <code class="xref py py-meth docutils literal notranslate"><span class="pre">getch()</span></code>; if no input becomes
  607. available within a specified delay (measured in tenths of a second),
  608. curses raises an exception.</p>
  609. <p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">getch()</span></code> method returns an integer; if it’s between 0 and 255, it
  610. represents the ASCII code of the key pressed. Values greater than 255 are
  611. special keys such as Page Up, Home, or the cursor keys. You can compare the
  612. value returned to constants such as <a class="reference internal" href="../library/curses.html#curses.KEY_PPAGE" title="curses.KEY_PPAGE"><code class="xref py py-const docutils literal notranslate"><span class="pre">curses.KEY_PPAGE</span></code></a>,
  613. <a class="reference internal" href="../library/curses.html#curses.KEY_HOME" title="curses.KEY_HOME"><code class="xref py py-const docutils literal notranslate"><span class="pre">curses.KEY_HOME</span></code></a>, or <a class="reference internal" href="../library/curses.html#curses.KEY_LEFT" title="curses.KEY_LEFT"><code class="xref py py-const docutils literal notranslate"><span class="pre">curses.KEY_LEFT</span></code></a>. The main loop of
  614. your program may look something like this:</p>
  615. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
  616. <span class="n">c</span> <span class="o">=</span> <span class="n">stdscr</span><span class="o">.</span><span class="n">getch</span><span class="p">()</span>
  617. <span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;p&#39;</span><span class="p">):</span>
  618. <span class="n">PrintDocument</span><span class="p">()</span>
  619. <span class="k">elif</span> <span class="n">c</span> <span class="o">==</span> <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;q&#39;</span><span class="p">):</span>
  620. <span class="k">break</span> <span class="c1"># Exit the while loop</span>
  621. <span class="k">elif</span> <span class="n">c</span> <span class="o">==</span> <span class="n">curses</span><span class="o">.</span><span class="n">KEY_HOME</span><span class="p">:</span>
  622. <span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span>
  623. </pre></div>
  624. </div>
  625. <p>The <a class="reference internal" href="../library/curses.ascii.html#module-curses.ascii" title="curses.ascii: Constants and set-membership functions for ASCII characters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses.ascii</span></code></a> module supplies ASCII class membership functions that
  626. take either integer or 1-character string arguments; these may be useful in
  627. writing more readable tests for such loops. It also supplies
  628. conversion functions that take either integer or 1-character-string arguments
  629. and return the same type. For example, <a class="reference internal" href="../library/curses.ascii.html#curses.ascii.ctrl" title="curses.ascii.ctrl"><code class="xref py py-func docutils literal notranslate"><span class="pre">curses.ascii.ctrl()</span></code></a> returns the
  630. control character corresponding to its argument.</p>
  631. <p>There’s also a method to retrieve an entire string,
  632. <a class="reference internal" href="../library/curses.html#curses.window.getstr" title="curses.window.getstr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getstr()</span></code></a>. It isn’t used very often, because its
  633. functionality is quite limited; the only editing keys available are
  634. the backspace key and the Enter key, which terminates the string. It
  635. can optionally be limited to a fixed number of characters.</p>
  636. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">curses</span><span class="o">.</span><span class="n">echo</span><span class="p">()</span> <span class="c1"># Enable echoing of characters</span>
  637. <span class="c1"># Get a 15-character string, with the cursor on the top line</span>
  638. <span class="n">s</span> <span class="o">=</span> <span class="n">stdscr</span><span class="o">.</span><span class="n">getstr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span>
  639. </pre></div>
  640. </div>
  641. <p>The <a class="reference internal" href="../library/curses.html#module-curses.textpad" title="curses.textpad: Emacs-like input editing in a curses window."><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses.textpad</span></code></a> module supplies a text box that supports an
  642. Emacs-like set of keybindings. Various methods of the
  643. <a class="reference internal" href="../library/curses.html#curses.textpad.Textbox" title="curses.textpad.Textbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Textbox</span></code></a> class support editing with input
  644. validation and gathering the edit results either with or without
  645. trailing spaces. Here’s an example:</p>
  646. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">curses</span>
  647. <span class="kn">from</span> <span class="nn">curses.textpad</span> <span class="kn">import</span> <span class="n">Textbox</span><span class="p">,</span> <span class="n">rectangle</span>
  648. <span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">stdscr</span><span class="p">):</span>
  649. <span class="n">stdscr</span><span class="o">.</span><span class="n">addstr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;Enter IM message: (hit Ctrl-G to send)&quot;</span><span class="p">)</span>
  650. <span class="n">editwin</span> <span class="o">=</span> <span class="n">curses</span><span class="o">.</span><span class="n">newwin</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
  651. <span class="n">rectangle</span><span class="p">(</span><span class="n">stdscr</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="o">+</span><span class="mi">5</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="o">+</span><span class="mi">30</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
  652. <span class="n">stdscr</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
  653. <span class="n">box</span> <span class="o">=</span> <span class="n">Textbox</span><span class="p">(</span><span class="n">editwin</span><span class="p">)</span>
  654. <span class="c1"># Let the user edit until Ctrl-G is struck.</span>
  655. <span class="n">box</span><span class="o">.</span><span class="n">edit</span><span class="p">()</span>
  656. <span class="c1"># Get resulting contents</span>
  657. <span class="n">message</span> <span class="o">=</span> <span class="n">box</span><span class="o">.</span><span class="n">gather</span><span class="p">()</span>
  658. </pre></div>
  659. </div>
  660. <p>See the library documentation on <a class="reference internal" href="../library/curses.html#module-curses.textpad" title="curses.textpad: Emacs-like input editing in a curses window."><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses.textpad</span></code></a> for more details.</p>
  661. </section>
  662. <section id="for-more-information">
  663. <h2>For More Information<a class="headerlink" href="#for-more-information" title="Permalink to this headline">¶</a></h2>
  664. <p>This HOWTO doesn’t cover some advanced topics, such as reading the
  665. contents of the screen or capturing mouse events from an xterm
  666. instance, but the Python library page for the <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> module is now
  667. reasonably complete. You should browse it next.</p>
  668. <p>If you’re in doubt about the detailed behavior of the curses
  669. functions, consult the manual pages for your curses implementation,
  670. whether it’s ncurses or a proprietary Unix vendor’s. The manual pages
  671. will document any quirks, and provide complete lists of all the
  672. functions, attributes, and <a class="reference internal" href="../library/curses.html#curses-acs-codes"><span class="std std-ref">ACS_*</span></a> characters available to
  673. you.</p>
  674. <p>Because the curses API is so large, some functions aren’t supported in
  675. the Python interface. Often this isn’t because they’re difficult to
  676. implement, but because no one has needed them yet. Also, Python
  677. doesn’t yet support the menu library associated with ncurses.
  678. Patches adding support for these would be welcome; see
  679. <a class="reference external" href="https://devguide.python.org/">the Python Developer’s Guide</a> to
  680. learn more about submitting patches to Python.</p>
  681. <ul class="simple">
  682. <li><p><a class="reference external" href="https://invisible-island.net/ncurses/ncurses-intro.html">Writing Programs with NCURSES</a>:
  683. a lengthy tutorial for C programmers.</p></li>
  684. <li><p><a class="reference external" href="https://linux.die.net/man/3/ncurses">The ncurses man page</a></p></li>
  685. <li><p><a class="reference external" href="https://invisible-island.net/ncurses/ncurses.faq.html">The ncurses FAQ</a></p></li>
  686. <li><p><a class="reference external" href="https://www.youtube.com/watch?v=eN1eZtjLEnU">“Use curses… don’t swear”</a>:
  687. video of a PyCon 2013 talk on controlling terminals using curses or Urwid.</p></li>
  688. <li><p><a class="reference external" href="https://pyvideo.org/video/1568/console-applications-with-urwid">“Console Applications with Urwid”</a>:
  689. video of a PyCon CA 2012 talk demonstrating some applications written using
  690. Urwid.</p></li>
  691. </ul>
  692. </section>
  693. </section>
  694. <div class="clearer"></div>
  695. </div>
  696. </div>
  697. </div>
  698. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  699. <div class="sphinxsidebarwrapper">
  700. <div>
  701. <h3><a href="../contents.html">Table of Contents</a></h3>
  702. <ul>
  703. <li><a class="reference internal" href="#">Curses Programming with Python</a><ul>
  704. <li><a class="reference internal" href="#what-is-curses">What is curses?</a><ul>
  705. <li><a class="reference internal" href="#the-python-curses-module">The Python curses module</a></li>
  706. </ul>
  707. </li>
  708. <li><a class="reference internal" href="#starting-and-ending-a-curses-application">Starting and ending a curses application</a></li>
  709. <li><a class="reference internal" href="#windows-and-pads">Windows and Pads</a></li>
  710. <li><a class="reference internal" href="#displaying-text">Displaying Text</a><ul>
  711. <li><a class="reference internal" href="#attributes-and-color">Attributes and Color</a></li>
  712. </ul>
  713. </li>
  714. <li><a class="reference internal" href="#user-input">User Input</a></li>
  715. <li><a class="reference internal" href="#for-more-information">For More Information</a></li>
  716. </ul>
  717. </li>
  718. </ul>
  719. </div>
  720. <div>
  721. <h4>Previous topic</h4>
  722. <p class="topless"><a href="cporting.html"
  723. title="previous chapter">Porting Extension Modules to Python 3</a></p>
  724. </div>
  725. <div>
  726. <h4>Next topic</h4>
  727. <p class="topless"><a href="descriptor.html"
  728. title="next chapter">Descriptor HowTo Guide</a></p>
  729. </div>
  730. <div role="note" aria-label="source link">
  731. <h3>This Page</h3>
  732. <ul class="this-page-menu">
  733. <li><a href="../bugs.html">Report a Bug</a></li>
  734. <li>
  735. <a href="https://github.com/python/cpython/blob/main/Doc/howto/curses.rst"
  736. rel="nofollow">Show Source
  737. </a>
  738. </li>
  739. </ul>
  740. </div>
  741. </div>
  742. </div>
  743. <div class="clearer"></div>
  744. </div>
  745. <div class="related" role="navigation" aria-label="related navigation">
  746. <h3>Navigation</h3>
  747. <ul>
  748. <li class="right" style="margin-right: 10px">
  749. <a href="../genindex.html" title="General Index"
  750. >index</a></li>
  751. <li class="right" >
  752. <a href="../py-modindex.html" title="Python Module Index"
  753. >modules</a> |</li>
  754. <li class="right" >
  755. <a href="descriptor.html" title="Descriptor HowTo Guide"
  756. >next</a> |</li>
  757. <li class="right" >
  758. <a href="cporting.html" title="Porting Extension Modules to Python 3"
  759. >previous</a> |</li>
  760. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  761. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  762. <li class="switchers">
  763. <div class="language_switcher_placeholder"></div>
  764. <div class="version_switcher_placeholder"></div>
  765. </li>
  766. <li>
  767. </li>
  768. <li id="cpython-language-and-version">
  769. <a href="../index.html">3.12.0 Documentation</a> &#187;
  770. </li>
  771. <li class="nav-item nav-item-1"><a href="index.html" >Python HOWTOs</a> &#187;</li>
  772. <li class="nav-item nav-item-this"><a href="">Curses Programming with Python</a></li>
  773. <li class="right">
  774. <div class="inline-search" role="search">
  775. <form class="inline-search" action="../search.html" method="get">
  776. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  777. <input type="submit" value="Go" />
  778. </form>
  779. </div>
  780. |
  781. </li>
  782. <li class="right">
  783. <label class="theme-selector-label">
  784. Theme
  785. <select class="theme-selector" oninput="activateTheme(this.value)">
  786. <option value="auto" selected>Auto</option>
  787. <option value="light">Light</option>
  788. <option value="dark">Dark</option>
  789. </select>
  790. </label> |</li>
  791. </ul>
  792. </div>
  793. <div class="footer">
  794. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  795. <br />
  796. This page is licensed under the Python Software Foundation License Version 2.
  797. <br />
  798. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  799. <br />
  800. See <a href="/license.html">History and License</a> for more information.<br />
  801. <br />
  802. The Python Software Foundation is a non-profit corporation.
  803. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  804. <br />
  805. <br />
  806. Last updated on Oct 02, 2023.
  807. <a href="/bugs.html">Found a bug</a>?
  808. <br />
  809. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  810. </div>
  811. </body>
  812. </html>