0

我在我的代码中使用 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 框中的所有文本都变成了灰色背景。

4

1 回答 1

0

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

" **wxTextCtrl::GetRange

virtual wxString GetRange(long from, long to) const**

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

请注意,多行 wxTextCtrl 中的位置与 GetValue 返回的字符串中的索引不对应,因为换行表示不同(CR 或 CR LF),因此应使用此方法获得正确的结果,而不是提取部分的整个价值。它也可能更有效,尤其是在控件包含大量数据的情况下。”

于 2014-03-25T03:56:45.337 回答