Why does tkinter Entry.xview_moveto fail?

I am working with tkinter on Python 3.4.1 (Windows 7), and I found that the ttk Entry.xview_moveto function does not work correctly. Here is a 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 all the way to the left, but that is not the case. However, I noticed that if I use

 b.after(500, b.xview_moveto, 1) 

It works great. Why should I delay a function call so that it works correctly? Am I doing something wrong?

Update . In addition to the fhdrsdg solution, I found that the Entry.after_idle method works for my program. This doesn't seem to work for the above simple example, but if someone else has the same problem as me, it might be a different, cleaner solution.

+5
source share
1 answer

This problem also exists in tcl / Tk .
The answer suggested there is to bind to the Entry <Expose> event and then disconnect. I tried to rewrite this in 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) 

update_idletasks seems to be when using tcl version lower than 8.5.5 .

+2
source

Source: https://habr.com/ru/post/1216426/


All Articles