bartonc 6,596
Recognized Expert Expert - #-----------------------------------------------------------------------------
-
# 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 1041 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 contained in, without having to be
perpetually told it by being passed A when B methods are called.
How can B store its A (or some handle to A) if A stores B?
|
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?
Flamewar........NOW!
jk, I really do want other opinions.
Thanks,
Derek
|
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 method. The structure is like so:
frmMain (MDI, runs on app start) calls
frmB (MDI child), which in turn calls
frmC (MDI child), which in turn calls
frmD (MDI child).
frmMain and frmB remain open while frmC and frmD are shown.
|
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 the app is
concerned). The problem is, If someone makes a typo, they may get an
unexpected error due accidentally calling the original attribute
instead of the wrapped version. Does anyone have a simple solution for
this?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |