3
votes

I have the following Entry box where due to obtaining values I have put a list option in for textvariable.

However I was wondering if it would be possible to put a default text in the background so to show which values are required in each box (like a greyscale text, 'Value 1, value 2 etc..).

self.numbers = [StringVar() for i in xrange(self.number_boxes) ] #Name available in global scope.       
box=Entry(self.frame_table,bg='white',borderwidth=0, width=10, justify="center", textvariable=self.numbers[i])

Can I add in maybe something change 'textvariable' upon a mouse click inside the box or can I simply just add in another textvariable or text to set a default text?

  self.box = []

  for i in xrange(self.number_boxes):

        self.clicked = False
        self.box.append(Entry(self.frame_table,bg='white',borderwidth=0, width=10, justify="center", textvariable=self.numbers[i], fg='grey'))
        self.box[i].grid(row=row_list,column=column+i, sticky='nsew', padx=1, pady=1) 
        self.box[i].insert(0, "Value %g" % float(i+1))
        self.box[i].bind("<Button-1>", self.callback)
1
Thanks. I can't used pack or grid with a list though. =S - user2063
Ok, I've got it but it only seems to work with the first box in the callback. I can't use box[i] with the append line, the others are all ok..I get the same error with append 'list out of range'. - user2063
You will have to learn lamba functions so that you can pass i to the callback function. The callback will change to accept i as an input parameter: def callback(event, i): in the function definition and box[i] in the body of the callback. Search stackoverflow for examples of lamba functions. Best of luck. - gary
Ok, thank you again for your help =) - user2063
Glad it helped. I'll check out your other questions when I get a chance. - gary

1 Answers

4
votes

In order to put default text in your Entry widget, you can use the insert() method as described here.

box.insert(0, "Value 1")    # Set default text at cursor position 0.

Now in order to change the contents of box after the user performs a mouse click inside the box, you will need to bind an event to the Entry object. For example, the following code deletes the contents of the box when it is clicked. (You can read about event and bindings here.) Below I show a full example of this.

Note that deleting the text in the box is probably only practical for the first click (i.e. when deleting the default contents), so I created a global flag clicked to keep track of whether it has been clicked.

from tkinter import Tk, Entry, END    # Python3. For Python2.x, import Tkinter.

# Use this as a flag to indicate if the box was clicked.
global clicked   
clicked = False

# Delete the contents of the Entry widget. Use the flag
# so that this only happens the first time.
def callback(event):
    global clicked
    if (clicked == False):
        box[0].delete(0, END)         #  
        box[0].config(fg = "black")   # Change the colour of the text here.
        clicked = True

root = Tk()
box = []                              # Declare a list for the Entry widgets.

box.append(Entry(fg = "gray"))        # Create an Entry box with gray text.
box[0].bind("<Button-1>", callback)   # Bind a mouse-click to the callback function.
box[0].insert(0, "Value 1")           # Set default text at cursor position 0.

box.append(Entry(fg = "gray"))        # Make a 2nd Entry; store a reference to it in box.
box[1].insert(0, "Value 2")

box[0].pack()                         #
box[1].pack()

if __name__ == "__main__":
    root.mainloop()