Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 19th, 2007, 1:42 PM   #1
poopman
Newbie
 
Join Date: Feb 2007
Posts: 13
Rep Power: 0 poopman is on a distinguished road
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!
poopman is offline   Reply With Quote
Old Feb 19th, 2007, 2:57 PM   #2
ptmcg
Newbie
 
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0 ptmcg is on a distinguished road
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]
"""
ptmcg is offline   Reply With Quote
Old Feb 19th, 2007, 3:35 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by poopman View Post
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!
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:
python Syntax (Toggle Plain Text)
  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.
Arevos is offline   Reply With Quote
Old Feb 19th, 2007, 6:19 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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?
...
Sane is offline   Reply With Quote
Old Feb 20th, 2007, 9:17 AM   #5
ptmcg
Newbie
 
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0 ptmcg is on a distinguished road
: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...
ptmcg is offline   Reply With Quote
Old Feb 26th, 2007, 1:09 PM   #6
poopman
Newbie
 
Join Date: Feb 2007
Posts: 13
Rep Power: 0 poopman is on a distinguished road
Sorry for the late reply. I am using Tkinter.
poopman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:27 AM.

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