473,804 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help needed with the Tkinter Entry widget

Elias Alhanatis
56 New Member
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 1912
ilikepython
844 Recognized Expert Contributor
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 Recognized Expert Expert
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
56 New Member
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
4940
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 happens on loss of focus, not when pressing Esc. 2) TABbing between multiple entry fields does undesired things with the selection, and with cursor placement. Can anyone offer any suggestions for how to fix this? I'm attemting to
6
6089
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 somebody clue me in regarding how this is done? Muchas gracias. Peace
1
2255
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: I have script started from W2K console that normally prints ascii messages to the screen. However, I have command line "debug" -flag that might cause printing
6
10320
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 convert the name to a reference using nametowidget(). self.nametowidget(event.widget.winfo_parent()).hasChanged= True It works, but I can't help but think I'm missing a more direct route. Am I? Bill
3
2151
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 the grid layout. If I make and grid the Label *before* the Entry then the Entry widget doesn't seem to work - it lets me put the cursor in it, but I can't type! See example code below. Is this just me doing something really really silly, or is...
6
1795
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 = Tkinter.Frame(root); Frame.pack (side='left')
8
2030
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 World')
1
9517
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, index, mode):
0
1516
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 blocked (i cannot type characters into it) when I call askopenfilename before I create the widget. Calling askopenfile again (by clicking the button) releases the block, I can type into the entry as expected.
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.