![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2007
Posts: 13
Rep Power: 0
![]() |
Can a button perform two actions?
I've build a GUI script and I would like one of the buttons to perform an action that I have defined in the script and also quit the GUI interface. I can only figure out how to assign the button to do one or the other, but not both. Any advice? Thanks!
![]() |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0
![]() |
You could create a class that implements __call__ that takes all of the methods and calls them in turn. Here's a version that returns all of the return values in a list, unless an exception is thrown, in which case None is returned.
-- Paul class Multicall(object):
def __init__(self,*args):
# all methods must have same signature
self.methods = args
def __call__(self,*args):
retList = [None]*len(self.methods)
for i,m in enumerate(self.methods):
try:
retList[i] = m(*args)
except:
# change 'break' to 'continue' if you want all methods to run
break
return retList
addOne = lambda x : x + 1
addTen = lambda x : x + 10
invert = lambda x : 1.0 / x
add100 = lambda x : x + 100
callAll = Multicall( addOne, addTen, invert, add100 )
print callAll( 5 )
print callAll( 0 )
"""
prints:
[6, 15, 0.20000000000000001, 105]
[1, 10, None, None]
""" |
|
|
|
|
|
#3 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Quote:
If you really want some manner of serial function caller, why not simply: python Syntax (Toggle Plain Text)
|
||
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
Yeah... ptmcg's response is definitely going overboard. Not to mention, most likely not even applicable under these circumstances
If it were wxWidgets, it would be as simple as this: class MyFrame(wxFrame):
...
def _event_okay(self, event=None):
# processes task for okay button
if event != None:
event.skip()
def _event_quit(self, event=None):
# processes task for quit button
if event != None:
event.skip()
def _event_other_button(self, event):
# processes two tasks sequentially
self._event_okay()
self._event_quit()
event.skip()
...Unless I grossely misunderstand the concern? ... |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0
![]() |
:beard: I guess my OO training from Smalltalk/C++/Java days is still showing, I instinctively jump to writing a class, especially since Python offers such nice built-in class impersonation features, such as classes as numerics (for operator overloading) or classes as callables.
After I posted that last note, it dawned on me that a simple closure (as Arevos so clearly showed) would do just as well. I think I'll revisit pyparsing one of these days and see how much "class overkill" can be winnowed out. Fortunately, I *can* be trained... |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2007
Posts: 13
Rep Power: 0
![]() |
Sorry for the late reply. I am using Tkinter.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem Associated with Vector Source code | buggytoast | Java | 3 | Apr 2nd, 2006 5:41 AM |
| Disabling "Ok" button | jayme | C++ | 2 | Jan 9th, 2006 4:50 AM |
| IE and dynamic button creation | MegaArcon | JavaScript and Client-Side Browser Scripting | 5 | Dec 6th, 2005 8:31 AM |
| GUI Progress Bar | badbasser98 | C++ | 55 | Nov 4th, 2005 7:28 AM |
| move program console window back | badbasser98 | C++ | 21 | Oct 18th, 2005 2:02 PM |