简体   繁体   English

移动到文本小部件tkinter中的特定位置

[英]Moving to a specific position in a text widget tkinter

i wanted to know if it's possible to move to a specific position in a text widget in tkinter. 我想知道是否可以在tkinter的文本小部件中移动到特定位置。 I implemented search in text widget and wanted to implement "next" and "previous" methods for my search to move across the text widget. 我在文本小部件中实现了搜索,并希望实现“下一个”和“上一个”方法,以使我的搜索跨文本小部件进行。 Is it possible to do? 有可能吗? Thanks for any help. 谢谢你的帮助。

Here is a code for my method: 这是我的方法的代码:

def find():
    xml.tag_delete("search")
    xml.tag_configure("search", background="green")
    start="1.0"
    if len(fi.get()) > 0:
        xml.mark_set("insert", xml.search(fi.get(), start))
        while True:
            pos = xml.search(fi.get(), start, END) 
            if pos == "": 
                break       
            start = pos + "+%dc" % len(fi.get()) 
            xml.tag_add("search", pos, "%s + %dc" % (pos,len(fi.get())))

fi is an entry field for search pattern. fi是搜索模式的输入字段。 xml is a text field. xml是一个文本字段。

The text widget has marks , which are in effect named positions. 文本小部件具有标记 ,实际上是命名的位置。 The insertion cursor is defined by the mark "insert" (and the tkinter constant INSERT ). 插入光标由标记"insert" (和tkinter常量INSERT )定义。 You can move this index using the mark_set method. 您可以使用mark_set方法移动此索引。

For example, this moves the insertion cursor to line 3, character 14: 例如,这会将插入光标移动到第3行,字符14:

the_widget.mark_set("insert", "3.14")

If you want to make sure the new insert position is visible you can use the see method, which will scroll the widget enough for the given index to be visible: 如果要确保新插入位置可见,则可以使用see方法,该方法将滚动小部件以使给定索引可见:

the_widget.see("insert")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM