找到你要的答案

Q:How to get current cursor position for Text widget

Q:如何获取文本部件的当前光标位置

I am using an onscreen keyboard to type data for a tkinter based gui.
I was able to use the entry field to enter, edit data through the onscreen keyboard such as getting current cursor position and length of the string.

temp = self.entry_label.get()
length_string=len(temp)
cursor_position = self.entry_label.index(INSERT)

But I wish to do the same for a Text widget. I could get the text for Text widget using get() method and its length but cannot get the current mouse cursor position.

temp=self.new_text.get(1.0, END)
cursor_position = self.new_text.index(INSERT)

Acutally it works and i am able to add character to that poisition , but after adding character cursor goes back to origional position , i.e last character

I am using an onscreen keyboard to type data for a tkinter based gui.
I was able to use the entry field to enter, edit data through the onscreen keyboard such as getting current cursor position and length of the string.

temp = self.entry_label.get()
length_string=len(temp)
cursor_position = self.entry_label.index(INSERT)

但我希望做同样的文本小部件。我能得到的文本的文本控件使用方法和它的长度get()但不能获取当前鼠标光标位置。

temp=self.new_text.get(1.0, END)
cursor_position = self.new_text.index(INSERT)

其实它和我可以添加字符,位置,但在添加字符光标回到原位置,即最后一个字符

answer1: 回答1:

maybe This works? Else maybe text_widget.index(Tkinter.INSERT) is what should work.

也许这个作品?别人也许text_widget。指数(Tkinter。插入)是怎么工作的。

python  python-2.7  tkinter