【问题标题】:wxPython: This program needs access to the screenwxPython:这个程序需要访问屏幕
【发布时间】:2018-07-09 21:42:18
【问题描述】:

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

import wx
app = wx.App()

运行此 sn-p 将返回以下内容:

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 的框架构建”是什么?对于其他人在这些问题上可以提供的任何帮助,我将不胜感激!

【问题讨论】:

  • 我认为这篇博文可能有灵魂blurringexistence.net/…。不过我没有机会阅读整本书,所以我不完全确定。
  • 使用pythonw 运行我的脚本对我有用。
  • @lanery 你能告诉我更多关于如何在 pythonw 中运行脚本的细节吗?
  • @pill45 尝试运行which pythonw(假设您使用的是 unix)。如果你得到输出,这意味着 pythonw 二进制文件在你的路径上,所以你可以使用:pythonw myscript.py。如果你在你的机器上安装 Anaconda,那你应该可以访问 pythonw
  • 当我在安装 wx 的 virtualenv 中运行 pythonw 时,它显示没有名为 wx 的模块,我错过了什么?

标签: python wxpython wxwidgets


【解决方案1】:

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

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

pythonw hello.py

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

【讨论】:

    【解决方案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
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。为了在 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'
      

      这让您可以使用 python CLI 导入 wx 并运行 wx 应用程序。它对我来说就像一个冠军。

      【讨论】:

        【解决方案4】:

        我总是让它工作,包括在更新到 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)
        

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

        【讨论】:

          【解决方案5】:

          @duhaime

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

          只需下载并安装。

          您不必为了安装和运行这样一个简单的应用程序而玩 anaconda。

          【讨论】:

          • 你有链接吗?这是我在他们的下载页面上看到的:wxpython.org/pages/downloads 如果你在谈论sourceforge.net/projects/wxpython/files/wxPython/3.0.2.0,我尝试的安装程序根本不起作用——它们在你的机器上工作了吗?
          • @duhaime,是的,确实如此。我猜 Robin 重新做了网站设计并将 wxPython 推送到 PyPI 的地方。您在尝试从 SF 安装时遇到了什么问题?你向 wxPython ML 报告了吗?
          • 我没有记录,也记不起我遇到的错误,尽管我刚刚下载并构建了代码并且它运行良好(尽管我仍然不确定我将如何运行带有这些应用程序文件的 Python 脚本)。也就是说,我花了一个小时帮助一位教授让旧学生的代码运行起来;我不太可能再次使用这个库......
          • @duhaime,如果是用旧 wxPython 版本编写的旧代码,我们会努力将其更新到新版本。并且根据它过去编写的版本,这项工作可能很重要。但这与那里的任何其他库没有什么不同——在一切顺利编译和运行之前,您仍然需要修复一些东西。简化的唯一方法是随着新版本逐渐升级,您教授没有这样做。所以这个小时听起来是一个很好的时间框架。
          【解决方案6】:

          wxPython官网有解决办法:

          如果您使用的是 conda:

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

          >> pythonw your_app.py

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多