wxPython: This program needs access to the screen

旧巷老猫 提交于 2019-12-14 02:14:19

问题


I'm trying to use the Python GUI wx (installable via pip install wxPython) in the following minimal app:

import wx
app = wx.App()

Running this snippet returns the following:

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.

Does anyone know how to help wx gain "access to the screen", or what a "Framework build of Python" is? I'd be grateful for any help others can offer with these questions!


回答1:


This resolves the problem, but it can't be the prettiest solution:

# 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



回答2:


wxPython on Mac within a virtual environment throws this error, as explained by wxPython website here: https://wiki.wxpython.org/wxPythonVirtualenvOnMac

If you are not running it in a virtual environment and still receive this error, try running your script that uses wxpython with "pythonw" instead of "python". Ex:

pythonw hello.py

^See section "4.1.2 Running Scripts with a GUI" (on MacOS) from the following page in the Python Docs to see this python quirk explained: https://docs.python.org/3/using/mac.html




回答3:


@duhaime

There is an installer for OSX on the official wxPython web-site.

Just download it and install.

You don't need to play with anaconda just to install and run such a simple application.




回答4:


I ran into the same problem. In order to use anaconda's python executable and wxPython on a mac you will need to run "pythonw" (instead of "python"). This calls the python executable that is compatible with wxPython. But in order to get it to work on my mac I had to update my anaconda packages by running:

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

followed by:

conda install wxPython

which installs the "pythonw" executable in the "//anaconda3/bin" directory (you might have anaconda installed somewhere else). Then I could run any "program_with_xwPython.py" that imports/contains and uses wx using "pythonw" as follows:

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

You can then also launch a python REPL (i.e the >>> prompt) that works with wxPython using:

pythonw # instead of 'python'

This let's you import wx and run wx apps with the python CLI. It's been working like a champ for me.



来源:https://stackoverflow.com/questions/48531006/wxpython-this-program-needs-access-to-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!