Annonymous
Annonymous

Reputation: 978

How to get buttons to get a function to return something?

Python has the very useful lambda function, which I am using in a button, but I need it so that when the button is pressed it makes the main function return a string (or boolean), let us say True.

Here is an example code:

from tkintet import *

def buttonwindow():
    tk = Tk()
    button = Button(tk, text = "Press Me!", commmand = lambda : return True)
    button.pack() 
print (buttonwindow())

but before it can even get to print, Python indicates that there is a bug in the code at the end of the word return. I think it might be because return True is doing so for the lambda (which I don't want anyway, because I need buttonwindow(), not the lambda, to return True) , maybe a function which lambdas cannot do.

Does anyone know how I could get it so that buttonwindow() returns true. I have already tried using no lambda (e.g: ...command = return True) fails aswell. Using lambdas is probably the wrong idea but I need to know what the right idea is. Thanks in advance for any answers!

Upvotes: 0

Views: 2345

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385880

The core question you seem to be asking is "Does anyone know how I could get it so that buttonwindow() returns true"

The answer to that is to better understand how tkinter works, and then craft a solution using that knowledge. You can't get there by figuring out how to get lambda to return true. The problem is much deeper than a couple lines of code can solve.

If you're trying to create a function that opens a window and waits for user input before returning, you need to do a couple of things. First, you need to make sure you run the event loop after creating your widget(s). You then need to create a callback function that sets a value somewhere accessible to the main function. Next, you need to have the callback function stop the event loop after the user enters their data. Finally, you need to get that data and return it after the event loop stops.

The easiest way is to create a class, so that you can use an instance variable to pass data around. Following is a working example:

import tkinter as tk

class Example(object):
    def __init__(self):
        self.value = None
        self.root = None

    def show(self):
        '''Show the window, and wait for the user to click a button'''

        self.root = tk.Tk()
        true_button = tk.Button(self.root, text = "True", 
                                command= lambda: self.finish(True))
        false_button = tk.Button(self.root, text = "False", 
                                 command= lambda: self.finish(False))

        true_button.pack()
        false_button.pack()

        # start the loop, and wait for the dialog to be
        # destroyed. Then, return the value:
        self.root.mainloop()
        return self.value

    def finish(self, value):
        '''Set the value and close the window

        This will cause the show() function to return.
        '''
        self.value = value
        self.root.destroy()


print("getting ready to show dialog...")
print("value:", Example().show())

Upvotes: 1

Joel Cornett
Joel Cornett

Reputation: 24788

Why there's a "bug" at return

A lambda function implicitly returns the value of the evaluated expression, so explicitly using return is superfluous, and (as you have discovered) illegal in Python.

Therefore, the lambda function:

lambda: "foo"

Is equivalent to:

def equivalent_function():
    return "foo"

How to get "True" to print to stdout

If you simply want to print True to stdout, you can do:

def my_callback():
    print(True)

button = Button(tk, text="Press Me!", command=my_callback)

If you are using Python 3.x, then print is just a regular function, called with parentheses, and you can use a lambda function with no problems:

button = Button(tk, text="Press Me!", command=lambda: print(True))

However, if you are using Python 2.x, print is a statement (like return), and using it in a lambda function will raise an error. You can use the Python 3.x print function in Python 2.x by including the following import at the top of your script:

from __future__ import print_function

Upvotes: 1

Related Questions