wxPython SetStyle不工作

2024-05-06 07:06:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用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)))

我已经测试了这段代码,并确保我最远的位置不是在TextCtrl的末尾(它每次都在预期的位置)。但由于某些原因,textcrl框中的所有文本都是灰色背景。在


Tags: the代码in文本selfispositionall
1条回答
网友
1楼 · 发布于 2024-05-06 07:06:23

来自wxpython2.8文档。最后一段解释了您的问题所在:

““ **wxTextCtrl::GetRange

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

返回包含文本的字符串,该文本在控件中的位置从开始到结束。位置必须已由另一个wxTextCtrl方法返回。在

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

相关问题 更多 >