2014-03-25 51 views
0

我在我的代码中使用了wx.TextCtrl.SetStyle(),但它改变了所有文本的样式!wxPython SetStyle不工作

这里是我的代码:

# Get all the words in my TextCtrl 
words = self.GetValue().split(" ") 
# Find out what the farthest uneditable word is. 
farthest_uneditable = (len(words) // length_constants["words_per_block"]) * length_constants["words_per_block"] 
# Use this word knowledge to calculate the actual farthest uneditable character is 
farthest_position = 0 
for word in range(farthest_uneditable): 
    farthest_position += len(words[word]) + 1 
# Make all the uneditable text (everything from the beginning to farthest_uneditable) have a grey background 
self.SetStyle(0, farthest_position, wx.TextAttr(wx.NullColour, (84, 84, 84))) 

我已经测试此代码,并确保我farthest_position是不是在我的TextCtrl结束(它已经在每一次的预期位置)。出于某种原因,我的TextCtrl框中的所有文本都变成灰色背景。

回答

0

从wxPython 2.8文档。最后一段说明了在哪里你的问题是:

“ ** wxTextCtrl :: GetRange

虚拟wxString GetRange(从长,长到)const的**

返回一个包含启动文本字符串位置必须由另一个wxTextCtrl方法返回

请注意,多行wxTextCtrl中的位置与GetValue返回的字符串中的位置不对应,因为不同的新行表示(CR或C R LF),所以这种方法应该用来获得正确的结果,而不是提取整个值的一部分。它也可能更高效,特别是如果控件包含大量数据。“