0

I want a placeholder entry that is done by the below code. but I need the input password which would be hidden from the user (which may be in the form of asterisks.)

from tkinter import *

root=Tk()
root.geometry("300x200+600+250")
root.config(background="#E0FFFF")
root.resizable(False,False)


def userText(event):
    e1.delete(0,END)
    usercheck=True

def passText(event):
    e2.delete(0, END)
    passcheck=True



a=StringVar()
b=StringVar()
usercheck=False
passcheck=False


Label(root,text="User name",bg="#E0FFFF").place(x=20,y=50)
e1= Entry(root,textvariable=a)
e1.place(x=100,y=50)
e1.insert(0,"Enter username")
e1.bind("<Button>",userText)

Label(root,text="Password",bg="#E0FFFF").place(x=20,y=95)
e2= Entry(root,textvariable=b)
e2.place(x=100,y=95)
e2.insert(0,"Enter password")
e2.bind("<Button>",passText)


root.mainloop()
Super
  • 23
  • 1
  • 8
  • 1
    Does this answer your question? [How to create a password entry field using Tkinter](https://stackoverflow.com/questions/2416486/how-to-create-a-password-entry-field-using-tkinter) – Matthias Jun 11 '20 at 10:17
  • Thanks for your reply. but this is not what I want. I want a placeholder too it should be visible to the user but when user click on the entry for writing password that input(password )should in the form of asterisks – Super Jun 11 '20 at 10:23
  • ***is not what I want***: You want to show `"Enter password"` which disapears on start typing then show asterisks? – stovfl Jun 11 '20 at 11:06
  • You can set `show="*"` when the password entry gets the focus. – acw1668 Jun 11 '20 at 11:11
  • @Super Does this answer your question? [[How to add placeholder to an Entry in tkinter?](https://stackoverflow.com/a/47928390/7414759) combine it with the first comment linked answer. – stovfl Jun 11 '20 at 11:15
  • @acw1668 I did this but it converts the "enter password" into asterisk. – Super Jun 11 '20 at 12:07
  • @stovfl yes you got what I want but the link you provide me is not giving my answer! – Super Jun 11 '20 at 12:09
  • ***the link you provide***: Implement it according the linked answer. After it is working extend with `show="*"` and vice versa `show=""`. In `""` and `<"Leave">` – stovfl Jun 11 '20 at 12:12
  • You need to set `show=""` when the password entry ***loses focus and nothing is entered***. Set `show="*"` when the password entry ***get focus or something is entered***. – acw1668 Jun 11 '20 at 12:13

2 Answers2

0

Extended tkinter.Entry showing placeholder text and * on a password entry.


import tkinter as tk


class Entry(tk.Entry):
    def __init__(self, master, placeholder):
        super().__init__(master)

        self.placeholder = placeholder
        self._is_password = True if placeholder == "password" else False

        self.bind("<FocusIn>", self.on_focus_in)
        self.bind("<FocusOut>", self.on_focus_out)

        self._state = 'placeholder'
        self.insert(0, self.placeholder)

    def on_focus_in(self, event):
        if self._is_password:
          self.configure(show='*')

        if self._state == 'placeholder':
            self._state = ''
            self.delete('0', 'end')

    def on_focus_out(self, event):
        if not self.get():
          if self._is_password:
            self.configure(show='')

          self._state = 'placeholder'
          self.insert(0, self.placeholder)

Usage:

if __name__ == "__main__": 
    root = tk.Tk() 

    username = Entry(root, "username")
    username.pack()

    password = Entry(root, "password")
    password.pack()  

    root.mainloop()
stovfl
  • 14,998
  • 7
  • 24
  • 51
0

to hide the password use:

e2.config(show='*')

Sytze
  • 39
  • 1
  • 10
  • 1
    See "[Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". While this might be technically correct, it doesn't explain why it solves the problem or should be the selected answer. We should educate along with helping solve the problem. – the Tin Man Mar 22 '22 at 04:48