2

Using python3. I'm new to tkinter writing a program with a GUI that takes some input parameters and does some data processing, so it needs entry boxes where I can type numbers in.

I cannot for the life of me figure out how to get the entry boxes to stop lagging, i.e: when a box is clicked on and typed into, the changes made to the text in that entry box don't appear until I click on another button or text box in the window. Functionally, the program still works like this but it's irritating not being able to see what I'm typing and others will eventually use this software.

I've gotten halfway to fixing the problem by putting in event bindings so that key presses trigger an update of the idle tasks. The text now updates while typing, but it's "one letter slow" (a typed letter appears only after the next letter is typed) and this is still not ideal.

Help?

MWE:

from tkinter import *


###Window: 'top'
top = Tk()                              #make window
top.geometry("670x360")                 #window size
top.wm_title("Test program for TKinter")#window title

#window and label background colour
bgcol='light sea green'
top.configure(background=bgcol)         #set colour


#events to refresh window
def keypress(event):
    print('key pressed')
    #update all idle widgets (remove text box lag)
    top.update_idletasks()

def mouseentry(event):
    print('mouse entered text box')
    #update all idle widgets (remove text box lag)
    top.update_idletasks()


##text entry box
Label(top,background=bgcol, text="").pack() #spacing label

v = StringVar()
e=Entry(top, width=50, textvariable=v)#,height=1)
e.pack()

Label(top,background=bgcol, text="").pack() #spacing label

v.set("a default value")
s = e.get()

e.bind("<Key>",keypress)
e.bind("<Enter>",mouseentry)

#Label displaying text box contents (not working right now, not important)
Label(top,background=bgcol, textvariable=s).pack() #text display label

#make buttons appear on start
top.update()
top.mainloop()

How do I update the entry widget as it is typed? Is there something really simple that I'm overlooking?

nale
  • 451
  • 1
  • 4
  • 4
  • I do not see any lag, and I don't see any reason why there should be a lag unless your "print" statement is very slow. if you replace the print statement with something else (eg: updating a counter), do you still see the lag? – Bryan Oakley Jun 29 '15 at 11:34
  • The reason you are one character behind is explained in detail in this answer: http://stackoverflow.com/a/11542200/7432 – Bryan Oakley Jun 29 '15 at 11:36
  • I was using anaconda3, but it still lags when running via the command line. – nale Jun 29 '15 at 12:00
  • When I remove update_idletasks from the events, it doesn't update at all. I don't think it's a speed issue, I think the window just refuses to refresh on its own while typing. Removing print statements makes no difference. – nale Jun 29 '15 at 12:09
  • There seems to be something very wrong with your system. The call to `update_idletasks` _should_ be completely superfluous. The behavior you describe is what would expect if there were no event loop running, though your code shows a call to `mainloop`. Since the behavior you describe is very non-standard, it seems to point to something being broken in your environment. – Bryan Oakley Jun 29 '15 at 12:37
  • I thought it was a bit odd that there was no constant window refresh like Processing has. Oh dear! Will reinstall python3 on its own and try the command line again. EDIT: yep, works with 3.4 downloaded from python.org. Must be something wrong with anaconda3 or at least my setup. – nale Jun 29 '15 at 13:11
  • When you figure out what the problem is, post an answer for posterity. :) – Ethan Furman Jun 29 '15 at 18:29

1 Answers1

0

Answer to own question:

Mainloop wasn't running in the background (or something) of my python install, for whatever reason, whether run via Anaconda or via the terminal.

Simple reinstall of Anaconda IDE with python3.4 fixed all my issues.

nale
  • 451
  • 1
  • 4
  • 4
  • The code in your question shows you explicitly calling `mainloop` so your explanation is puzzling. However, I'm glad a re-install fixed it. – Bryan Oakley Jun 30 '15 at 16:30