Hi all,
I'm fairly new to Python and have been able to develop a fair amount of code including some GUI (using wxPython for the GUI but believe this is a regular Python problem). My issue has to do with resolving scope.
I have a class definition with a whole bunch of stuff (defs, etc.) in it:
And also defined the callback/function:
NameError: name 'self' is not defined
(Note: also changed self.pwr_btn to just pwr_btn and pwr_btn became the error)
I understand why I get the error (I have other objects like variables, labels, etc. that will also be accessed by the callback and expect the same errors). I believe I also understand how the code in my main class is able to see the callback...the callback, an imported module, is now global.
So, how do I solve the scope issue so that module-ized callbacks (and I'm creating a few more, as well) like this one can "see"/access objects back in class MainClassWindow? My guess is that I may need to pass some kind of object reference to the callback, but am unsure of the best way to do this. I also can't imagine "global"-ing a whole bunch of objects.
Thanks in advance for the help.....Cheers....
I'm fairly new to Python and have been able to develop a fair amount of code including some GUI (using wxPython for the GUI but believe this is a regular Python problem). My issue has to do with resolving scope.
I have a class definition with a whole bunch of stuff (defs, etc.) in it:
class MainClassWindow(wx.Frame): def __init__(self): ....lots of stuff snipped....In this class I created and bind-ed a togglebutton definition:
self.pwr_btn.Bind(wx.EVT_TOGGLEBUTTON, self.PWRToggle)
And also defined the callback/function:
def self.PWRToggle(event): if self.pwr_btn.GetValue() == True: ....not so much stuff snipped....The program runs fine with the pwr_btn functioning as expected. I decided to "module-ize" the PWRToggle callback (when I did that self.PWRToggle changed to PWRToggle both in the main class and callback module) and then import it (and plan to do the same with a bunch of other callbacks). As a module, the import part works fine, but now when I press pwr_btn I get the following error msg:
NameError: name 'self' is not defined
(Note: also changed self.pwr_btn to just pwr_btn and pwr_btn became the error)
I understand why I get the error (I have other objects like variables, labels, etc. that will also be accessed by the callback and expect the same errors). I believe I also understand how the code in my main class is able to see the callback...the callback, an imported module, is now global.
So, how do I solve the scope issue so that module-ized callbacks (and I'm creating a few more, as well) like this one can "see"/access objects back in class MainClassWindow? My guess is that I may need to pass some kind of object reference to the callback, but am unsure of the best way to do this. I also can't imagine "global"-ing a whole bunch of objects.
Thanks in advance for the help.....Cheers....