From: Kevin A. <al...@se...> - 2004-09-17 15:56:34
|
On Sep 17, 2004, at 8:10 AM, John wrote: > On Thu, 16 Sep 2004, Kevin Altis wrote: > >> On Sep 16, 2004, at 12:28 PM, John wrote: >> >>> Why don't we put this back on the list. Others may be interested. >>> >>> Something else popped up for me. While looking at converting my >>> dialogs I realized I need to pass a database 'handle' from my main >>> app >>> to >>> the dialogs which executes operations on the tables. With the dialogs >>> it >>> was easy, call the dialog with the handle when I instantiate the >>> class, and >>> assign the handle to self.handle in the __init__ method. If I were to >>> call >>> a 'sub' -app it does not seem to be as straight forward, give how >>> backgrounds are called. Although I could have a get like method say >>> getdbhandle, to accomplish what I was doing in the >>> 'constructor' __init__. I'll guess I'll dive into the background >>> code and find out. ;) >> >> You have a couple of options. One, simply refer to variables in the >> parent. For example, it is common when creating a CustomDialog to set >> the parent as so (example from resourceEditor dialogInfoDialog.py... >> >> class DialogInfoDialog(model.CustomDialog): >> def __init__(self, aBg, rsrc): >> model.CustomDialog.__init__(self, aBg) >> >> self.parent = aBg >> ... >> >> If you had an attribute in the main background that called the dialog >> called db then you could refer to it as self.parent.db. Actually, the >> line above is redundant because CustomDialog __init__ sets the parent >> as well. For Background, CustomDialog, and PageBackground you can >> always just use the wxPython GetParent() method to get the same info. >> There is a mixedCase getParent alias as well. >> >> If you're creating a child window then you can set variables in the >> child using the reference you keep when you create the child, so >> something like: >> >> self.subWindow = model.childWindow(...) >> self.subWindow.db = self.db >> >> In your child window, you could then use self.db. The on_initialize >> event handler won't run until after your current event handler >> terminates, so the on_initialize can actually make references to >> self.db. >> >> ka >> > I get the dialog in fact that's close to what I'm currently doing. My > question is how to pass parameters to a class derived from > model.background *in the constructor* once you have the instance it's > easy enough to set them, but is it possible to pass parameters to the > __init__ for model.background or oninitialize when you make the call > app = model.Application(Launcher) for example. > -john You can't pass additional args to on_initialize, nor can you pass additional args to a subclass of model.Background because you don't ever make a direct instantiation of your Background subclass, that is done for you by the childWindow function. So, if you want to add additional args, the simplest thing to do is to make your own childWindow function that calls model.childWindow and then tacks on your additional attributes before returning the instance of the class. This is similar to the suggestion I made to Bartek in an email on 2004-09-10 and to which I just sent a correction. def childWindow(parent, frameClass, filename=None, rsrc=None, additonalArg1, additionalArg2): background = model.childWindow(parent, frameClass, filename, rsrc) background.additionalArg1 = additionalArg1 background.additionalArg2 = additionalArg2 return background Or if you want to get really complicated you could completely replace the childWindow function so it sends additional args when instantiating frameClass, provide your own __init__ method in your subclass, making sure to call model.Background.__init__ with the proper args, but that seems a lot more complicated. ka |