Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does tkinter's Entry.xview_moveto fail?

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.

like image 769
Sidnoea Avatar asked Nov 01 '22 07:11

Sidnoea


1 Answers

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.

like image 128
fhdrsdg Avatar answered Nov 15 '22 03:11

fhdrsdg