AttributeError: 'module' object has no attribute 'Frame' 解决方法

来源:互联网 发布:苹果6在线软件 编辑:程序博客网 时间:2024/06/14 20:57


最近对wxpython的GUI编程试用了下,试用过程中出现报错:

[python] view plaincopyprint?
  1. Traceback (most recent call last):  
  2.   File "E:\study\python\wxpython1\stdout_err.py", line 7in <module>  
  3.     class Frame(wx.Frame):  
  4. AttributeError: 'module' object has no attribute 'Frame'  
找不到模块,网上找了下有很多解决办法和回答的解释,最主要的情况如下:

1、自己根本就没有安装wxpython,或者自己安装不正确,出现问题;自己import下wx测试即可;

2、自己命名的文件与python自带的模块文件名重名,这种情况只需将自己的文件改名即可。

3、如果以上都没有问题,还是出现一些异常报错,那就说明你可能安装了不止一个版本的wxpython,或者某个版本不准确;可用如下办法解决:

在文件开头加入import要使用的版本号

[python] view plaincopyprint?
  1. import wxversion  
  2. wxversion.select('2.8')  
  3. import wx  

对于版本号的确认可以参考自己的安装文件或py文件;

找到 C:\Python27\Lib\site-packages 目录下的wxversion.py文件

说明如下:

[python] view plaincopyprint?
  1. #----------------------------------------------------------------------  
  2. # Name:        wxversion  
  3. # Purpose:     Allows a wxPython program to search for alternate   
  4. #              installations of the wxPython packages and modify sys.path  
  5. #              so they will be found when "import wx" is done.  
  6. #  
  7. # Author:      Robin Dunn  
  8. #  
  9. # Created:     24-Sept-2004  
  10. # RCS-ID:      $Id: wxversion.py 49375 2007-10-23 21:41:52Z RD $  
  11. # Copyright:   (c) 2004 by Total Control Software  
  12. # Licence:     wxWindows license  
  13. #----------------------------------------------------------------------  
  14.   
  15. """ 
  16. If you have more than one version of wxPython installed this module 
  17. allows your application to choose which version of wxPython will be 
  18. imported when it does 'import wx'.  The main function of this module 
  19. is `select` and you use it like this:: 
  20.  
  21.     import wxversion 
  22.     wxversion.select('2.4') 
  23.     import wx 
  24.  
  25. Or additional build options can also be selected, although they will 
  26. not be required if they are not installed, like this:: 
  27.  
  28.     import wxversion 
  29.     wxversion.select('2.5.3-unicode') 
  30.     import wx 
  31.  
  32. Or you can require an exact match on the build options like this:: 
  33.  
  34.     import wxversion 
  35.     wxversion.select('2.5.3-unicode', optionsRequired=True) 
  36.     import wx 
  37.  
  38. Finally you can also specify a collection of versions that are allowed 
  39. by your application, like this:: 
  40.  
  41.     import wxversion 
  42.     wxversion.select(['2.5.4', '2.5.5', '2.6']) 
  43.     import wx 
  44.  
  45.  
  46. Of course the default wxPython version can also be controlled by 
  47. setting PYTHONPATH or by editing the wx.pth path configuration file, 
  48. but using wxversion will allow an application to manage the version 
  49. selection itself rather than depend on the user to setup the 
  50. environment correctly. 
  51.  
  52. It works by searching the sys.path for directories matching wx-* and 
  53. then comparing them to what was passed to the select function.  If a 
  54. match is found then that path is inserted into sys.path. 
  55.  
  56. NOTE: If you are making a 'bundle' of your application with a tool 
  57. like py2exe then you should *not* use the wxversion module since it 
  58. looks at the filesystem for the directories on sys.path, it will fail 
  59. in a bundled environment.  Instead you should simply ensure that the 
  60. version of wxPython that you want is found by default on the sys.path 
  61. when making the bundled version by setting PYTHONPATH.  Then that 
  62. version will be included in your bundle and your app will work as 
  63. expected.  Py2exe and the others usually have a way to tell at runtime 
  64. if they are running from a bundle or running raw, so you can check 
  65. that and only use wxversion if needed.  For example, for py2exe:: 
  66.  
  67.     if not hasattr(sys, 'frozen'): 
  68.         import wxversion 
  69.         wxversion.select('2.5') 
  70.     import wx 
  71.  
  72. More documentation on wxversion and multi-version installs can be 
  73. found at: http://wiki.wxpython.org/index.cgi/MultiVersionInstalls 
  74.  
  75. """  
我就是用egg安装不准确,然后从工作正常的机器上拷贝了wx-2.8-msw-unicode 这个文件夹放到C:\Python27\Lib\site-packages 下,再加上导入之前指定版本,就工作正常了

希望对发现有同样问题的人有点帮助,这个问题之前就花了我很多时间一直没搞定,后来到公司的电脑比较了下才ok的。

[python] view plaincopyprint?
  1. >>> print wx.version()  
  2. 2.8.12.1 (msw-unicode)  

这是我转载别人的 。http://blog.csdn.net/julius_lee/article/details/8466560

我的wx版本是

打开__version__.py 文件

如下# This file was generated by setup.py...

VERSION_STRING  = '3.0.0.0'
MAJOR_VERSION   = 3
MINOR_VERSION   = 0
RELEASE_VERSION = 0
SUBREL_VERSION  = 0

VERSION = (MAJOR_VERSION, MINOR_VERSION, RELEASE_VERSION,
           SUBREL_VERSION, '')
我的版本是 3.0.修改上面的2.8就可以啦。

0 0
原创粉丝点击