2014-03-25 50 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),所以這種方法應該用來獲得正確的結果,而不是提取整個值的一部分。它也可能更高效,特別是如果控件包含大量數據。「