how to return value from button clicked by python

C

chen tao

Hi,
I have several buttons, I want to realize: when I click first
button, the button will call a function, and the function should
return some parameter value, because I need this value for the other
buttons.
I tried the button.invoke() function, it almost got it...however,
I only want it returns value when the button clicked, but because the
program is in the class _ini_ function, so it always runs once before
I click the button...
Any one can give me some suggestions?
My scripts is like this:
def call1():
.....
return para

def app():
....
class GUI():
def __init__(self,parent = None):
........
B1 = Button(self.master, text ='browse...', command = call1)
B1.grid(row=len(self.user_input_index),column = 3,sticky=W)
print self.B1.invoke()
call2 = lambda: app(self.B1.invoke())
B2 = Button(self.master, text ='browse...',command = call2)
 
R

r

    I tried the button.invoke() function, it almost got it...however,
I only want it returns value when the button clicked, but because the
program is in the class _ini_ function, so it always runs once before
I click the button...

so don't call the function in the __init__ method OR just write a
def...?
 
H

Hendrik van Rooyen

As Tim said - you cannot return anything, as the button command is called by
the main GUI loop.

If you really want to, you can set a variable global to the class, (Like
self.my_passed_param) and use it in the second button - however then you have
to handle cases where the user does not click the buttons in the sequence you
expect.
You're thinking of your program in the wrong way. When you write a GUI,
things don't happen in order. Your __init__ function merely sets up your
window structure. The window has not actually been created or displayed at
that time.

Later, when you call the "mainloop" function, it will start to process
messages. Your window will be displayed, and then you'll go idle while
waiting for user input. When you click a button, the mainloop will call
your handler, do a little processing, and return.

So, your button function can't really return anything. There's nothing to
return it TO. If there is some action you need to take when the button is
clicked, then you DO that function in the button handler.

This is perfectly right.

- Hendrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top