在文本小部件中移动插入位置

时间:2013-02-27 17:04:56

标签: python-3.x tkinter

似乎声明:MyText.mark_set(INSERT, 'new index')修改Text .insert()方法的插入位置,但不修改键盘上键入的文本的插入位置。

换句话说,有没有办法使用.insert()方法插入等效的CTRL-END键

from tkinter import *

def curseur(bidon):
    mytext.mark_set(INSERT, END)

root = Tk()
mytext = Text(root)
mytext.pack()
mytext.insert(INSERT, "Clic in other window, then clic back here (line one)\n")
mytext.insert(INSERT, "Type something on the keyboard\n")
mytext.insert(INSERT, "The typed text must go to the end of the widget\n")
mytext.bind("<FocusIn>", curseur)
root.mainloop()

谢谢,

1 个答案:

答案 0 :(得分:1)

这确实会移动Text.insert()方法的插入位置键入键入的文本的插入位置。事实证明你需要一点延迟,以便点击文本小部件的正常效果不会覆盖我们重新定位光标:)

from tkinter import *

def curseur(bidon):
    root.after(50, lambda: mytext.mark_set(INSERT, END))

root = Tk()
mytext = Text(root)
mytext.pack()
mytext.insert(INSERT, "Clic in other window, then clic back here (line one)\n")
mytext.insert(INSERT, "Type something on the keyboard\n")
mytext.insert(INSERT, "The typed text must go to the end of the widget\n")
mytext.bind("<FocusIn>", curseur)
root.mainloop()