Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

3,199 views
Skip to first unread message

hmmeera...@gmail.com

unread,
Jan 25, 2017, 2:45:11 AM1/25/17
to
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()

Peter Otten

unread,
Jan 25, 2017, 4:19:21 AM1/25/17
to
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.

hmmeera...@gmail.com

unread,
Jan 25, 2017, 4:53:30 AM1/25/17
to
Thanks Peter

Christian Gollwitzer

unread,
Jan 25, 2017, 2:12:27 PM1/25/17
to
Am 25.01.17 um 10:18 schrieb Peter Otten:
> hmmeera...@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.

> search.bind("<Button-1>", clear_search)
>

This is the correct answer for a mouse click. The typical use case
(disappearing placeholder) should also trigger, when somebody uses the
tab key to set the focus to the window. This can be achieved by binding
to the <FocusIn> event instead:

search.bind("<FocusIn>", clear_search)

In addition, repeated clicking does not clear the text then.

Christian

tonysm...@gmail.com

unread,
Nov 11, 2017, 8:08:21 PM11/11/17
to
Thanks it was helpful
Message has been deleted

saksham...@gmail.com

unread,
May 31, 2019, 3:08:40 PM5/31/19
to
NOT WORKING IN MINE PLZ HELP



from tkinter import *
g=Tk()
g.geometry("{0}x{1}+0+0".format(g.winfo_screenwidth(), g.winfo_screenheight()))
g.title("Check")
g.configure(background='powder blue')


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

e1=Entry(g)
e1.insert(0,'username')
e1.pack()
e1.bind("<Button-1>", clear_search)
e1.bind("<FocusIn>", clear_search)

g.mainloop()

Chris Angelico

unread,
May 31, 2019, 3:24:23 PM5/31/19
to
Did you look at the exception you get when this runs? It might
possibly be able to point you to the problem, and maybe suggest how
you could resolve it (by importing something, perhaps).

ChrisA

Rhodri James

unread,
May 31, 2019, 3:24:39 PM5/31/19
to
On 31/05/2019 20:08, saksham...@gmail.com wrote:
> NOT WORKING IN MINE PLZ HELP
>
>
>
> from tkinter import *
> g=Tk()
^^
> g.geometry("{0}x{1}+0+0".format(g.winfo_screenwidth(), g.winfo_screenheight()))
> g.title("Check")
> g.configure(background='powder blue')
>
>
> def clear_search(event):
> e1.delete(0, tk.END)
^^
>
> e1=Entry(g)
> e1.insert(0,'username')
> e1.pack()
> e1.bind("<Button-1>", clear_search)
> e1.bind("<FocusIn>", clear_search)
>
> g.mainloop()

Spot the difference between the identifiers I underlined. If you run
this from the command line, the traceback tells you exactly what is
going on.

--
Rhodri James *-* Kynesim Ltd
0 new messages