0

I have a following peice of code where I want query button to disappear when entry box is empty. But that is not happening.When I tried to print the entry value on pressing any key, it shows the last value, not the current value.

eg. if "Test" is previous value in pin, than on hitting backspace pin.get() shows Test instead of "Tes". I am new to python and Tkinter, so don't know what silly thing I am doing.

def send_button ():
    print pin.get()
    if pin.get() == "":
        print "in if"
        query.place_forget()
    else: 
        query.place(relx=0.0, rely=0.75, relwidth=0.2, relheight=0.15)
    return TRUE
#Entry box
pin = StringVar()
ent1 = Entry(top, textvariable = pin, validate = 'key', vcmd = send_button)

#Query button
query = Button (top, command = get_temp, text = 'Query')
top.mainloop()
dimo414
  • 47,227
  • 18
  • 148
  • 244
Dcode
  • 223
  • 1
  • 14
  • One thing found that on using "is" in place of "==" for string match, code is going in the if loop. But again, it is running one character behind. And at the end, when entry is empty, it doesn't go in send_button function. After that on typing any character, query button disappears and came after typing second character – Dcode May 04 '16 at 05:14

1 Answers1

0

validation happens before the character is inserted in the widget. That's it's whole purpose: to decide if the edit should be allowed or not. It can't make that decision of the text has already been entered (or deleted).

Entry validation has a way to pass in the value of the text before it is entered, after it is entered, what is being inserted or deleted, and several other pieces of information.

See this answer for a comprehensive example:

https://stackoverflow.com/a/4140988/7432

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685