By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

Help needed with the Tkinter Entry widget

Elias Alhanatis
P: 56
Hello to everybody!!
I am running Python 2.5.1 on Windows Vista and i have a problem with the
"Entry" widget of Tkinter. Take a look at this code:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. def fetch():
  4.     q=(e.get())
  5.     print q
  6.  
  7. root=Tk()
  8. f=Frame(root)
  9. f.pack()
  10. e=Entry(f,width=26)
  11. e.pack()
  12. e.focus()
  13.  
  14. e.bind("<Any-KeyPress>",(lambda event: fetch()))
  15.  
  16. root.mainloop()
The problem i have ( as you can verify if you run the script) , is that
although the first Key-Press is shown in the Entry field , the computer
doesn't assign it to the variable which it is assigned , until a SECOND
Key-Press occurs. The same goes for the second one ( although it appears on the
widget , its not assigned until the THIRD Key-Press occurs ) and so on...

Could anyone tell me if this is normal behaviour (and if so how can i "correct" it,
or my code has some kind of flaw?

Many thanks in advance!!!!
Oct 3 '07 #1
Share this Question
Share on Google+
3 Replies


ilikepython
Expert 100+
P: 844
Hello to everybody!!
I am running Python 2.5.1 on Windows Vista and i have a problem with the
"Entry" widget of Tkinter. Take a look at this code:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. def fetch():
  4.     q=(e.get())
  5.     print q
  6.  
  7. root=Tk()
  8. f=Frame(root)
  9. f.pack()
  10. e=Entry(f,width=26)
  11. e.pack()
  12. e.focus()
  13.  
  14. e.bind("<Any-KeyPress>",(lambda event: fetch()))
  15.  
  16. root.mainloop()
The problem i have ( as you can verify if you run the script) , is that
although the first Key-Press is shown in the Entry field , the computer
doesn't assign it to the variable which it is assigned , until a SECOND
Key-Press occurs. The same goes for the second one ( although it appears on the
widget , its not assigned until the THIRD Key-Press occurs ) and so on...

Could anyone tell me if this is normal behaviour (and if so how can i "correct" it,
or my code has some kind of flaw?

Many thanks in advance!!!!
The problem is that you are calling e.get() before the entry widget gets the key press. I think you can fix it with this:
Expand|Select|Wrap|Line Numbers
  1. def fetch(event):
  2.     print event.char + e.get()
  3.  
  4.  
  5. e.bind("<Any-KeyPress>", fetch)
  6.  
If that doesn't work, try printing another member of the event object.
Oct 4 '07 #2

bartonc
Expert 5K+
P: 6,596
The problem is that you are calling e.get() before the entry widget gets the key press. I think you can fix it with this:
Expand|Select|Wrap|Line Numbers
  1. def fetch(event):
  2.     print event.char + e.get()
  3.  
  4.  
  5. e.bind("<Any-KeyPress>", fetch)
  6.  
If that doesn't work, try printing another member of the event object.
I like it!
It may be the other way 'round, though:
Expand|Select|Wrap|Line Numbers
  1. def fetch(event):
  2.     print e.get() + event.char
  3. # and maybe like this is more general
  4. #    print event.widget.get() + event.char
  5.  
  6. e.bind("<Any-KeyPress>", fetch)
  7.  
Oct 4 '07 #3

Elias Alhanatis
P: 56
Dear friends ,

I am realy GRATEFULL for your quick and helpfull reply!!!!
I was trying to do solve this problem for 3 days (..and nights...) and realy got
frustrated.......

Thanks again!!!!
Oct 4 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.