How to autofill another entry from previous entry in Python tkinter

Viewed 68

I am creating a program for extracting the details of a user. So far, so good. The entries working properly along with labels and export at the end as .txt and as qrcode. The only problem is that I want the username to autofill by combining the firstname + lastname seperated by a dot. eg. Firstname : John, Lastname : Wick, username : John.Wick. How I can manage the entry of the username combine the entry given for firstname and the entry given for the lastname? The only thing I can manage so far is

firstname = StringVar()
lastname = StringVar()
Username = StringVar()
Country = StringVar()
Team = StringVar()

first_name_entry = Entry(textvariable=firstname,width="30")
last_name_entry = Entry(textvariable=lastname,width="30")
Username_entry = Entry(textvariable=firstname,width="30")
Country_entry = Entry(textvariable=Country,width="30")
Team_entry = Entry(textvariable=Team,width="30")

So basically I get only the firstname. How can I add also there the lastname?

1 Answers

What you could do is use the trace method of the StringVar(), and define a function to insert onto the entry widget, like:

def suggest(*args):
    Username_entry.delete(0,'end')
    Username_entry.insert(0,f'{firstname.get()}.{lastname.get()}') #or use firstname.get()+'.'+lastname.get() instead of f strings

....#same comes

firstname.trace('w',suggest)
lastname.trace('w',suggest)

Also keep in mind, that you have assigned same textvariable for both first_name_entry and Username_entry. Make sure to remove that.

Related