I'm working with tkinter on Python 3.4.1 (Windows 7), and I'm finding that ttk's Entry.xview_moveto function is not working properly. Here is some simple code:
from tkinter import *
from tkinter import ttk
a = Tk()
text = StringVar(a, value='qwertyuiopasdfghjklzxcvbnm1234567890')
b = ttk.Entry(a, textvariable=text)
b.grid()
b.xview_moveto(1)
The xview_moveto function should scroll the text all the way to the left, but it doesn't. However, I noticed that if I use
b.after(500, b.xview_moveto, 1)
it works just fine. Why do I have to delay the function call in order for it to work properly? Am I doing something wrong?
Update: In addition to fhdrsdg's solution, I have found that the Entry.after_idle
method works for my program. It does not seem to work for the above simple example, but if anybody else has the same problem as me, this could be another, cleaner-looking solution.
This problem also exists in tcl/Tk.
The answer proposed there is to bind to the Entry's <Expose>
event and unbind that afterwards. I've tried to rewrite that to Python / tkinter:
def xview_event_handler(e):
e.widget.update_idletasks()
e.widget.xview_moveto(1)
e.widget.unbind('<Expose>')
a = Tk()
text = StringVar(a, value='qwertyuiopasdfghjklzxcvbnm1234567890')
b = ttk.Entry(a, textvariable=text)
b.grid()
b.bind('<Expose>', xview_event_handler)
The update_idletasks
seems to be needed when using a tcl version below 8.5.5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With