19

how do i get the return value A to C? I am not using class by the way.

def button:
    mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
    A = B.get()
    return A

B = StringVar()
C = ""
myentry = Entry(myGui, textvariable = B).grid(row = 1, column = 0)
Submit = Button(myGui, text = "Submit", command = button).grid(row = 1, column = 1)
0

8 Answers 8

24

Short answer: you cannot. Callbacks can't return anything because there's nowhere to return it to -- except the event loop, which doesn't do anything with return values.

In an event based application, what you typically will do is set an attribute on a class. Or, if you're a beginner, you can set a global variable. Using a global variable isn't a good idea for real code that has to be maintained over time but it's OK for experimentation.

So, for example, since C appears to be a global variable in your example, you would do something like:

def button():
    global C
    mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
    A = B.get()
    C = A
Sign up to request clarification or add additional context in comments.

Comments

6

You could call C.set from within the button function:

def button:
    mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
    A = B.get()
    C.set(A)
    # return A   # return values are useless here

Comments

2

it's easy just declare A a global.

def button:
global A
mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
A = B.get()
return A

B = StringVar()`
C = ""
myentry = Entry(myGui, textvariable = B).grid(row = 1, column = 0)
Submit = Button(myGui, text = "Submit", command = button).grid(row = 1, column = 1)
# and then A is not empty
B= A

Comments

2

The only known way to get the return value of a button command is by using button.invoke(). Tkinter wraps the button command in a tcl function, but the wrapped function returns whatever the original function returns. See the source code:

def invoke(self):
    """Invoke the command associated with the button.
    The return value is the return value from the command,
    or an empty string if there is no command associated with
    the button. This command is ignored if the button's state
    is disabled.
    """
    return self.tk.call(self._w, 'invoke')

Example:

import tkinter as tk

def command():
    return 'value'

root=tk.Tk()
b = tk.Button(root, text='example', command=command)
b.pack()
value = b.invoke()
print(value)
root.mainloop() 

Comments

1

You create a child class of Tkinter.Tk, and define a member variable self.A in that class. Then you can mimic return behavior by

self.A = B.get()

See, Return values of Tkinter text entry, close GUI

Comments

0

The Answer 3 is the best Option.

But I think that happen a little error (maybe in python 3).

You have to write:

 Submit = Button(myGui, text = "Submit", command = lambda:C==button()).grid(row 
 = 1, column = 1)

Look the double "==" in "lambda: C==...".

Does work.

So the code colud be:

def button():
mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
A = B.get()
return A

B = StringVar()
C = ""
myentry = Entry(myGui, textvariable = B).grid(row = 1, column = 0)
Submit = Button(myGui, text = "Submit", command = lambda: C==button()).grid(row = 1, column = 1)

(............. and the rest of the code)

Comments

0

There are two ways to do it: 1) a global variable 2) to store the variable you want to return in another class:

1) define the function you use in a button:

def yourFunction (x):
    global variab
    variab = x

Transfer the function to your button:

button = Button (root, command=lambda: yourFunction (x))

I know that you don't like to use too many globals, so there's a second solution:

  1. create a class storing the returned variable:
class ClassToStoreReturnedVariable:
    def __init__ (self):
        self.returnedVariable = None

    def returnVariable (self, x):
        self.returnedVariable = x


returnedVariable = ClassToStoreReturnedVariable ()


class YourButton:
    def __init__ (self):
        self.anotherThing = None
        self.button = Button (root, command=lambda: returnedVariable.returnVariable (x))
        self.pack ()

The button must be written in the end of a button class, because you may want to create a function using self. Such function won't work, if init doesn't create self, before you transfer the function to the button. And don't forget to pack or grid it.

Comments

-1

Old question, but most answers suggested a global variable. I don't like using too many global variables in my projects, so here's my solution.

When declaring your Tkinter button, you can use a lambda function as the command. This lambda can interact with variables that are within the same namespace as the button you are defining. Be sure to define this variable before initializing the button.

def button():
    mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
    A = B.get()
    return A

B = StringVar()
C = ""
myentry = Entry(myGui, textvariable = B).grid(row = 1, column = 0)
Submit = Button(myGui, text = "Submit", command = lambda: C=button()).grid(row = 1, column = 1)

You may need to have self as an argument for button depending on your project organization, but the concept is the same. Lambda commands are also useful for passing arguments to the button command.

2 Comments

Look the double "==" in "lambda: C==...". as suggested by @Vicente
You can't assign to a variable inside a lambda. == isn't valid here either, what would it be comparing?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.