-1

I am writing a GUI application in python with tkinter. I have a function that is used in the button widget to change the text of an entry widget. When I run the python script, the entry widget updates with the text before I click the button widget. Can anyone tell me why it is doing this and how to fix it?

Please see my code below:

import tkinter as tk

def changeText(TKEntry, text):
    TKEntry.insert(0, text)

def buildMain():
    window = tk.Tk()
    window.title("Login")
    window.geometry("200x110")

    lblLogin = tk.Label(window, text="Log In")
    lblLogin.place(x=85,y=0)

    lblUser = tk.Label(window, text="Username:")
    lblUser.place(x=0,y=30)
    
    edtUser = tk.Entry()
    edtUser.place(x=70,y=30)
    
    lblPass = tk.Label(window, text="Password:")
    lblPass.place(x=0,y=50)

    edtPass = tk.Entry()
    edtPass.place(x=70,y=50)

    btnLogin = tk.Button(text="Log In", command= changeText(edtUser,"Text Changed"))
    btnLogin.place(x=150, y=80)

    window.mainloop()

buildMain()
szluhab
  • 1
  • 2
  • 2
    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) – Matiiss Jul 23 '21 at 12:11

1 Answers1

0

You need lambda:

command=lambda:changeText(edtUser,"Text Changed")

I think when the button was defined, the function was executed. That's why the entry was updated