[wxPython] [Q] Using more than one source

Hi, all.

I’m a newbie in Python and wxPython and I got problem using more than two source file.

Most examples uses one source file, but it is not realistic so that I tried to use two source files, then I got stuck.

I first created the main application source file as follows:

from wxPython.wx import *

class MainApp(wxApp):

def OnInit(self):
    from MainFrame import *
    mainFrame = MainFrame(NULL)
    mainFrame.Show( true )
    self.SetTopWindow( mainFrame )
    return true

app = ConceptualPhysicsApp( 0 )
app.MainLoop()

I created MainFrame.py at the same directory and I imported MainFrame as in the above code. I first tried “import MainFrame”, but it didn’t work at all.

Here is some part of MainFrame.py

from wxPython.wx import *

class MainFrame( wxMDIParentFrame ):

def __init__( self, parent ):       
    menu = self.makeMenu()
    self.SetMenuBar( menu )

    EVT_MENU( self, ID_OPEN, self.OnMenuOpen )
    EVT_MENU( self, ID_CLOSE, self.OnMenuClose )
    EVT_MENU( self, ID_SAVE, self.OnMenuSave )
    EVT_MENU( self, ID_SAVEAS, self.OnMenuSaveAs )
    EVT_MENU( self, ID_EXIT, self.OnMenuExit )
    EVT_MENU( self, ID_ABOUT, self.OnMenuAbout )
    EVT_CLOSE( self, self.OnCloseWindow )

def makeMenu( self ):
    fmenu = wxMenu()
    fmenu.Append( ID_OPEN, "&Open...", "Open saved file" )
    fmenu.Append( ID_CLOSE, "&Close...", "Close file" )
    fmenu.Append( ID_SAVE, "&Save...", "Save file" )
    fmenu.Append( ID_SAVEAS, "Save &As", "Save as file" )
    fmenu.AppendSeparator()
    fmenu.Append( ID_EXIT, "E&xit", "Quit the program" )

I tried to run MainApp by typing “python MainApp.py” in the directory I saved them and I got the following error message.

Traceback (most recent call last):

File “MainFrame.py”, line 22, in makeMenu

fmenu.Append( ID_OPEN, "&Open...", "Open saved file" )

NameError: global name ‘ID_OPEN’ is not defined.

I guess I should import something for ID_OPEN, but I don’t know what.

Could you help me solve this problem?

Thanks in advance.

YJ

[ Don't send HTML mail to this list. ]

I got the following error message.
Traceback (most recent call last):
....
File "MainFrame.py", line 22, in makeMenu
    fmenu.Append( ID_OPEN, "&Open...", "Open saved file" )
NameError: global name 'ID_OPEN' is not defined.

I guess I should import something for ID_OPEN, but I don't know what.
Could you help me solve this problem?

What makes you think that these identifiers should exist already in
something you can import? Maybe you are thinking of wxID_OPEN? There is
really no reason to use these standard IDs however (since there are only a
few of them and you'll usually have menu items that don't have a predefined
ID.) You can set your own IDs by assigning a value to them, or use the
return value of wxNewId() to get a unique value. Please read the demo to
get a better feel for how to use the library.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

Where do you see these identifiers (ID_OPEN, ID_CLOSE, etc) in the
documentation? As far as I know, they don't exist, so you would need to
define them yourself:

ID_OPEN = wxNewId()
ID_CLOSE = wxNewId()
...
etc

···

On Monday 05 November 2001 15:10, Young-Jin Lee wrote:

        EVT_MENU( self, ID_OPEN, self.OnMenuOpen )
        EVT_MENU( self, ID_CLOSE, self.OnMenuClose )
        EVT_MENU( self, ID_SAVE, self.OnMenuSave )
        EVT_MENU( self, ID_SAVEAS, self.OnMenuSaveAs )
        EVT_MENU( self, ID_EXIT, self.OnMenuExit )
        EVT_MENU( self, ID_ABOUT, self.OnMenuAbout )
        EVT_CLOSE( self, self.OnCloseWindow )

--
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308

Young-Jin Lee wrote:

Here is some part of MainFrame.py

from wxPython.wx import *

class MainFrame( wxMDIParentFrame ):
    def __init__( self, parent ):
        menu = self.makeMenu()
        self.SetMenuBar( menu )

        EVT_MENU( self, ID_OPEN, self.OnMenuOpen )
        EVT_MENU( self, ID_CLOSE, self.OnMenuClose )
        EVT_MENU( self, ID_SAVE, self.OnMenuSave )
        EVT_MENU( self, ID_SAVEAS, self.OnMenuSaveAs )
        EVT_MENU( self, ID_EXIT, self.OnMenuExit )
        EVT_MENU( self, ID_ABOUT, self.OnMenuAbout )
        EVT_CLOSE( self, self.OnCloseWindow )

    def makeMenu( self ):
        fmenu = wxMenu()
        fmenu.Append( ID_OPEN, "&Open...", "Open saved file" )
        fmenu.Append( ID_CLOSE, "&Close...", "Close file" )
        fmenu.Append( ID_SAVE, "&Save...", "Save file" )
        fmenu.Append( ID_SAVEAS, "Save &As", "Save as file" )
        fmenu.AppendSeparator()
        fmenu.Append( ID_EXIT, "E&xit", "Quit the program" )
....

I tried to run MainApp by typing "python MainApp.py" in the directory
I saved them and I got the following error message.
Traceback (most recent call last):
....
File "MainFrame.py", line 22, in makeMenu
    fmenu.Append( ID_OPEN, "&Open...", "Open saved file" )
NameError: global name 'ID_OPEN' is not defined.

I guess I should import something for ID_OPEN, but I don't know what.
Could you help me solve this problem?
Thanks in advance.

You're almost right. You are importing your file correctly, but you
havn't defined ID_OPEN. Somewhere in MainFrame.py,you need:

ID_OPEN = 100
ID_CLOSE = 110

etc.

Note: It's pretty clear that you are new to Python. wxPython is a pretty
complex place to start learning Python. MOst of us have found that it is
a lot easier to get a good grasp of Python for non-GUI applications
first, and then start learning wxPython. There are two reasons for this:
1) wxPython is a pretty comlicated package that take advamtage of many
of the nice features of Python
2) Virtually all of the wxPython docs are written with eth assumption
that you are familiar with Python, and OO programming in Python in
particular.

Good luck,
-Chris

···

--
Christopher Barker,
Ph.D.
ChrisHBarker@home.net --- --- ---
http://members.home.net/barkerlohmann ---@@ -----@@ -----@@
                                   ------@@@ ------@@@ ------@@@
Oil Spill Modeling ------ @ ------ @ ------ @
Water Resources Engineering ------- --------- --------
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------