469,600 Members | 2,209 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

why "import wx" doesn't work?

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()
Jul 19 '05 #1
9 4480
monkey wrote:
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
Jul 19 '05 #2
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?
Jul 19 '05 #3
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 ( :
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

Jul 19 '05 #4
> 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.
Jul 19 '05 #5
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_R ESIZE):
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

Jul 19 '05 #6
The Great 'monkey' uttered these words on 4/28/2005 5:50 PM:
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.


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
Jul 19 '05 #7

Użytkownik "monkey" <m@m.com> napisał w wiadomo¶ci
news:42**********@rain.i-cable.com...
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...


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
Jul 19 '05 #8
They both work, thanks for your instruction ( :
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_R ESIZE):
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

Jul 19 '05 #9
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.
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

Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Vio | last post: by
2 posts views Thread by wen | last post: by
5 posts views Thread by mark_galeck_spam_magnet | last post: by
YarrOfDoom
4 posts views Thread by YarrOfDoom | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.