how do i remove focus of my entry in python tkinter?

  Kiến thức lập trình
import tkinter as tk
from tkinter import *

placeholderg = 'username'
placeholders = 'password'

root = Tk()
root.title("???")
root.geometry("1920x1080")

def erase_username(event=None):
    if username.get() == placeholderg:
      username.delete(0,'end')
def add_username(event=None):
    if username.get() == '':
      username.insert(0,placeholderg)
def erase_password(event=None):
  password.config(show="*")
  if password.get() == placeholders:
      password.delete(0,'end')
def add_password(event=None):
    if password.get() == '':
      password.config(show="")
      password.insert(0,placeholders)

username = Entry(root)
add_username()
username.bind('<FocusIn>',erase_username)
username.bind('<FocusOut>',add_username)
username.place(x=250,y=100)

password = Entry(root)
add_password()
password.bind('<FocusIn>',erase_password)
password.bind('<FocusOut>',add_password)
password.place(x=250, y=125)

if you click one of the entry’s it has focus and it will delete the placeholder so you can write in it without having to delete the placeholder yourself. When it loses focus and the entry is empty it puts the placeholder back. My problem is that on this the only way to lose focus is by clicking the other entry, but I want that it also loses focus if you click on the window or anything that is not the entry.

1

LEAVE A COMMENT