Search code examples
pythontkinterpasswordshidehint

Tkinter - Hide Hint


How can this hint be automatically deleted when I select the username and password section? (Don't show after clicking)

from tkinter import *
root =Tk()
def save_fonc():
    kul=entry1.get()
    pas=entry2.get()
    print("Username:",kul,"Password:",pas)

#------------------
entry1=Entry(root)
entry1.insert(0,"Username")
entry1.pack()
#------------------
entry2=Entry(root)
entry2.insert(0,"Password")
#entry2.config(show="*")#and also I don't want to show password section.
entry2.pack()
#------------------
buton_kaydet=Button(root,text="Enter",command=save_fonc)
buton_kaydet.pack()

root.geometry("300x200")
root.mainloop()

Solution

  • So here You go improved Your code:

    from tkinter import *
    root = Tk()
    
    
    def save_fonc():
        kul = entry1.get()
        pas = entry2.get()
        print("Username:", kul, "Password:", pas)
    
    
    def pass_func(event):
        entry2.delete(0, 'end')
        entry2.config(show='*')
    
    # ------------------
    entry1 = Entry(root)
    entry1.insert(0, "Username")
    entry1.pack()
    entry1.bind('<FocusIn>', lambda e: entry1.delete(0, 'end'))
    # ------------------
    entry2 = Entry(root)
    entry2.insert(0, "Password")
    entry2.pack()
    entry2.bind('<FocusIn>', pass_func)
    # ------------------
    buton_kaydet = Button(root, text="Enter", command=save_fonc)
    buton_kaydet.pack()
    
    root.geometry("300x200")
    root.mainloop()
    

    So basically .bind binds each entry to an event in this case: '<FocusIn>' which is triggered when focus switches to that widget. Also in the second case the entry is configured only after event trigger so that 'Password' is visible as normal