1

I have created a gui using tkinter in which I created a new window with Toplevel widget and added the widgets to this window such as a entry box , label and button widget . The command attribute of the button widget calls a callback function using the lambda expression such that the gui must not proceed before executing this callback function . But , due to some problem when I press the button widget the callback function is not called .

def toplevel_widget(obj):
    def suppress():
        try:
            if obj.fillna_value.get():
                fill_na_value_window.withdraw()
            else:
                raise Empty_na_value_entry_Error("The textbox can't be left empty!!\nFill any data in it.")
        except Empty_na_value_entry_Error as e:
            msgbox.showerror("Error",e.msg)

    fill_na_value_window=tk.Toplevel(obj)
    obj.fillna_value=tk.StringVar()
    tk.ttk.Label(fill_na_value_window,text="Enter the value to be inserted").grid(row=1,column=0,padx=10,pady=10)
    tk.ttk.Entry(fill_na_value_window,textvariable=obj.fillna_value,width=15).grid(row=1,column=1,padx=10,pady=10)
    tk.ttk.Button(fill_na_value_window,text="Ok",command=lambda : suppress,width=10).grid(row=2,columnspan=2)


    Cleanser_manual.fillna_toplevel_widget(obj)
    val=obj.fillna_value.get()
    print("Value : ",val)  #here **Value :    is printed on screen**

Here the suppress() function is not called even after the button is clicked

1
  • 3
    Because the callback you passed simply returns the suppress function object . Don't use lambda, just pass suppress itself (you could use lambda: suppress() but that's just redundant) Commented Jul 23, 2019 at 19:30

3 Answers 3

1

You are not calling supress inside the lambda function, you missed the parenthesis

(..., command=lambda: supress(), ...)

At any case you don't need lambda at all, you could just pass the supress function as parameter

(..., command=supress, ...)
Sign up to request clarification or add additional context in comments.

Comments

1

To expand on @angeldeluz777's answer:

What you are trying to do is pass a reference to a callback function as the command argument to Button. However, what command=lambda : suppress is actually doing is passing in a lambda function that does nothing except return the reference to the callback function, which is why the button click is having no effect.

Change your code from:

 tk.ttk.Button(fill_na_value_window,text="Ok",command=lambda : suppress,width=10).grid(row=2,columnspan=2)

to:

 tk.ttk.Button(fill_na_value_window,text="Ok",command=suppress,width=10).grid(row=2,columnspan=2)

This might help illustrate what's going on:

Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     print("Hello")
...
>>> callback = f
>>> callback()
Hello
>>> callback2 = lambda: f
>>> callback2()
<function f at 0x7fafacc10c80>
>>>

Comments

0

As @juanpa.arrivillaga said.

tk.ttk.Button(fill_na_value_window,text="Ok",command=suppress,width=10).grid(row=2,columnspan=2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.