简体   繁体   English

函数未在Tkinter文本小部件中突出显示模式

[英]Function doesn't highlight a pattern in tkinter text widget

i wrote small function to highlight search pattern in text widget and assigned it to a button "Find". 我写了一个小功能来突出显示文本小部件中的搜索模式,并将其分配给按钮“查找”。 But turned out that it doesn't highlight needed pattern in text widget and just stucks. 但是事实证明,它不会在文本小部件中突出显示所需的模式,而只是突出显示。

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

Who can tell me what am i doing wrong? 谁能告诉我我在做什么错? xml is a text widget, fi is a entry widget, so a pattern is usually what user puts in fi widget. xml是文本小部件, fi是条目小部件,因此模式通常是用户在fi小部件中输入的内容。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

Every time you search, you search from "1.0" to the end of the document. 每次搜索时,都从“ 1.0”搜索到文档末尾。 If what you're searching for is in the document even once, this while loop will never end because index will never be an empty string. 如果您要搜索的内容甚至在文档中一次,那么while循环将永远不会结束,因为index永远不会是一个空字符串。

The solution is to do start="1.0" before the loop, and then modify your search to start at start since you're updating this variable at the end of your loop. 解决方案是在循环之前执行start="1.0" ,然后将搜索修改为从start因为您要在循环结束时更新此变量。

start = "1.0"
while True:
    index = xml.search(fi.get(), start, END) 

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

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