1

HI guys i'm trying to simply create a text space that replaces the letters or any input into asterisks just like happen with the regular passwords. But why if i bind any key to the function "text" if i press any key the first input is empty? Becuase if i type "m" when in the function i print(e.get()) the output is empty in the console? Why it ignores the input and how to simply show the asterisk in the text area and bind the normal password to the variable "a" just like in my code? With e.delete and e.insert i simply cut the last character and replace it with asterisk here is my code

from tkinter import  *
a = ""
window = Tk()
def entra():
    global a
    if a == "cgf":
        print ("a")
    else:
        print("noo")
def text (event):
    global a
    a = a + e.get()
    print(e.get())
    e.delete(len(e.get()) - 1)
    e.insert(END, "*")
e = Entry(window, borderwidth=3, width=50, bg="white", fg="black")
e.place(x=100, y=35)
e.bind("<Key>", text)
bottone_entra = Button (window, text = "Entra", borderwidth=2.5,command = entra)
bottone_entra.place (x=100, y=65)
etichetta = Label(window, text = "PASSWORD", bg = "#2B2B2B", fg = "white")
etichetta.place(x=97.5, y=10)
window.geometry ("500x500")
window.title ("Password")
window.configure(bg = "#2B2B2B")
window.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Your binding is called *before* the Entry's own binding, so the character hasn't actually been inserted yet. You could even prevent it from being inserted by doing `return "break"` in your handler. – jasonharper Apr 27 '22 at 16:55

1 Answers1

1

You don't have to manually build a logic for it, tkinter luckily already provides it. You just have to set an extra option to the entry widget, show='*':

e = Entry(window, borderwidth=3, width=50, bg="white", fg="black", show='*')

There are many reasons as to why you should avoid building/using this logic of your own, to begin with, once you do get(), you will only get **** and not whatever the text originally was.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46