[Tutor] Tkinter: Deleting text when clicking in field.

brandon w thisisonlyatest at gmx.com
Sat Aug 20 21:12:53 CEST 2011


I worked on this for a long time. I did many searches to fix the many 
error messages I was getting and I finally got this to work. I would now 
just like to have the text disappear when a person clicks in the box to 
type something. How can I do that?

(This is just a sample of the whole program.)

from Tkinter import *

class MyGrid(Frame):
     def __init__(self, win=None):
         Frame.__init__(self, win)
         self.grid()
         self.mkWidgets()

[...snip...]

self.mytext = StringVar()
         self.mytext.set("Enter text here")  # This text needs to be 
deleted upon clicking in the field.
         self.e = Entry(bg='orange', textvariable=self.mytext, 
relief=SUNKEN, width=50)
         self.e.grid(row=0, column=0)

[...snip...]

app = MyGrid()
app.mainloop()

#==============================================================END

I have created a method to clear the field.
This is not working:

def clearBox(self):
         self.mytext.delete(0, END)
         return

self.mytext = StringVar(None)
         self.mytext.set("Enter text here")
         self.e = Entry(bg='orange', textvariable=self.mytext, 
relief=SUNKEN, width=50, command=self.clearBox)
         self.e.grid(row=0, column=0)

This does not work either:

def clearBox(self):
         self.mytext.get()
         self.mytext.delete(0, END)
         return

self.mytext = StringVar(None)
         self.mytext.set("Enter text here")
         self.e = Entry(bg='orange', textvariable=self.mytext, 
relief=SUNKEN, width=50, command=self.clearBox)
         self.e.grid(row=0, column=0)

This either:

def clearBox(self):
         #self.mytext.get()
         self.e.delete(0, END)
         return




More information about the Tutor mailing list