最近對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)