I'm all for mass production through subclassing, but mass defining
functions? That could get a bit crazy. If the functions all do
basically the same thing, why not have one function that accepts the
variable as a parameter.
Unless you are trying to associate, say, a sound with a panel (I can
only guess from your code snippets), then subclassing the panel; is
the solution. Just add the PlaySound method to the class, and each
panel has it.
It is entirely possible that I am not seeing your programming goal,
here, so your miles may vary.
90% of my coding issues are not knowing a solution exists. A real life example:
If a man doesn't know about the "shovel," and he must dig a hole, he might use his hands. Sure it works, his hands may
be bruised, cut and dirty, but the hole is complete after an extended amount of time.
Introduce a "shovel" and he's done faster, the hole is better,
his hands aside from calusses are intact, and his hands are
better suited for using a shovel next time.
I'm not sure how to introduce a variable to a function.
I have 6 buttons on a frame, and they are all meant to trigger
an event that brings up a file browse dialog for picking a wav
file. The two things that this dialog must do are see what the
previously chosen file and path was. And retain the value if
the user doesn't choose anything (cancel / close.) And pass on the selection if the user makes a new choice.
I have another 6 buttons on a frame that do the same thing
for a font, except this one only needs to retain one value the "native font info" for the previously chosen font, and of
course pass on the new "native font info" if a new font is
chosen.
Previously I had done this using a slew of global variables,
but instead opted to use a dictionary to store the values.
So if there is a way to use ONE function for a font dialog
and ONE function for a sound dialog, and pass variables into the function for it to be stapled to various objects than
cool... A code example would be great
Steve
···
----- Original Message ----- From: "Josh English" <joshua.r.english@gmail.com>
To: <wxpython-users@lists.wxwidgets.org>
Sent: Monday, 21 July, 2008 13:07
Subject: Re: [wxpython-users] Is there a DRY method for events?
I'm all for mass production through subclassing, but mass defining
functions? That could get a bit crazy. If the functions all do
basically the same thing, why not have one function that accepts the
variable as a parameter.
Unless you are trying to associate, say, a sound with a panel (I can
only guess from your code snippets), then subclassing the panel; is
the solution. Just add the PlaySound method to the class, and each
panel has it.
It is entirely possible that I am not seeing your programming goal,
here, so your miles may vary.
I'm not sure how to introduce a variable to a function.
Do you mean a callback function? If so, then yes, there are a few tricks to doing it, you need to pick what makes sense to you.
I've enclosed a sample app that does it four different ways. You can mix and match them a bit too, as there are two issues to solve:
1) how does your callback know what button was pushed?
2) how do you store and access data associated with that button?
What makes sense depends on your application, but I personally like the lambda trick -- and you can pass any python object (or more than one) through that way)
Your mission, should you choose to accept it, is to figure out this code, and then post it and an explanation in the wxPython Wiki -- your payback for the help you've gotten here.
Everyone else's mission is to add a few more methods for doing this -- (or commentary on the pluses and minuses...)
Hint: there have been a couple threads about this on this list in the last couple months....
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Here are my additions, which manipulate the event client data:
... in the __init__ method before setting the layout
### Setting the Event Client Data
ButtonSizer = wx.StaticBoxSizer(wx.StaticBox(self,label="Client data"),
wx.VERTICAL)
for i in range(NumButtons):
b = wx.Button(self,label=str(i))
b.Bind(wx.EVT_BUTTON,self.OnEventData)
ButtonSizer.Add(b,0,wx.EXPAND|wx.ALL,10)
MainSizer.Add(ButtonSizer)
... after the OnClick events:
def OnEventData(self,evt):
data = wx.GetTextFromUser("What do you want to say?")
evt.SetClientData(data if data else "Nothing to say")
self.ReportData(evt)
def ReportData(self,evt):
print "A button said: %s" % evt.GetClientData()
Commentary:
This is an ugly kludge I put together a while ago.
I don't use lambda functions, as I thought they were going away in
Python 3. This would suck because they are handy and I don't know if
the P3 way of writing around them will feel natural.
Josh
···
On Mon, Jul 21, 2008 at 1:27 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:
Everyone else's mission is to add a few more methods for doing this -- (or
commentary on the pluses and minuses...)
good idea -- I'll add that, It's nice to see visually which buttons are
which.
Anything in the name of clarity...
This seems to be solving a different problem (actually, I'm not sure what
problem it is solving).
As I said, it's ugly.
evt.WhatTheUserSaid = data if data else "Nothing to say"
This may risk Programming-Style-Whack-A-Mole, but I don't like this
technique, mainly because
I wouldn't come back to this and necessarily know if this is a custom
property or not. Of course, wxPython
wouldn't have WhatTheUserSaid, most likely. If I need custom
properties, I tend to subclass.
I don't use lambda functions, as I thought they were going away in
Python 3.
nope from:
Thanks. I don't keep up as much as I should.
Ultimately, it might be easiest if optional arguments and keyword
arguments could be tossed into the Bind method.
I also wonder if Connect could add some functionality. I can't seem
to get connect to work right now. I give it six arguments, and it
raises an error saying it wants 5 and I've only given it 4.
Maybe someone with better wx-fu can figure that one out.
···
On Tue, Jul 22, 2008 at 12:10 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:
--
Josh English
Joshua.R.English@gmail.com
Hi Josh,
It sounds like you did not place a self at the beginning of the
method...