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

来源:互联网 发布:php 重定向 url 编辑:程序博客网 时间:2024/06/05 20:58

最近对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 wxversionwxversion.select('2.8')import wx

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

找到 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 moduleallows your application to choose which version of wxPython will beimported when it does 'import wx'.  The main function of this moduleis `select` and you use it like this::    import wxversion    wxversion.select('2.4')    import wxOr additional build options can also be selected, although they willnot be required if they are not installed, like this::    import wxversion    wxversion.select('2.5.3-unicode')    import wxOr you can require an exact match on the build options like this::    import wxversion    wxversion.select('2.5.3-unicode', optionsRequired=True)    import wxFinally you can also specify a collection of versions that are allowedby your application, like this::    import wxversion    wxversion.select(['2.5.4', '2.5.5', '2.6'])    import wxOf course the default wxPython version can also be controlled bysetting PYTHONPATH or by editing the wx.pth path configuration file,but using wxversion will allow an application to manage the versionselection itself rather than depend on the user to setup theenvironment correctly.It works by searching the sys.path for directories matching wx-* andthen comparing them to what was passed to the select function.  If amatch is found then that path is inserted into sys.path.NOTE: If you are making a 'bundle' of your application with a toollike py2exe then you should *not* use the wxversion module since itlooks at the filesystem for the directories on sys.path, it will failin a bundled environment.  Instead you should simply ensure that theversion of wxPython that you want is found by default on the sys.pathwhen making the bundled version by setting PYTHONPATH.  Then thatversion will be included in your bundle and your app will work asexpected.  Py2exe and the others usually have a way to tell at runtimeif they are running from a bundle or running raw, so you can checkthat and only use wxversion if needed.  For example, for py2exe::    if not hasattr(sys, 'frozen'):        import wxversion        wxversion.select('2.5')    import wxMore documentation on wxversion and multi-version installs can befound 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)


原创粉丝点击