如何将 wxPython 用于 Python 3?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26251030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:15:44  来源:igfitidea点击:

How to use wxPython for Python 3?

pythonmacospython-3.xwxpython

提问by Nick

I installed wxPython 3.0.1.1, but I'm unable to import wxusing Python 3.4.1. I am getting the following error:

我安装了wxPython 3.0.1.1,但我无法import wx使用Python 3.4.1. 我收到以下错误:

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'wx'

Nevertheless, I can import wxif I use Python 2.7(the default installation in my OS X 10.9):

不过,import wx如果我使用Python 2.7(我的 中的默认安装OS X 10.9),我可以:

Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>>

How can I use wxPython for Python 3, and specifically for Python 3.4.1?

如何将 wxPython 用于 Python 3,尤其是 Python 3.4.1?

采纳答案by nepix32

You have two different pythons installed on your machine (3.4.1 and 2.7.5). Do not expect to be able to use one package installed in one python (wxPython 3.0.1.1 at python 2.7.5) automatically to be available in another python.

您的机器上安装了两个不同的 python(3.4.1 和 2.7.5)。不要期望能够使用安装在一个 python 中的一个包(python 2.7.5 中的 wxPython 3.0.1.1)自动在另一个 python 中可用。

Additionally wxPython (classic) does not work for Python 3. You need wxPython Phoenixto be able to do that.

此外,wxPython(经典)不适用于 Python 3。您需要wxPython Phoenix才能做到这一点。

EDIT: The recommended way (by @RobinDunn) to install wxPython(the Phoenix variety which will work on 2.7 and 3, now hosted on PyPI) nowadays is just doing:

编辑:现在推荐的安装方式(由@RobinDunn 提供)wxPython(适用于 2.7 和 3 的 Phoenix 版本,现在托管在 PyPI 上)正在执行:

pip install wxPython

If you have the developer version installed, just do the following beforehand:

如果您安装了开发者版本,只需事先执行以下操作:

pip uninstall wxPython_Phoenix

You can try to install one of the wxPython Phoenix snapshotsin your Python 3.4.1. However, mind that Phoenix is not 1000% compatible with classic and you may experience the one or another hiccup when reusing classic code (but transitioning its doableand worth it).

您可以尝试在 Python 3.4.1 中安装wxPython Phoenix 快照之一。但是,请注意,Phoenix 并非 1000% 与经典兼容,您在重用经典代码时可能会遇到一个或另一个问题(但转换其可行且值得)。

You can find a complete explanation/description in the following wxPython wiki at the following link:

您可以在以下 wxPython wiki 中的以下链接中找到完整的解释/描述:

Installing wxPython-Phoenix using pip

使用 pip 安装 wxPython-Phoenix

There are several important points:

有几个重要的点:

  • that pip/setuptoolis new enough (> 6.x.x/> 12.x.x)

  • that the builds are "inofficial", and thus piprefuses to install it: basically you need to add --prewhen installing with pip.

  • that you waive SSL verification --trusted-host wxpython.org(no longer needed in modern versions where https now works properly).

  • pip/setuptool够新了 (> 6.xx/> 12.xx)

  • 构建是“非官方的”,因此pip拒绝安装它:基本上你需要--pre在使用 pip 安装时添加。

  • 您放弃 SSL 验证--trusted-host wxpython.org(在 https 现在可以正常工作的现代版本中不再需要)。

Full command for Windows machines:

Windows 机器的完整命令:

C:\python27\scripts\pip.exe install --upgrade --pre -f https://wxpython.org/Phoenix/snapshot-builds/ wxPython_Phoenix

Note that this will install wxPython Phoenix for Python 2.7.

请注意,这将为 Python 2.7 安装 wxPython Phoenix。

回答by inirlan

Check your sys.path in an interpreter :

在解释器中检查您的 sys.path :

import sys
sys.path

If you don't have the proper link to the proper directory, it won't work.

如果您没有指向正确目录的正确链接,它将无法工作。

Also, check Lib/site-packages in your python directory, to make sure wx is properly installed on your python 3. (there should be a directory starting with "wx-3.0")

另外,检查 python 目录中的 Lib/site-packages,以确保 wx 正确安装在你的 python 3 上。(应该有一个以“wx-3.0”开头的目录)

回答by jotrocken

As it seems, wxPython is not yet completely ported for Python 3, even though the version number might suggest that. This is why the wx modules aren't added to the sys.pathof Python 3.

看起来,wxPython 还没有完全移植到 Python 3 中,尽管版本号可能表明了这一点。这就是为什么 wx 模块没有添加到sys.pathPython 3 的原因。

You can either settle for using wxPython from Python 2.7 or have a look at this SO entry: Still no wxPython for Python 3 (or 3.3) yet?This is what @nepix32 suggested.

您可以选择使用 Python 2.7 中的 wxPython,也可以查看此 SO 条目:仍然没有适用于 Python 3(或 3.3)的 wxPython 吗?这是@nepix32 的建议。

Alternatively, use another GUI library which works with Python 3. Here is a list.

或者,使用另一个适用于 Python 3 的 GUI 库。这是一个列表

回答by Werner

To use wxPython with your Python 3.4x you need to use wxPython Phoenix - as others have pointed out. To install it you can do:

要在 Python 3.4x 中使用 wxPython,您需要使用 wxPython Phoenix - 正如其他人指出的那样。要安装它,您可以执行以下操作:

pip install -U --pre -f http://wxpython.org/Phoenix/snapshot-builds/ wxPython_Phoenix 

Note the space after the last '/' and wxPython_Phoenix

注意最后一个 '/' 和 wxPython_Phoenix 后面的空格

回答by Seyi Shoboyejo

Perhaps the solutions given earlier worked before. But what worked for me today (June 1, 2017) was:

也许之前给出的解决方案以前有效。但今天(2017 年 6 月 1 日)对我有用的是:

pip install - U - - pre - f https://wxpython.org/Phoenix/snapshot-builds/ wxPython

Always check the Readme.txt for this one...

始终检查 Readme.txt 以了解此内容...