最近对wxpython的GUI编程试用了下,试用过程中出现报错:
Traceback (most recent call last): File "E:\study\python\wxpython1\stdout_err.py", line 7, in <module> class Frame(wx.Frame): AttributeError: 'module' object has no attribute 'Frame'找不到模块,网上找了下有很多解决办法和回答的解释,最主要的情况如下:
1、自己根本就没有安装wxpython,或者自己安装不正确,出现问题;自己import下wx测试即可;
2、自己命名的文件与python自带的模块文件名重名,这种情况只需将自己的文件改名即可。
3、如果以上都没有问题,还是出现一些异常报错,那就说明你可能安装了不止一个版本的wxpython,或者某个版本不准确;可用如下办法解决:
在文件开头加入import要使用的版本号
import wxversion wxversion.select('2.8') import wx
找到 C:\Python27\Lib\site-packages 目录下的wxversion.py文件
说明如下:
#---------------------------------------------------------------------- # Name: wxversion # Purpose: Allows a wxPython program to search for alternate # installations of the wxPython packages and modify sys.path # so they will be found when "import wx" is done. # # Author: Robin Dunn # # Created: 24-Sept-2004 # RCS-ID: $Id: wxversion.py 49375 2007-10-23 21:41:52Z RD $ # Copyright: (c) 2004 by Total Control Software # Licence: wxWindows license #---------------------------------------------------------------------- """ If you have more than one version of wxPython installed this module allows your application to choose which version of wxPython will be imported when it does 'import wx'. The main function of this module is `select` and you use it like this:: import wxversion wxversion.select('2.4') import wx Or additional build options can also be selected, although they will not be required if they are not installed, like this:: import wxversion wxversion.select('2.5.3-unicode') import wx Or you can require an exact match on the build options like this:: import wxversion wxversion.select('2.5.3-unicode', optionsRequired=True) import wx Finally you can also specify a collection of versions that are allowed by your application, like this:: import wxversion wxversion.select(['2.5.4', '2.5.5', '2.6']) import wx Of course the default wxPython version can also be controlled by setting PYTHONPATH or by editing the wx.pth path configuration file, but using wxversion will allow an application to manage the version selection itself rather than depend on the user to setup the environment correctly. It works by searching the sys.path for directories matching wx-* and then comparing them to what was passed to the select function. If a match is found then that path is inserted into sys.path. NOTE: If you are making a 'bundle' of your application with a tool like py2exe then you should *not* use the wxversion module since it looks at the filesystem for the directories on sys.path, it will fail in a bundled environment. Instead you should simply ensure that the version of wxPython that you want is found by default on the sys.path when making the bundled version by setting PYTHONPATH. Then that version will be included in your bundle and your app will work as expected. Py2exe and the others usually have a way to tell at runtime if they are running from a bundle or running raw, so you can check that and only use wxversion if needed. For example, for py2exe:: if not hasattr(sys, 'frozen'): import wxversion wxversion.select('2.5') import wx More documentation on wxversion and multi-version installs can be found at: http://wiki.wxpython.org/index.cgi/MultiVersionInstalls """我就是用egg安装不准确,然后从工作正常的机器上拷贝了wx-2.8-msw-unicode 这个文件夹放到C:\Python27\Lib\site-packages 下,再加上导入之前指定版本,就工作正常了
希望对发现有同样问题的人有点帮助,这个问题之前就花了我很多时间一直没搞定,后来到公司的电脑比较了下才ok的。
>>> print wx.version() 2.8.12.1 (msw-unicode)