- #-----------------------------------------------------------------------------
-
# Name: ObjectRedirect.py
-
# Purpose: Demonstrate class methode redirection with new-style classes
-
#
-
# Author: Barton
-
#
-
# Created: 2007/09/20
-
# RCS-ID: $Id: ObjectRedirect.py $
-
# Copyright: (c) 2007
-
# Licence: free, free, free
-
#-----------------------------------------------------------------------------
-
-
-
"""In GUI programming, it is almost always the case that a simple widget needs
-
a parent window to be displayed in. No problem... Create the Frame() with a
-
sizer (usually) and add (say) a multi-line text widget. And there you have your
-
text display. Now, in the main application, create an instance of your new Frame
-
and call myFrame.Show() to get it on the screen. But, what about writing text to
-
that widget that is owned by the Frame??? You could:
-
self.myFrame.textObj.WriteText("the text to write") # but that many dots can be worrisome
-
or
-
textObj = self.myFrame.GetTextObj() # but there's that extra line
-
textObj.WriteText("the text to write") # and you need to define GetTextObj()
-
-
I've been toying with a trick that seems to work quite well on wxPython widgets that all
-
decend from a python object (new-style class objects). In the following snippet, imagine that these
-
objects are all several layers deep in the object hierarchy and decent from the same roots"""
-
-
-
class SimulatedTextWidget(object):
-
"""bla-bla_bla"""
-
def __init__(self, parent, *args, **kwargs):
-
self.parent = parent
-
-
def WriteText(self, text):
-
print text
-
-
-
class SimulatedFrame(object):
-
"""bla-bla_bla"""
-
def __init__(self, parent, *args, **kwargs):
-
self.parent = parent
-
self.simTextObj = SimulatedTextWidget(self)
-
-
def __getattribute__(self, name):
-
"""Redirect method calls: Try this object first. If it's not a method/attribute
-
of this object, try the redirected object."""
-
try:
-
return object.__getattribute__(self, name)
-
except AttributeError:
-
return object.__getattribute__(self.simTextObj, name)
-
-
-
-
if __name__ == "__main__":
-
frame = SimulatedFrame(None)
-
frame.WriteText("Hello world!")
0 1014 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Joseph Turian |
last post by:
Okay.
So I have a class A, which contains a vector of B.
Any B item is owned by a single, unique A. Indeed, when we create A, A
creates its B items.
B would like to know which A it is...
|
by: Derek Basch |
last post by:
This one has always bugged me. Is it better to just slap a "self" in
front of any variable that will be used by more than one class method
or should I pass around variable between the methods?
...
|
by: Earl |
last post by:
I need to call a method on an owned child form, and am wondering if the best
way of doing this is to capture the Closing event of the form that passes
control back to the form where I have the...
|
by: Rafe |
last post by:
Hi,
I'm working within an application (making a lot of wrappers), but the
application is not case sensitive. For example, Typing obj.name,
obj.Name, or even object.naMe is all fine (as far as...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |