wxPython help

Started by
18 comments, last by Genjix 18 years, 10 months ago
yes, as you can see from the traceback

Quote:
File "/home/genjix/media/programming/python/wx.py", line 3, in ?
from wxPython.wx import *
ImportError: No module named wx


As a general rule: Do NOT name your files the same as library files.
Advertisement
Quote:Original post by Anonymous Poster
yes, as you can see from the traceback

Quote:
File "/home/genjix/media/programming/python/wx.py", line 3, in ?
from wxPython.wx import *
ImportError: No module named wx


As a general rule: Do NOT name your files the same as library files.


yes, but I renamed it to amir.py and still got the same error message.
What version of wxPython do you have?

The newer versions work with import wx, not from wxPython.wx import *

The old tutorial is outdated (heck it even says so on the front page).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I'm having the same problem. I'm trying to install BT++:

#> python2.3 BT++.py
Traceback (most recent call last):
File "BT++.py", line 7, in ?
from wxPython.wx import *

I've tried replacing "from wxPython.wx import *" with "import wx" with no luck. I've searched my entire system for wx.py, as well as where Python is installed with no results. I've also looked through wxpython and python's 'make -n install' with no reference to a wx.py popping up.

Looking around for information on this, I've found that if you add "/usr/lib/python2.3/site-packages" to PYTHONPATH, it will work, but it doesn't.


Thanks
oh.. i have wxPython in /usr/lib/python2.4/site-packages (using an rpm from http://rpm.pbone.net), but it doesn't work still.
when I try and run the sample demo's i get a crosshair mouse cursor and if I try to click anywhere the computer speaker just beeps.
looking in /usr/lib/python2.4/site-packages/ i noticed wx/ was under wx-2.5.3-gtk2-unicode/, so I made a symbolic link from site-packages/, but it still doesn't work.
I may have discovered what my problem is. I installed Python from source, not knowing I already had it. Therefore, I recently removed all instances of sourcecode files. This would have also removed wxPython files. So now I'm installing wxPython again.
First of all, don't name your files the same name as modules you will be importing.

Second, it used to be wxPython.wx* but now it's wx.*.

import wx
class MyFrame(wx.Frame):

Let us know how that works out.

from wx.Python import *
genjix@linux:~/media/programming/python> ./k.pyTraceback (most recent call last):  File "./k.py", line 3, in ?    from wxPython.wx import *  File "/usr/lib/python2.4/site-packages/wx-2.5.3-gtk2-unicode/wxPython/__init__.py", line 10, in ?    import _wx  File "/usr/lib/python2.4/site-packages/wx-2.5.3-gtk2-unicode/wxPython/_wx.py", line 3, in ?    from _core import *  File "/usr/lib/python2.4/site-packages/wx-2.5.3-gtk2-unicode/wxPython/_core.py", line 15, in ?    import wx._coreImportError: Bad magic number in /home/genjix/media/programming/python/wx.pyc


import wx and import wx.wxPython
genjix@linux:~/media/programming/python> ./k.pyTraceback (most recent call last):  File "./k.py", line 3, in ?    import wxImportError: Bad magic number in /home/genjix/media/programming/python/wx.pycgenjix@linux:~/media/programming/python>


it should be noted that when I installed the rpm i invoked --nodeps (because i didn't have gtk_gl installed and didn't want it).

but still when I installed from source on another system on SUSE it still wouldn't work.

I can compile a wxWidgets app.

#!/usr/bin/pythonimport wx.wxPythonID_ABOUT = 101ID_EXIT  = 102class MyFrame(wxFrame):	def __init__():		var = 10


also when I run the examples (like About.py) I get a crosshair cursor and when I click anywhere the computer speaker beeps twice until I kill the app (control C).
I don't think that this is necessary:

import wx.wxPython

It can be replaced by this line:

import wx

If import wx doesn't work, then I'd say your installation is borked. I've got no experience installing from source. I always just install the prebuilt packages. It saves time.

Look at the following sample from ogre
## This code is in the Public Domain## This currently only works with wx 2.5!## If you are using wx 2.4, comment out the MainLoop function.import wx, sysfrom wx import Frameimport pyogre.ogre as ogreimport timeclass MyFrame(wx.Frame):    def __init__(            self, parent, ID, title, pos=wx.DefaultPosition,            size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE           ):        size = wx.Size(800,600)        wx.Frame.__init__(self, parent, ID, title, pos, size, style)        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)        self.Bind(wx.EVT_SIZE, self.OnSize)    def OnSize(self, event):        size = self.GetClientSize()        print 'resizing', size        if getattr(self, 'app', None):            self.app.update()        event.Skip()    def OnEraseBackground(self, event):        pass # Do nothing, to avoid flashing on MSW.    def OnCloseMe(self, event):        self.Close(True)    def OnCloseWindow(self, event):        self.Destroy()class App(wx.App):    """Application class."""    def OnInit(self):        self.frame = MyFrame(None, -1, 'PopupButton Test')        self.frame.Show()        self.SetTopWindow(self.frame)        return True    def initializeOgre(self):            root = ogre.Root()        self.root = root        # setup resources        config = ogre.ConfigFile()        config.loadFromFile('resources.cfg')        for sectionName, mm in config.getSectionIterator():            if mm:                for key, path in mm.items():                    ogre.ResourceGroupManager.getSingleton().addResourceLocation(path, key, sectionName)        #configure        carryOn = root.showConfigDialog()        if not carryOn:            sys.exit('Quit from Config Dialog')         root.initialise(False)        renderParameters = ogre.StringStringMap()        renderParameters['externalWindowHandle'] = str(self.frame.GetHandle())        renderWindow = root.createRenderWindow('TestOgre render window', 640,                                               480, False, renderParameters)        ogre.ResourceGroupManager.getSingleton().initialiseAllResourceGroups()        # chooseSceneManager        sceneManager = root.getSceneManager(ogre.ST_GENERIC)        # createCamera        camera = sceneManager.createCamera('PlayerCam')        camera.position = (0, 0, 500)        camera.lookAt(ogre.Vector3(0, 0, -300))        camera.nearClipDistance = 5        # createViewports        viewport = renderWindow.addViewport(camera, 0, 0.0, 0.0, 1.0, 1.0)        viewport.backgroundColour = 0, 0, 0        # set mipmaps ignored...        # create scene        sceneManager.ambientLight = 0.2, 0.2, 0.2        sceneManager.setSkyDome(True, 'Examples/CloudySky', 4.0, 8.0)        light = sceneManager.createLight('MainLight')        light.position = (20, 80, 50)        plane = ogre.Plane()        plane.normal = ogre.Vector3(0, 1, 0)        plane.d = 200        ogre.MeshManager.getSingleton().createPlane('FloorPlane', "General",                                                    plane, 200000, 200000,                                                    20, 20, True, 1, 50, 50,                                                    (0, 0, 1))        # create floor entity        entity = sceneManager.createEntity('floor', 'FloorPlane')        entity.setMaterialName('Examples/RustySteel')        sceneManager.rootSceneNode.createChildSceneNode().attachObject(entity)        # create head entity        headNode = sceneManager.rootSceneNode.createChildSceneNode()        entity = sceneManager.createEntity('head', 'ogrehead.mesh')        headNode.attachObject(entity)        # make sure the camera track this node        camera.setAutoTracking(True, headNode)        # create the camera node & attach camera        cameraNode = sceneManager.rootSceneNode.createChildSceneNode()        cameraNode.attachObject(camera)        # set up spline animation of node        animation = sceneManager.createAnimation('CameraTrack', 10)        animation.interpolationMode = ogre.Animation.IM_SPLINE        animationTrack = animation.createTrack(0, cameraNode)        key = animationTrack.createKeyFrame(0)        key = animationTrack.createKeyFrame(2.5)        key.translation = (500.0, 500.0, -1000.0)        key = animationTrack.createKeyFrame(5)        key.translation = (-1500.0, -1500.0, -600.0)        key = animationTrack.createKeyFrame(7.5)        key.translation = (0.0, -100.0, 0.0)        key = animationTrack.createKeyFrame(10.0)        key.translation = (0.0, 0.0, 0.0)        animationState = sceneManager.createAnimationState('CameraTrack')        animationState.enabled = True        # add some fog        sceneManager.setFog(ogre.FOG_EXP, ogre.ColourValue.White, 0.0002)        self.frame.app = self                renderWindow.active = True        self.update()    def update(self):        self.root.renderOneFrame()    def MainLoop(self):        self.keepGoing = True        evtloop = wx.EventLoop()        old = wx.EventLoop.GetActive()        wx.EventLoop.SetActive(evtloop)                while self.keepGoing:            self.update()            while evtloop.Pending():                evtloop.Dispatch()            time.sleep(0.10)            self.ProcessIdle()            while self.Pending():                self.Dispatch()            self.update()def main():    app = App()    app.initializeOgre()    app.MainLoop()if __name__ == '__main__':    main()
ok well thanks anyway. I'll probably wait for a new release and then ask author if it doesn't work. (after I ++ you its now 1234!)

This topic is closed to new replies.

Advertisement