Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Can a button perform two actions? (http://www.programmingforums.org/showthread.php?t=12622)

poopman Feb 19th, 2007 1:42 PM

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! :D

ptmcg Feb 19th, 2007 2:57 PM

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]
"""


Arevos Feb 19th, 2007 3:35 PM

Quote:

Originally Posted by poopman (Post 124148)
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! :D

What GUI system are you using? There are a number of them available for Python, and the answer will depend upon your choice.

Quote:

Originally Posted by ptmcg
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.

Uh, that doesn't really sound like the best solution to the problem, as ingenious as it is. Why do you need the return values? Presumably the callback function attached to the button requires no return value, so there's no need for that code. And you might as well keep the exceptions, as if the return value is discarded, you'd want them around if something goes wrong.

If you really want some manner of serial function caller, why not simply:
:

  1. def multicall(*funcs):
  2.     def caller(*args):
  3.         for func in funcs: func(*args)
  4.     return caller

However, it's probably just easier to create your own function.

Sane Feb 19th, 2007 6:19 PM

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?
...

ptmcg Feb 20th, 2007 9:17 AM

: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...

poopman Feb 26th, 2007 1:09 PM

Sorry for the late reply. I am using Tkinter.


All times are GMT -5. The time now is 2:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC