MAC Conda環境wxpython報錯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程序,提示:
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.

原因是在MAC下通過虛擬Python環境運行,就會有這個問題。

之后搜索,找到了一個舊的官方一個通過腳本解決的頁面,他的虛擬環境是VirtualEnv(我的是conda),照着他的的代碼寫了個腳本依舊不管用。

重新好好看了官方wiki的安裝文檔,也沒有這方面的信息。

機緣巧合,終於在wxpython的下載頁面找到了解決辦法,這里不得不吐槽下,為啥不更新在安裝幫助文檔中!!!

https://www.wxpython.org/pages/downloads/
在如上頁面中有一欄“Conda or Anaconda”,原文如下:
If you are using a Python environment managed by Conda or Anaconda then you can install wxPython using the conda tool just like you would install any other package. Check here for details on what builds are available. The fine folks at Conda-forge have done a good job at keeping it up to date with new releases.

One small gotcha to be aware of with the conda builds of wxPython is that on OSX/macOS you will need to use pythonw to execute your wxPython applications, not the typical python command. To use pythonw you will need to install the python.app pacakge from conda.

簡單說就是運行wxpython,得用pythonw,不能用python。而且得安裝python.app

切到我建的虛擬環境:
>conda activate myevnname

安裝python.app
>conda install -c conda-forge python.app

之后用pythonw運行
>pythonw main.py
運行成功,不再報錯


那么在pycharm中怎么設置呢
點擊‘PyCharm’菜單->Preferences菜單->Project:mypro一級菜單->Python Interpreter二級菜單->點擊右上齒輪->Add菜單

 

 選Conda Environment菜單->選Existing enviroment(前提是你已經新建了conda的虛擬環境並安裝了wxpython和python.app)->在Interpreter項后點...按鈕

 

 選擇你虛擬環境的pythonw->點OK->點OK->點OK

 

 這樣,在pycharm中也可以直接運行wxpython的代碼了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM