Reputation: 194
Please note that this is written in python 3
I am using Tkinter and am trying to call the foo-function by pressing the button in the tk-window and get "Hello World!" printed, what am I doing wrong?
from tkinter import *
window1 = Tk()
class WidgetCreate(object):
def __init__(self, widget_type, window_num, text_str, fun, numr, numc):
self.obj = Button(window_num, text=text_str, command=lambda: fun)
self.obj.grid(row=numr, column=numc)
def foo():
print("Hello World!")
but1 = WidgetCreate("Button", window1, "This Button 1", foo, 1, 1)
window1.mainloop()
The button is visible in the to-window however when pressed nothing happens :(
Upvotes: 0
Views: 390
Reputation: 3797
You miss the parenthesis in your command
argument to Button
. So your lambda function doesn't call the function you want. It has to be:
self.obj = Button(window_num, text=text_str, command=lambda: fun())
Or even simpler you do it without the lambda function and give fun
directly as argument:
self.obj = Button(window_num, text=text_str, command=fun)
Upvotes: 2