Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在wx.TextCtrl.GetValue()上循环时,保持跟踪单词的位置,以启用对重复单词的处理_Python_Wxpython - Fatal编程技术网

Python 在wx.TextCtrl.GetValue()上循环时,保持跟踪单词的位置,以启用对重复单词的处理

Python 在wx.TextCtrl.GetValue()上循环时,保持跟踪单词的位置,以启用对重复单词的处理,python,wxpython,Python,Wxpython,我曾尝试在wxPythonGUI中制作拼写检查器,但在重复拼写单词时遇到了问题。函数需要将单词设置为红色,然后在MessageDialog中建议更正。关于MessageDialog上的更正建议,这一切都很好,但我无法改变重复拼写错误单词的颜色。 我知道问题是当我得到单词的起始位置时。。。它一直在考虑这个词的第一次出现,而忽略了其他词 for i in range(self.tx_input.GetNumberOfLines()): line = self.tx_input

我曾尝试在wxPythonGUI中制作拼写检查器,但在重复拼写单词时遇到了问题。函数需要将单词设置为红色,然后在MessageDialog中建议更正。关于MessageDialog上的更正建议,这一切都很好,但我无法改变重复拼写错误单词的颜色。 我知道问题是当我得到单词的起始位置时。。。它一直在考虑这个词的第一次出现,而忽略了其他词

for i in range(self.tx_input.GetNumberOfLines()):
            line = self.tx_input.GetLineText(i)
            for word in text:
                if word in line and word not in suggestion:
                    startPos = self.tx_input.GetValue().find(word)
                    endPos = startPos + len(word)
                    self.tx_input.SetStyle(startPos, endPos, wx.TextAttr("red", "white")) 
因为我在一行一行地循环,我会理解如果拼写错误的单词出现在同一行,但最奇怪的是,当重复出现在另一行时,它也会失败

我需要帮助找出如何跟踪word的位置,这些位置已被处理,最终在新出现时被忽略


完整代码
类添加问题(wx.Frame):
定义初始化(自身,父级):
wx.Frame.\uuuuu init\uuuuuuu(self,parent,id=wx.id\u ANY,title='Grammar Checker',pos=wx.DefaultPosition,size=wx.size(350350),style=wx.DEFAULT\u Frame\u style)
自缩背景颜色(wx颜色(0,93,126))
面板=wx.面板(自)
mainBox=wx.BoxSizer(wx.VERTICAL)
面板设置器(主机箱)
self.tx_input=wx.TextCtrl(面板,wx.ID_ANY,wx.EmptyString,wx.DefaultPosition,wx.Size(350150),wx.TE_多行| wx.TE_RICH2)
mainBox.Add(self.tx_输入,1,wx.ALL | wx.EXPAND,5)
btnBox=wx.BoxSizer(wx.VERTICAL)
self.btn=wx.按钮(面板,wx.ID_ANY,u'Grammar Check',wx.DefaultPosition,wx.DefaultSize,0)
添加(self.btn,0,wx.ALL,5)
主框。添加(btnBox,0,wx.ALL | wx.ALIGN_CENTER,5)
self.btn.Bind(wx.EVT_按钮,self.grammarchek)
def warn(自我、父项、消息、标题='WARNING!'):
dlg=wx.MessageDialog(父项、消息、标题、wx.OK | wx.ICON_警告)
dlg.ShowModal()
dlg.Destroy()
def GRAMARCHECK(自我、事件):
从symspellpy导入SymSpell
符号拼写=符号拼写()
sym_拼写.load_字典(“frequency_dictionary_en_82_765.txt”,0,1)
input\u term=self.tx\u input.GetValue().lower()
#过滤非必需字符
忽略=r'!"#$%&\'()*+,-./:;?@[\\]^_`{|}~1234567890“”–'
text=sym\u拼写.word\u分段(输入\u术语.translate(str.maketrans(“,”,忽略)),最大\u编辑\u距离=2)。分段的\u字符串.split()
suggestion=sym\u拼写.单词切分(input\u term.translate(str.maketrans(“,”,ignore)),max\u edit\u distance=2)。已更正\u string.split()
对于范围内的i(self.tx\u input.GetNumberOfLines()):
line=self.tx\u input.GetLineText(i)
对于文本中的单词:
如果单词在行中,单词不在建议中:
startPos=self.tx\u input.GetValue().find(word)
endPos=startPos+len(字)
self.tx_input.SetStyle(startPos、endPos、wx.texttr(“红色”、“白色”))
对于范围内的i(len(text)):
如果文本[i]!=建议[i]:
self.warn(self,“`+text[i]+”`+”你是说“+”`+suggestion[i]+”?)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app=wx.app(假)
框架=添加问题(无)
frame.Show()
app.MainLoop()

您总是使用
string.find(word)

查找类似“python查找子字符串的所有匹配项”的内容,我发现:

# using list comprehension + startswith() 
# All occurrences of substring in string  
res = [i for i in range(len(test_str)) if test_str.startswith(test_sub, i)] 

两者都返回测试字符串中的起始位置列表,并且可以根据您的代码进行调整

# using list comprehension + startswith() 
# All occurrences of substring in string  
res = [i for i in range(len(test_str)) if test_str.startswith(test_sub, i)] 
import re

# using re.finditer() 
# All occurrences of substring in string  
res = [i.start() for i in re.finditer(test_sub, test_str)]