why "import wx" doesn't work?

M

monkey

I just learn to make a blank windows frame with python and wxpython. I found
the statment "import wx" cannot work as the original "from wxPython.wx
import *". I see in the readme file of wxpython that if I install it as the
default one, I can use "import wx" instead of the long one. What is wrong?
The code pasted below:

import wx # the default is "from wxPython.wx import *", I change it and it
just can't work.

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

app = MyApp(0)
app.MainLoop()
 
P

Peter Hansen

monkey said:
I just learn to make a blank windows frame with python and wxpython. I found
the statment "import wx" cannot work as the original "from wxPython.wx
import *". I see in the readme file of wxpython that if I install it as the
default one, I can use "import wx" instead of the long one. What is wrong?
The code pasted below:

import wx # the default is "from wxPython.wx import *", I change it and it
just can't work.

class MyApp(wxApp):
....

Assuming you've installed a version of wxPython that is recent enough
that "import wx" works (it's really unclear from what you've written
above), then the problem you are facing is not using the namespace that
you've now imported. Do this instead:

class MyApp(wx.App):
def OnInit(self):
frame = wx.Frame(NULL, -1, "....


Note that "wx." before everything from wxPython...

-Peter
 
M

monkey

It is the current version of wxPython(2.6). But follow you instruction it
still can't work...
But if using the default "from wxPython.wx import *", it work, don't know
what is the problem. May be this is an old example that cannot work with
"import wx". Because I get another example and it is ok.

Anyway, I hope I can catch up with you guys here in python programming soon.
Thanks a lot ( :
 
K

Kartic

The Great 'monkey' uttered these words on 4/28/2005 2:09 PM:
I just learn to make a blank windows frame with python and wxpython. I found
the statment "import wx" cannot work as the original "from wxPython.wx
import *". I see in the readme file of wxpython that if I install it as the
default one, I can use "import wx" instead of the long one. What is wrong?
The code pasted below:

import wx # the default is "from wxPython.wx import *", I change it and it
just can't work.


Which version of wxPython are you running? What do you mean by "does not
work"...does the import fail or is your code giving errors?
 
M

monkey

Which version of wxPython are you running? What do you mean by "does not
work"...does the import fail or is your code giving errors?

It is the current new version 2.6. The error message said that the class
wxApp is not defined...
But when using the default "from wxPython.wx import *", it works.
 
K

Kartic

The Great 'monkey' uttered these words on 4/28/2005 5:30 PM:
It is the current version of wxPython(2.6). But follow you instruction it
still can't work...
But if using the default "from wxPython.wx import *", it work, don't know
what is the problem. May be this is an old example that cannot work with
"import wx". Because I get another example and it is ok.


I suspect you are mixing program code for the namespace version (import
wx) with the old method of importing (from wxPython.wx import *).

Here are two version of a very simple app... try both and see if you get
any errors. And if so, _please_ post the exact error you get.

--- BEGIN The "new" namespace version ----

import wx

class MainFrame(wx.Frame):
def __init__(self, parent, id=-1, title="Test Wx", size=(-1, -1),
pos=(-1,-1), style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE):
wx.Frame.__init__(self, parent, id, title, size, pos, style)
self.Show(True)

app = wx.PySimpleApp()
frame = MainFrame(None, -1, "Test Wx NameSpace Style")
app.MainLoop()

--- END The "new" namespace version ----

--- BEGIN The old style import ----

from wxPython.wx import *

class MainFrame(wxFrame):
def __init__(self, parent, id=-1, title="Test Wx", size=(-1, -1),
pos=(-1,-1), style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE):
wxFrame.__init__(self, parent, id, title, size, pos, style)
self.Show(True)

app = wxPySimpleApp()
frame = MainFrame(None, -1, "Test Wx Old Style")
app.MainLoop()

--- END The old style import ----

Hope that helped!

Thanks,
-Kartic
 
K

Kartic

The Great 'monkey' uttered these words on 4/28/2005 5:50 PM:
It is the current new version 2.6. The error message said that the class
wxApp is not defined...
But when using the default "from wxPython.wx import *", it works.



See my previous post for examples... you are mixing the new import style
and old style of using the classes. That will not work.

Thanks,
-K
 
F

Filip Dreger

U¿ytkownik "monkey said:
It is the current new version 2.6. The error message said that the
class
wxApp is not defined...

This is very good! wxApp is never defined if you use "import wx". You
must use "wx.wxApp" instead.

If you import a module using "import anything", then all the names
imported from the module must begin with "anything.". If you import wx
using "import wx", then ALL the wx commands, classes and variables
(all the names) MUST begin with 'wx.". Change them, and your program
will work.

"from wx import *" is a special shortcut, allowing you to use all the
names without "wx.". If you change "from something import *" to
"import something", your code will always break, this is normal.

regards,
Filip Dreger
 
M

monkey

Bright ( ; You show me a crystal clear explaination.
As a newbie in python and even oop, I find the python documentation is not
easy to figure out. That's great with you guys so nice here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top