473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help needed with the Tkinter Entry widget

Elias Alhanatis
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
3 1885
ilikepython
844 Expert 512MB
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
6,596 Expert 4TB
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
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

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

Similar topics

3
by: Phil Schmidt | last post by:
I'm trying to make a custom entry widget, as in the code that follows. There are two problems I'm trying to fix: 1) I would like the widget to behave as myEntry.Escape() does now, except that it...
6
by: Elaine Jackson | last post by:
I've got a script where a button gets pushed over and over: to cut down on the carpal tunnel syndrome I'd like to have the button respond to presses of the Enter key as well as mouse clicks; can...
1
by: Pekka Niiranen | last post by:
Hi there, after reading TkInter/thread -recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965 I wondered if it was possible to avoid using threads for the following problem: ...
6
by: William Gill | last post by:
I am trying to get & set the properties of a widget's parent widget. What I have works, but seems like a long way around the block. First I get the widget name using w.winfo_parent(), then i...
3
by: Matt Hammond | last post by:
Here's a strange one in Tkinter that has me stumped: (I'm running python 2.4 on Suse Linux 9.3 64bit) I'm trying to make a set of Entry widgets with Label widgets to the left of each one, using...
6
by: swisscheese | last post by:
I must be missing something basic. Can anyone explain why 'A' does not show on the entry widget? import Tkinter root = Tkinter.Tk() class Col: Rows = def __init__(self): Frame =...
8
by: Lie | last post by:
Inspect the following code: --- start of code --- import Tkinter as Tk from Tkconstants import * root = Tk.Tk() e1 = Tk.Entry(root, text = 'Hello World') e2 = Tk.Entry(root, text = 'Hello...
1
by: C Martin | last post by:
What am I doing wrong in this code? The callback doesn't work from the Entry widget. ##start code import Tkinter tk = Tkinter.Tk() var = Tkinter.StringVar() print var._name def cb(name,...
0
by: Leonhard Vogt | last post by:
Hello I have the following problem in Python 2.5 on Windows XP. On Ubuntu I do not see the problem. I have a Tkinter application as in the following example The entry-widget is somehow...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.