wxPython:该程序需要访问屏幕

时间:2018-01-30 21:42:25

标签: python wxpython wxwidgets

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

import wx
app = wx.App()

运行此代码段会返回以下内容:

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获得“访问屏幕”,或者什么是“Python的框架构建”?如果有其他人可以提供这些问题的帮助,我将不胜感激!

6 个答案:

答案 0 :(得分:2)

这解决了问题,但它不是最漂亮的解决方案:

# 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

答案 1 :(得分:1)

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

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

<块引用>

此程序需要访问屏幕。请使用框架运行 构建python,并且仅当您在主显示屏上登录时 您的 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 建议

<块引用>

原因是一些 3rd 方工具(如 PyInstaller)可能需要使用 --enable-framework 构建 CPython 安装。您需要做的是在终端中运行 $ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.x.x。

所以我从头开始重新安装了 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)

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

答案 2 :(得分:0)

@duhaime

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

只需下载并安装。

只需安装和运行这样一个简单的应用程序,您就不需要使用anaconda。

答案 3 :(得分:0)

在虚拟环境中Mac上的Mac上的wxPython会引发此错误,如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

答案 4 :(得分:0)

我遇到了同样的问题。为了在Mac上使用anaconda的python可执行文件和wxPython,您将需要运行“ pythonw”(而不是“ python”)。这将调用与wxPython兼容的python可执行文件。但是为了使其在Mac上正常工作,我必须通过运行以下命令来更新anaconda软件包:

conda install anaconda #you might not need this if anaconda is up to date

其后:

conda install wxPython

它将“ pythonw”可执行文件安装在“ // anaconda3 / bin”目录中(您可能在其他位置安装了anaconda)。然后,我可以运行任何导入/包含并使用“ pythonw”使用wx的“ program_with_xwPython.py”,如下所示:

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

然后,您还可以使用以下命令启动适用于wxPython的python REPL(即>>>提示符):

pythonw # instead of 'python'

这使您可以导入wx并使用python CLI运行wx应用。对我来说,它一直像冠军一样。

答案 5 :(得分:0)

wxPython 官网有解决办法:

如果您使用的是 conda:

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

>> pythonw your_app.py