Hide text in entry box when i click on it.(GUI using Tkinter in python)

Peter Otten __peter__ at web.de
Wed Jan 25 04:18:41 EST 2017


hmmeeranrizvi18 at gmail.com wrote:

> Hello Guys,
> Here i am creating a entry box with some text,i need to hide the text when
> i click on it. Here is my code
> 
> from Tkinter import *
> obj = Tk()
> b = Entry(obj,width=100)
> b.insert(0,"Enter the value to search")
> b.pack()
> mainloop()

You need to bind a callback function to a mouse event:

import Tkinter as tk

def clear_search(event):
    search.delete(0, tk.END)

root = tk.Tk()

search = tk.Entry(root, width=100)
search.insert(0, "Enter the value to search")
search.pack()
search.bind("<Button-1>", clear_search)

root.mainloop()

This will always clear the Entry; you probably want to check if it contains 
the initial message first to avoid that the user accidentally loses input.




More information about the Python-list mailing list