#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    17
    Rep Power
    0

    Modules in wxPython


    I am not sure how to make modules in wxpython, I can do it Python, but for some reason I'm getting a lot of problems when I try to do it in wxPython.

    I made a very simple code using to files (test.py and test2.py).

    The is test.py:
    Code:
    from wxPython.wx import *
    from test2
    
    class MainWindow(wxFrame):
        def __init__(self,parent,id,title):
            wxFrame.__init__(self,parent,wxID_ANY, title, size = (200,100))
    
            EVT_LEFT_DOWN(self,test2.OnClick)
            
            self.Show(true)
            
    app = wxPySimpleApp()
    frame = MainWindow(None, -1, "Hello")
    app.MainLoop()
    and this is test2.py:
    Code:
    from wxPython.wx import *
    
    def OnClick(event):
        text1 = wxStaticText(self, -1, "HJJJJJJJ", (100,100))
        Show.text1(true)
    This is the error get when I open run the file in idle:
    Code:
    Traceback (most recent call last):
      File "C:\Documents and Settings\inuk\Desktop\Tweaks\Program\test.py", line 13, in ?
        frame = MainWindow(None, -1, "Hello")
      File "C:\Documents and Settings\inuk\Desktop\Tweaks\Program\test.py", line 8, in __init__
        EVT_LEFT_DOWN(self,test2.OnClick)
    AttributeError: 'module' object has no attribute 'OnClick'
    I'm a beginner, so I'm pretty sure this is simple to solve. So if anybody can help me that would be great.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Code:
    from wxPython.wx import *
    from test2
    from test2... what?
    This should be raising a SyntaxError before it gets any further...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    17
    Rep Power
    0
    I can't believe I did that, but I still have problems even though i fixed that.

    so test.py is now:
    Code:
    from wxPython.wx import *
    import test2
    
    class MainWindow(wxFrame):
        def __init__(self,parent,id,title):
            wxFrame.__init__(self,parent,wxID_ANY, title, size = (200,100))
    
            EVT_LEFT_DOWN(self,test2.OnClick)
            
            self.Show(true)
            
    app = wxPySimpleApp()
    frame = MainWindow(None, -1, "Hello")
    app.MainLoop()
    but i still get an error:
    Code:
    Traceback (most recent call last):
      File "C:\Documents and Settings\inuk\Desktop\Tweaks\Program\test2.py", line 4, in OnClick
        text1 = wxStaticText(self, -1, "HJJJJJJJ", (100,100))
    NameError: global name 'self' is not defined
    I've tried leaving the Parent section blank or with "none", but that doesn't work. I'm not sure exactly what to place instead of "self" or what I have to change to make "self" work, if i need to use the word "self" in the parent section.
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Your test2.OnClick event handler is not part of a class instance. self gets defined for you when an instance of a class is created but its scope is limited to the instance (if you have lots of class instances which self would be the right self in a global context. Methods of a class instance can use the self variable that was auto generated but normal functions wont know what it is

    grimey
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    17
    Rep Power
    0
    Thanks for you help, but I'm still kinda confused of what to do
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Generally you would put the OnClick handler back into the main window class since it is a normally considered a method of that class and not a seperate thing at all. There is nothing to stop the handler being a wrapper to the real code in another module but if as you have it the code you want executed is managing the window that gets messy.


    Code:
    def ....(self,...): #Some init method you call before the window is displayed
        self.text1 = wxStaticText(self, -1, "", (100,100))
        self.text1.Show(False)
    
    def OnClick(self, event):
        self.text1.SetLabel("Blah!!!")
        self.text1.Show(True) 
        
        ...
        anotherModule.actionCode(some paramter)
    Last edited by Grim Archon; November 26th, 2004 at 09:18 AM.

IMN logo majestic logo threadwatch logo seochat tools logo