Sign in to follow this  
Genjix

wxPython help

Recommended Posts

Genjix    100
#!/usr/bin/python

from wxPython.wx import *

ID_ABOUT = 101
ID_EXIT  = 102

class MyFrame(wxFrame):
	def __init__(self, parent, ID, title):
		wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(200, 150))
		self.CreateStatusBar()
		self.SetStatusText("This is the statusbar")

		menu = wxMenu()
		menu.Append(ID_ABOUT, "&About",
                    "More information about this program")
		menu.AppendSeparator()
		menu.Append(ID_EXIT, "E&xit", "Terminate the program")

		menuBar = wxMenuBar()
		menuBar.Append(menu , "&File");

		self.SetMenuBar(menuBar)

		EVT_MENU(self, ID_ABOUT, self.OnAbout)
		EVT_MENU(self, ID_EXIT,  self.TimeToQuit)

	def OnAbout(self, event):
		dlg = wxMessageDialog(self, "This sample program shows off\n"
						"frames, menus, statusbars, and this\n"
						"message dialog.",
						"About Me", wxOK | wxICON_INFORMATION)
		dlg.ShowModal()
		dlg.Destroy()

	def TimeToQuit(self, event):
		self.Close(true)

class MyApp(wxApp):
	def OnInit(self):
		frame = MyFrame(NULL , -1 , "Hello from wxPython")
		frame.Show(true)
		self.SetTopWindow(frame)
		return true

app = MyApp(0)
app.MainLoop()

produces
genjix@linux:~/media/programming/python> ./wx.py
Traceback (most recent call last):
  File "./wx.py", line 3, in ?
    from wxPython.wx import *
  File "/usr/lib/python2.3/site-packages/wxPython/__init__.py", line 10, in ?
    import _wx
  File "/usr/lib/python2.3/site-packages/wxPython/_wx.py", line 3, in ?
    from _core import *
  File "/usr/lib/python2.3/site-packages/wxPython/_core.py", line 15, in ?
    import wx._core
  File "/home/genjix/media/programming/python/wx.py", line 3, in ?
    from wxPython.wx import *
ImportError: No module named wx
genjix@linux:~/media/programming/python>
I didn't previously have wxPython-GTK rpm installed (SUSE here), so I installed it, but ignored my missing wxGTK-GL dependencies. I can successfully compile a wxWidgets app (C++). Thanks.

Share this post


Link to post
Share on other sites
Genjix    100
ok so I have re-installed wxPython from src, but make install doesn't seem to have put wxPython modules where they were last (any suggestions?), so I have put them in the same directory as my python files (wxPython directory).

how can I include them? I thought a simple from wxPython.wx import * would suffice.

Share this post


Link to post
Share on other sites
Genjix    100
Quote:
Original post by Kylotan
Try import wx, instead of from wxPython.wx import *.


nope. :(

wxPython/
...
wx/
wx.py


how can i include the wx files in the current directory?

where should they be on my system?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest 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.

Share this post


Link to post
Share on other sites
Genjix    100
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.

Share this post


Link to post
Share on other sites
Fruny    1658
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).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Genjix    100
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
smr    2468
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.

Share this post


Link to post
Share on other sites
Genjix    100
from wx.Python import *

genjix@linux:~/media/programming/python> ./k.py
Traceback (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._core
ImportError: Bad magic number in /home/genjix/media/programming/python/wx.pyc


import wx and import wx.wxPython

genjix@linux:~/media/programming/python> ./k.py
Traceback (most recent call last):
File "./k.py", line 3, in ?
import wx
ImportError: Bad magic number in /home/genjix/media/programming/python/wx.pyc
genjix@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/python

import wx.wxPython

ID_ABOUT = 101
ID_EXIT = 102

class 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).

Share this post


Link to post
Share on other sites
smr    2468
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, sys
from wx import Frame
import pyogre.ogre as ogre
import time

class 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()


Share this post


Link to post
Share on other sites
Genjix    100
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!)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this