<p>我的第一个反应是调用<code>txt.Refresh()</code>,因为我有过类似的使用wx.TextCtrl的经验,其中背景颜色没有更新,<code>Refresh()</code>调用强制重画。然而,这种做法似乎是不正确的。</p>
<p>在检查了<a href="http://wxpython.org/docs/api/wx.stc.StyledTextCtrl-class.html" rel="nofollow">StyledTextCtrl API</a>之后,似乎<code>SetBackground()</code>不是您想要的函数。我的理解是,由于STC可以在同一个框中有多个样式,因此单个文本样式优先于STC的设置。</p>
<p>经过一些研究,我发现了<a href="http://wxpython.org/docs/api/wx.stc.StyledTextCtrl-class.html#StyleSetBackground" rel="nofollow">StyleSetBackground()</a>函数。这将修改默认样式,使背景变为红色,从而有效地将背景设置为红色。你需要这样称呼它:</p>
<pre><code>txt.StyleSetBackground(wx.stc.STC_STYLE_DEFAULT, (255,0,0))
</code></pre>
<p>请记住,如果使用多个样式,则可能需要为每个样式调用此方法。</p>
<p>---编辑---<br/>
我忘了输入一些文本来检查我的代码。结果是,如果您所做的只是调用<code>SyleSetBackground()</code>,就像我上面建议的那样,当您输入文本时,输入文本的背景仍然是白色,而不是预期的红色。</p>
<p>再多研究一下,我发现在设置背景颜色后,通过调用以下代码可以很容易地解决这个问题:</p>
<pre><code>txt.StyleClearAll()
</code></pre>
<p>我的猜测是,当您创建StyledTextCtrl时,它会将文本样式设置为<code>wx.stc.STC_STYLE_DEFAULT</code>。到现在为止,一直都还不错。但是,在这一点之后,我们更改了默认样式的值(通过使背景变红),所以现在文本样式和默认样式是不同的。因此,我们需要调用<a href="http://wxpython.org/docs/api/wx.stc.StyledTextCtrl-class.html#StyleClearAll" rel="nofollow">StyleClearAll()</a>来将所有样式重置回默认样式(它现在有一个红色背景)。</p>