wxPython SetStyle不起作用

时间:2014-03-25 01:48:40

标签: python user-interface wxpython wx.textctrl

我在我的代码中使用wx.TextCtrl.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框中的所有文本都是灰色背景。

1 个答案:

答案 0 :(得分:0)

来自wxPython 2.8文档。最后一段解释了您的问题所在:

" ** wxTextCtrl :: GetRange

虚拟wxString GetRange(long from,long to)const **

返回包含从控件中的from和up位置开始的文本的字符串。这些位置必须由另一个wxTextCtrl方法返回。

请注意,多行wxTextCtrl中的位置与GetValue返回的字符串中的索引不对应,因为新的行表示形式(CR或CR LF)不同,因此应该使用此方法来获取正确的结果提取整个值的一部分。它也可能更有效,特别是如果控件包含大量数据。"