wxPython:此程序需要访问屏幕

wxPython: This program needs access to the screen

我正在尝试在以下最小应用程序中使用 Python GUI wx(可通过 pip install wxPython 安装):

import wx
app = wx.App()

运行 此代码段 returns 以下:

This program needs access to the screen. Please run with a Framework
build of python, and only when you are logged in on the main display
of your Mac.

有谁知道如何帮助 wx 获得 "access to the screen",或者 "Framework build of Python" 是什么?如果其他人可以就这些问题提供任何帮助,我将不胜感激!

这解决了问题,但不是最好的解决方案:

# install anaconda
install anaconda

# uninstall all versions of pythonWx
pip uninstall pythonWx -y
conda remove pythonwx

# install the python.app binary through conda
conda install python.app

# determine where the conda binary lives
which conda

# that previous command returns something like: 
# /Users/yaledhlab/anaconda3/bin/conda
# replace the /conda with /python.app
# and run the result in a terminal
/Users/yaledhlab/anaconda3/bin/python.app

# that should open a Python terminal (you know you're in the Python
# terminal if you see >>> as a prefix for your shell)
# import the python package manager and install wxPython to
# your python.app version of Python
import pip
pip.main(['install', 'wxPython'])

# exit the python interpreter
exit()

# run the program
/Users/yaledhlab/anaconda3/bin/python.app main.py

@duhaime

官方 wxPython 网站上有 OSX 的安装程序。

只需下载并安装即可。

你不需要为了安装和 运行 如此简单的应用程序而玩 anaconda。

wxPython on Mac 在虚拟环境中抛出此错误,如 wxPython 网站所解释:https://wiki.wxpython.org/wxPythonVirtualenvOnMac

如果您 运行 它不在虚拟环境中并且仍然收到此错误,请尝试 运行 您使用 wxpython 的脚本 "pythonw" 而不是 "python"。例如:

pythonw hello.py

^请参阅 Python 文档中下页的“4.1.2 运行 带有 GUI 的脚本”部分(在 MacOS 上)以查看此内容python 怪癖解释:https://docs.python.org/3/using/mac.html

我运行陷入同样的​​问题。为了在 mac 上使用 anaconda 的 python 可执行文件和 wxPython,您需要 运行 "pythonw"(而不是 "python")。这会调用与 wxPython 兼容的 python 可执行文件。但是为了让它在我的 mac 上工作,我必须通过 运行ning:

更新我的 anaconda 包
conda install anaconda #you might not need this if anaconda is up to date

其次是:

conda install wxPython

这会在“//anaconda3/bin”目录中安装 "pythonw" 可执行文件(您可能在其他地方安装了 anaconda)。然后我可以 运行 任何 imports/contains 的 "program_with_xwPython.py" 并使用 wx 使用 "pythonw" 如下:

pythonw program_with_xwPython.py # Note: 'python program_with_xwPython.py' gives the error still, you need to run 'pythonw'

然后您还可以启动一个 python REPL(即 >>> 提示),使用 wxPython 使用:

pythonw # instead of 'python'

这让您可以使用 python CLI 导入 wx 和 运行 wx 应用程序。它对我来说一直很有效。

我总是让它工作,包括更新到 Catalina 之后,我从来不需要 运行 一些愚蠢的 pythonw 而不是 python 并且我所有的 wx-enabled 脚本 运行宁正确。

有时我的环境出现问题,我认为是 pip-tools 在没有任何警告的情况下开始删除我的包。不确定发生了什么,但损坏已经造成,突然间我看到了臭名昭著的

This program needs access to the screen. Please run with a Framework build of python, and only when you are logged in on the main display of your Mac.

我使用 pyenv 管理我的环境,当前 python 环境是 3.4.7。让问题中的脚本为 tst.py,我 运行

$ python tst.py 
This program needs access to the screen. Please run with a
Framework build of python, and only when you are logged in
on the main display of your Mac.

我确保 wxpython 安装了 brew

$ brew info wxpython
wxpython: stable 4.1.1 (bottled)
Python bindings for wxWidgets
https://www.wxpython.org/
/usr/local/Cellar/wxpython/4.1.1 (1,227 files, 91.8MB) *
  Poured from bottle on 2021-03-04 at 14:52:18
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/wxpython.rb
License: LGPL-2.0-or-later with WxWindows-exception-3.1
==> Dependencies
Required: freetype ✔, jpeg ✔, libpng ✔, libtiff ✔, numpy ✔, python@3.9 ✔
==> Analytics
install: 643 (30 days), 2,701 (90 days), 12,659 (365 days)
install-on-request: 604 (30 days), 2,523 (90 days), 9,914 (365 days)
build-error: 0 (30 days)

wxPython wiki建议

The reason is because some 3rd party tools like PyInstaller might require CPython installation be built with --enable-framework. What you need to do is run $ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.x.x in your terminal.

所以我从头开始重新安装3.7.4

$ pyenv uninstall 3.7.4
$ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.7.4

并再次尝试该脚本

$ python tst.py 
2021-03-04 15:14:44.138 Python[18815:404240] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)

当然它并没有完全解决它,但至少烦人的错误消失了...

wxPython官网有解决办法:

如果您使用的是 conda:

>> conda install python.app # this will install latest pythonw

>> pythonw your_app.py