-4

So I have this example code:

Import tkinter as tk
window = tk.Tk() 
def showText(myText):
    label = tk.Label(window, text=myText)
    label.pack()
button = tk.Button(window, text="Press me", command=showText("Hello World"))
button.pack()
window.mainloop()

If I run this, the label would be displayed instantly, how can I achieve the same result, but have the label with the myText parameter displayed after the button press?

  • this problem was many times - use `lambda` like `commad=lambda:showText("Hello World")`. And if you use `lambda` inside `for`-loop to assing function with different values (i.e using `for x in ... `) then do `command=lambda val=x:function(val)` instead of `command=lambda:function(x)` – furas May 08 '22 at 12:23
  • Does this answer your question? [Why is the command bound to a Button or event executed when declared?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) – Sylvester Kruin May 08 '22 at 14:42

0 Answers0