Python Library To Control Mac

Introduction

The curses and ncurses (new curses) libraries go back to1980's and 90's and provide an API to create textualuser interfaces (TUI). If you write a command-lineapplication, you should consider using curses to implementfunctionality you could not otherwise do with standardconsole output. The text editor nano is a goodexample of a ncurses application. We will look at howto use this library in Python.

The python-control package can be installed using pip, conda or the standard distutils/setuptools mechanisms. The package requires numpy and scipy, and the plotting routines require matplotlib. In addition, some routines require the slycot library in order to implement more advanced features (including some MIMO functionality). PyVISA is a Python package that enables you to control all kinds of measurement devices independently of the interface (e.g. GPIB, RS232, USB, Ethernet). As an example, reading self-identification from a Keithley Multimeter with GPIB number 12 is as easy as three lines of Python code. How to install Python correctly on Mac OSX. Once you install Package Control, restart ST2 and bring up the Command Palette (Command+Shift+P on OS X.

Read more about curses programming from one of thencurses authors, Thomas E. Dickey, who also worked on xterm and lynx among other things.https://invisible-island.net/.Another author of ncurses was Eric S. Raymond, who has a bunch of awesome writings at http://www.catb.org/~esr/.

The official Python curses tutorial is really good, make sure to check it outas wellat https://docs.python.org/3/howto/curses.html.The full API documentation is also available athttps://docs.python.org/3/library/curses.html.There are lots of useful functions in the full API that are not coveredhere. I strongly encourage you to browse the full documentation.This tutorial will serve as an introduction to common tasks.

If you want to check out a simple finished project that uses Python curses,check out the issh DevDungeon projectwhich creates a menu for choosing SSH connections.

Installation

The curses package comes with the Python standard library.In Linux and Mac, the curses dependencies shouldalready be installed so there is no extra steps needed.On Windows, you need to install one special Python package,windows-cursesavailable on PyPIto add support.

You can verify everything works by running a Python interpreter andattempting to import curses. If you do not get any errors, you are ingood shape.

Core concepts

There are a few important concepts to understand before digging in tothe advanced concepts. Some primary things you will need to understand are:

Python Library To Control Mac Free

  • The concept of 'windows'
  • How to initialize and shut down curses to get a main window
  • How to add text, clear, and refresh windows

These primary topics will be covered first before we look in to somemore common tasks like modifying terminal properties to turn off the cursor,listening for key presses, centering text, and more.

Create a screen

Python Library To Control Mac

Now that we are confident the curses import worked,we can try to initialize it.When you initialize curses,it creates a new window and clears the screen,displaying your new window.This example will show how to initialize curses andobtain a window to work with. We will call the main window screen.

After running this example,you might be surprised by the behavior.It will display a blank screen for one second,and then you will see all of your print statements at the end when youreturn to the terminal.The print statements continue going to standard output and will remain there,even though it is not visible.It's creating a special buffer that is being displayed in the terminal,independent of STDOUT.If print statements don't go to the screen,then how do you get text on to this fancy new screen we initialized?

Print text to screen

Now that we know how to initialize a blank screen and clean up at the end,let's try adding text to the screen.This example shows how to initialize the screen like before,but taking it further and adding a string to the screen.Note that you need to refresh the screen after making changes.

With this knowledge, you can draw text anywhere you want, all over the screen!You can do all kinds of stuff with just this knowledge alone.You may be wondering how you know your limits,like what is the maximum row and maximum column?If you want to fill up the screen or draw a border,what rows and columns should you use?We'll cover that in a later section.

Clear the screen

You could go through cell by cell and fill it with a black backgroundspace character to reset the terminal, but there is a convenientfunction to clear the screen, with the clear() function of a window.

Windows

Python Library To Control Mac

Curses provides two important concepts: windows and pads.So far we have been working with one window, the main screen.You can create multiple windows of different sizes and place them aroundthe screen. You can do all the same things we showed with the 'screen'window like addstr() and addch().

You can save the contents of a window to a file, fill the contentsof the window from a file, add borders, add background characters, create sub-windows,and more.

Check out the full API documentation at https://docs.python.org/3/library/curses.html#window-objects.

This example shows how to create a window, add some text,and then move the window to a different location. It demonstrateshow text is automatically wrapped when the window width is reached.

Pads

Pads are basically windows that can have content that is larger than its displayarea. Pads are essentially scrollable windows.

Read more about pads athttps://docs.python.org/3/howto/curses.html#windows-and-pads.

To create a pad, you do it very similarly, using curses.newpad() insteadof curses.newwin(). When calling refresh() on the pad, you have toprovide a few extra arguments though. To refresh the pad you have to tell it

See the documentation for refresh() at https://docs.python.org/3/library/curses.html#curses.window.refresh.

Other common tasks

Now let's look at some other common tasks.

Shell out

Note that when you call curses.endwin() it returns your original terminal,but if you call screen.refresh() again after that,you can get your screen back,and you'd have to call curses.endwin() again to bring back STDOUT once again.This can be useful if you want to temporarily hide your screen anddo something in the original terminal before jumping back in toyour screen.

This example shows you how to'shell out'.This allows you to hide your screen and enter a command promptto do some tasks and then when you exit the shell,go back to your custom screen.

Get screen size

When using curses it's usually very important to make sure you are workingwithin the boundaries of the current terminal.If your application does not adapt to the terminal size,you can at least check the size and ensure it meets your minimum required size.A safe default size to assume is generally 80x24.Let's see how we figure out exactly what size the user's terminal is with curses.

Center text

By knowing the width and height of the terminal,you can calculate the center of the screen and position text accordingly.This shows how to use the screen width and height to find the centerpoint of the screen and print text that is properly aligned.This example shows how to calculate the middle row and the properoffset to make the text look centered.

Turn blinking cursor on and off

The terminal is always keeping track of the current cursor position.After you write text to the screen with addstr() the cursor ends up inthe cell just after your text.You can hide the cursor so it does not blink and is not visible to the user.You can just as easily turn it back on.You can control the behavior using the curs_set() function asdemonstrated in this example.

Format text

If we were trying to make an awesome menu for our program,it could use some colors.Curses allows you to change the color and the style of the text.You can make text bold, highlighted, or underline as well as change thecolor of the foreground and background.Notice in this example how you can call addstr() without passing it anx,y coordinate pair, it outputs to the current cursor location.The n newline character will control the cursor as normal and move itto the beginning of the next line.

Wait for key press

In all of these examples so far though, we've been using curses.napms()function to hold the curses screen open for a few secondsbefore it closes itself.

How do we give the user some control?

Instead of sleeping to hold the screen open for a few seconds,let's wait until the user presses the 'q' key to quit.This example will show how to get a keypress from the user.

Clean up

Depending on what settings you modify in the terminal, you might wantto do some cleanup to restore the state of the terminal

For example, if you turned off the cursor, disabled echo, turned on thekeypad keys, or turned on cbreak mode, you will likely want to restorethe original state of the terminal. Here are a few examples to keep in mind:

Using wrapper()

In the last section we looked at manually resetting the terminal manually.There is a better way provided in the standard library with curses.wrapper().

Sometimes you might encounter an unexpected exception in your applicationthat can cause it to crash. This can potentially leave your terminalin a bad state that is unusable after the crash. To accomodate this,the curses package provides a function that can wrap your whole applicationthat way it can handle restoring things to a sane state.

To use the wrapper, create a function that takes one argument: the screen.Then, call wrapper() and pass it your function that will operate withthe screen. The wrapper() function takes care of initializing the cursesscreen that is normally done with curses.initscr() and also takes care ofcalling curses.endwin() when your function is over or an exception is caught.

This example creates a main() function that will be passed to wrapper().The main function will intentionally throw an exception to show howthe wrapper function will exit somewhat gracefully and restore the terminalto a decent state.

Related libraries

Here are some other libraries that might be useful when trying to createcurses-based applications.

Python

Related tutorials

References

Important

If you are using a Python from any current python.orgPython installer for macOS (3.9.0+, 3.8.0+, or 3.7.2+),no further action is needed to use IDLE or tkinter.A built-in version of Tcl/Tk 8.6 will be used.

If you are using macOS 10.6 or later, the Apple-suppliedTcl/Tk 8.5 has serious bugs that can cause application crashes.If you wish to use IDLE or Tkinter, do not use the Apple-suppliedPythons. Instead, install and use a newer version of Pythonfrom python.org or a third-party distributor that supplies orlinks with a newer version of Tcl/Tk.

Python's integrated development environment,IDLE, and thetkinter GUI toolkitit uses, depend on the Tk GUI toolkit which isnot part of Python itself. For best results, it is important that theproper release of Tcl/Tk is installed on your machine.For recent Python installers for macOS downloadable from this website,here is a summary of current recommendations followed by more detailedinformation.

PythonReleaseInstallerVariantmacOSReleaseRecommendedTcl/TkAlternateTcl/TkNotRecommended
3.9.0,3.8.6,3.7.9all10.9 to10.15built-in8.6.8

There are currently three major variants of Tk in common use on macOS:

Aqua Cocoa Tk
A newer native implementation availableas a universal 64-bit and 32-bit binary. This variant is the standard nativemacOS variant in Tk 8.6 and as of Tk 8.5.13. Aqua Cocoa support wasbackported to Tk 8.5 (prior to 8.5.13) and released by Apple starting with macOS 10.6and by ActiveState starting with their 8.5.9.1 release.
Aqua Carbon Tk
Because it is implemented with older macOS Carbon interfaces, it isonly available as a 32-bit binary (usually for Intel and PowerPCprocessors). Aqua Carbon Tk 8.4 is included with macOS releases 10.4through 10.14 and is also available from ActiveState. Aqua Carbon variantsof Tk 8.5 had been available as an ActiveState Community Download priorto ActiveTcl 8.5.9. As of 8.5.13, the Tk project no longer supportsCarbon builds of Tk 8.5. 32-bit-only Python installers downloadablefrom this website for older Python releases were linked with Aqua CarbonTk 8.4.
X11 Tk
The traditional platform-independent UNIX Tk implementation whichrequires an X11 server, such as the Apple X11.app available as anoptional component in older macOS releases or from third-partydistributors. 64-bit and32-bit binaries can be built. While the Python installers downloadablefrom this website do not support X11 Tk, other distributors ofPython for macOS may do so.

built-in 8.6.8

As of Python 3.7.0, 3.6.8, and 2.7.16, all current Python installers for macOSdownloadable from python.org supplytheir own private copies of Tcl/Tk 8.6.8. They do not look for or use anythird-party or system copies of Tcl/Tk. This is an Aqua Cocoa Tk.

ActiveTcl 8.5.18.0

Python Library To Control Macbook Pro

ActiveState provides binary distributions of Tcl/Tk which are upward compatiblewith and generally more up-to-date than those provided by Apple in macOSreleases. This version of Tcl/Tk includes fixes for some critical problemsthat you may encounter using tkinter or IDLE (see Apple 8.5.9 below).You can download an installer for this release fromthe ActiveState web site.Note that ActiveState Community Edition binaries are not open source andare covered by an ActiveState license. You should read the licensebefore downloading to verify that your usage complies with its terms of use.As of Python 3.7.0, 3.6.8, and 2.7.16, no current Python installers for macOSdownloadable from python.org make use of this or any other external versionof Tcl/Tk.

This is an Aqua Cocoa Tk.

Apple 8.5.9

This release is included in macOS 10.7 through at least macOS 10.14.As of this writing,there are at least two known issues with Tk 8.5.9 thatare present in Apple 8.5.9 Tk but fixed in more recent upstream 8.5 releases.The more serious problem is an immediate crash in Tkwhen entering a composition character, like Option-u on a US keyboard.(This problem is documented asTk bug 2907388.)There is also the more general problem of input manager support for compositecharacters(Tk bug 3205153)which has also been fixed in more recent Tcl/Tk 8.5 releases.You can avoid these problems by using a current python.org installeror by using a third-partydistribution of Python that does not use Apple 8.5.9 Tk.This is an Aqua Cocoa Tk.

Apple 8.5.7

This release is included in macOS 10.6. IDLE is known to hang or crashwhen used with the Apple 8.5.7 included in all versions of macOS 10.6.x.Because of this,we strongly recommend that you do not attempt to use Tkinter or IDLE withthe Apple-supplied Python 2.6.1 in 10.6. Instead, install a newer version ofPython that supports a newer version of Tk.This is an Aqua Cocoa Tk.

Note

While Tcl and Tk areseparate frameworks and libraries, they are closely related and arenormally installed or updated simultaneously. You should notattempt to mix-and-match Tcl and Tk versions. References toa specific version of Tk assume the corresponding version ofTcl is installed as well.

The Python for macOS installers downloaded from this website dynamicallylink at runtime to Tcl/Tk macOS frameworks. The Tcl/Tk major version isdetermined when the installer is created and cannot be overridden.All current python.org installers for Python 3.7.x, 3.6.x,and 2.7.x link to their own built-in Tcl/Tk 8.6 frameworks and do not useexternal Tcl/Tk frameworks so the rest of this section only applies tonon-current releases and, as such, no longer supported.

The Python 64-bit/32-bit macOS installers for Python 3.6.x andand 2.7.x dynamically link to Tcl/Tk 8.5 frameworks.The dynamically linking occurs when tkinter (Python 3)or Tkinter (Python 2) is first imported (specifically, the internal_tkinter C extension module). By default, the macOS dynamic linkerlooks first in /Library/Frameworks for Tcl and Tk frameworks withthe proper major version. This is the standard location for third-partyor built from source frameworks, including the ActiveTcl releases.If frameworks of the proper major version are not found there,the dynamic linker looks for the same version in/System/Library/Frameworks, the location for Apple-suppliedframeworks shipped with macOS. (Note, you should normally not modifyor delete files in /System/Library.)

Python Library To Control Mac Download

As is common on macOS, the installed Pythons and the Tcl and Tkframeworks are built to run on multiple CPU architectures (universalbinaries) and across multiple macOS levels (minimum deploymenttarget). For Python to be able to dynamically link with a particularTcl and Tk version, the available architectures in the Tcl/Tk frameworksmust include the architecture that Python is running in and theirminimum deployment target should be no greater than that of Python.

Python Library To Control Mac Screen

  • 2020-10-05 - updated for 3.9.0 and 3.8.6, remove 2.7
  • 2020-08-17 - updated for 3.7.9
  • 2020-07-20 - updated for 3.8.5
  • 2020-06-27 - updated for 3.7.8
  • 2020-05-14 - updated for 3.8.3
  • 2020-03-10 - updated for 3.8.2 and 3.7.7
  • 2019-12-19 - updated for 3.8.1, 3.7.6, and 2.7.17
  • 2019-10-15 - updated for 3.8.0, 3.7.5, and macOS 10.15
  • 2019-07-08 - updated for 3.7.4; 3.6.x is now security-fix-only
  • 2019-03-25 - updated for 3.7.3
  • 2019-03-03 - updated for 2.7.16
  • 2018-12-24 - updated for 3.7.2 and 3.6.8
  • 2018-10-20 - updated for 3.7.1, 3.6.7, and macOS 10.14
  • 2018-06-27 - updated for 3.7.0 and 3.6.6
  • 2018-05-30 - updated for 3.7.0b5
  • 2018-05-02 - updated for 3.7.0b4 and 2.7.15; removed 32-bit-only refs
  • 2018-03-29 - updated for 3.7.0b3 and 3.6.5
  • 2018-02-28 - updated for 3.7.0b2
  • 2018-01-31 - updated for 3.7.0b1 and 3.6.4
  • 2017-10-03 - updated for 3.6.3 and macOS 10.13
  • 2017-09-16 - updated for 2.7.14; removed 3.5.x
  • 2017-07-17 - updated for 3.6.2
  • 2017-03-21 - updated for 3.6.1 and (belatedly) 3.5.3
  • 2016-12-23 - updated for 3.6.0
  • 2016-12-17 - updated for 2.7.13
  • 2016-09-23 - updated for macOS 10.12
  • 2016-07-31 - updated for 3.5.2 and 2.7.12; removed 3.4.x
  • 2015-12-20 - updated for 3.4.4
  • 2015-12-06 - updated for 3.5.1, 2.7.11, and macOS 10.11
  • 2015-09-13 - updated for 3.5.0
  • 2015-05-23 - updated for 2.7.10 and ActiveTcl 8.5.18.0
  • 2015-02-23 - updated for 3.4.3
  • 2014-12-10 - updated for 2.7.9 and ActiveTcl 8.5.17.0
  • 2014-10-16 - updated for macOS 10.10
  • 2014-10-06 - updated for 3.4.2 and ActiveTcl 8.5.16.0
  • 2014-09-22 - updated for 3.4.2rc1
  • 2014-07-01 - updated for 2.7.8
  • 2014-06-01 - updated for 2.7.7; removed 2.7.6 and 3.3.5
  • 2014-05-18 - updated for 3.4.1 and 2.7.7rc1
  • 2014-03-16 - updated for 3.4.0 and 3.3.5
  • 2014-02-10 - updated for 3.3.4 and 3.4.0rc1
  • 2014-01-05 - updated for 3.4.0b2
  • 2013-11-24 - clarify that the ActiveState website still refers to 8.5.15.0
  • 2013-11-24 - removed built-in for 3.4.0b1, removed 3.3.2 and 2.7.5
  • 2013-11-10 - ActiveTcl 8.5.15.1; removed built-in for 3.3.3rc2 and 2.7.6.
  • 2013-10-27 - updated for 3.3.3rc1 and 2.7.6rc1 and their built-in 8.5.15.
  • 2013-10-24 - updated for macOS 10.9 and ActiveTcl 8.5.15, removed 3.2.5.
  • 2013-10-20 - updated for 3.4.0a4 and its built-in 8.5.15.
  • 2013-09-29 - updated for 3.4.0a3
  • 2013-09-09 - updated for 3.4.0a2 and its built-in 8.5.14.
  • 2013-08-03 - updated for 3.4.0a1 and ActiveTcl 8.4.20
  • 2013-05-18 - updated for ActiveTcl 8.5.14
  • 2013-05-15 - updated for 3.3.2, 2.7.5, and 3.2.5
  • 2013-04-06 - updated for 3.3.1, 2.7.4, and 3.2.4
  • 2012-12-26 - updated for ActiveTcl 8.5.13 and Issue 15853 patch installer
  • 2012-09-29 - updated for 3.3.0 final and reverted to ActiveTcl 8.5.11.1
  • 2012-08-02 - updated for ActiveTcl 8.5.12
  • 2012-07-28 - updated for macOS 10.8
  • 2012-04-11 - updated for 3.2.3 final and 2.7.3 final
  • 2012-03-18 - updated for 3.2.3rc2 and 2.7.3rc2
  • 2012-03-04 - updated for ActiveTcl 8.5.11.1, 3.2.3rc1, 2.7.3rc1, removed 3.1.4
  • 2011-11-12 - updated for ActiveTcl 8.5.11
  • 2011-09-04 - updated for 3.2.2 final
  • 2011-07-21 - updated for macOS 10.7 and ActiveTcl 8.5.10.1
  • 2011-07-09 - updated for 3.2.1 final and ActiveTcl 8.5.10
  • 2011-06-12 - updated for 2.7.2 final and 3.1.4 final
  • 2011-05-30 - updated for 3.2.1rc, 2.7.2rc, and 3.1.4rc
  • 2011-03-08 - add warnings and include details on how Python links with Tcl/Tk releases
  • 2011-02-20 - updated for 3.2 final
  • 2011-01-31 draft 1 - preliminary info for 3.2rc2
  • 2011-01-14 draft 0