Help needed with the Tkinter Entry widget

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Elias Alhanatis
    New Member
    • Aug 2007
    • 56

    Help needed with the Tkinter Entry widget

    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:
    Code:
    from Tkinter import *
    
    def fetch():
        q=(e.get())
        print q
        
    root=Tk()
    f=Frame(root)
    f.pack()
    e=Entry(f,width=26)
    e.pack()
    e.focus()
    
    e.bind("<Any-KeyPress>",(lambda event: fetch()))
    
    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!!!!
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by 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:
    Code:
    from Tkinter import *
    
    def fetch():
        q=(e.get())
        print q
        
    root=Tk()
    f=Frame(root)
    f.pack()
    e=Entry(f,width=26)
    e.pack()
    e.focus()
    
    e.bind("<Any-KeyPress>",(lambda event: fetch()))
    
    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:
    [code=python]
    def fetch(event):
    print event.char + e.get()


    e.bind("<Any-KeyPress>", fetch)
    [/code]
    If that doesn't work, try printing another member of the event object.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by ilikepython
      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:
      [code=python]
      def fetch(event):
      print event.char + e.get()


      e.bind("<Any-KeyPress>", fetch)
      [/code]
      If that doesn't work, try printing another member of the event object.
      I like it!
      It may be the other way 'round, though:
      [code=python]
      def fetch(event):
      print e.get() + event.char
      # and maybe like this is more general
      # print event.widget.ge t() + event.char

      e.bind("<Any-KeyPress>", fetch)
      [/code]

      Comment

      • Elias Alhanatis
        New Member
        • Aug 2007
        • 56

        #4
        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!!!!

        Comment

        Working...