'Setting tkinter Text yview / Text view area

I would like to set the tkinter Text box's view area / yview to a specific area. How do I do this? I know that one can retrieve the current yview but have been unsuccessful in setting it to a desired value. I know that it can be set to the desired value because I am getting the yview, destroying the Text box, and then recreating the text box. From there I would like to set the view area to what it was before but am having trouble. What should I do?

Example yviews:

(0.0, 1.0)
(0.07407407407407407, 1.0)

Code:

#Define the Text widget
t = Text(window)
t.pack()
for i in range(100):
    t.insert(END, "{}\n".format(i))
def get_y():
    global y
    y = t.yview()
b = Button(window, text="get y", command=get_y)
b.pack()
b1 = Button(window, text="set y", command=lambda: t.yview(MOVETO, y[0]/y[1]))
b1.pack()

The code above gets the user very close but it always goes a little up or down and isn't quite right. What should I do to fix it? Can this be done similarly with xview?



Solution 1:[1]

I figured it out. We can get the yview with t.yview() but we can't just insert a yview value and expect it to use it. Instead, we must use t.yview(MOVETO, yview[0]). This gives it a value it can work with and goes to about the correct position

NOTE: The same can be done with xview but use xview[1] instead

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1