Python textctrl setstyle 无法正确读取插入点
我在我编写的 GUI 中使用 python textctrl 。当我阅读了几个文件后,我将文件的内容发布到textctrl中。由于它们的格式相似,我尝试对前几个字符进行着色以标记每个文件输出的开始。我所做的如下:
for file in self.dir:
f = open(file, 'r')
strInfo = f.read()
if self.dir.index(file) == 0:
self.textctrl.SetValue(strInfo)
self.textctrl.SetStyle(self.textctrl.GetInsertionPoint(),
self.textctrl.GetInsertionPoint()+22, wx.TextAttr("RED", "YELLOW"))
else:
self.textctrl.AppendText(strInfo)
self.textctrl.SetStyle(self.textctrl.GetInsertionPoint(),
self.textctrl.GetInsertionPoint()+22, wx.TextAttr("RED", "YELLOW"))
f.close()
基本上,此代码应该为每个文件输出的前 22 个字符着色。
但它并不像我预期的那样工作。我测试了 self.dir 列表中的 3 个文件。它为第一个文件输出的前 22 个字符着色。然后,对于附加在后面的其他两个文件输出,它对第三个文件的整个部分进行着色,但不对第二个文件输出进行着色。
我打印了 GetInsertionPoint()
进行调试。这是正确的。我不知道出了什么问题。有什么建议需要帮助吗?
I am using python textctrl in a GUI I wrote. After I read several files, I post the contents of the file in textctrl. Since they are all in similar format, I tried to color the first several charactors to mark out the start of each file output. What I did is the following:
for file in self.dir:
f = open(file, 'r')
strInfo = f.read()
if self.dir.index(file) == 0:
self.textctrl.SetValue(strInfo)
self.textctrl.SetStyle(self.textctrl.GetInsertionPoint(),
self.textctrl.GetInsertionPoint()+22, wx.TextAttr("RED", "YELLOW"))
else:
self.textctrl.AppendText(strInfo)
self.textctrl.SetStyle(self.textctrl.GetInsertionPoint(),
self.textctrl.GetInsertionPoint()+22, wx.TextAttr("RED", "YELLOW"))
f.close()
Basically, this code should color the first 22 characters for each file output.
But it does not work like I expected. I tested on 3 files in the self.dir list. It colors the first 22 characters of the first file output. Then for the other two file outputs, which are appended after, it colors the whole part of the third file but do not colors at all the second file output.
I printed out the GetInsertionPoint()
to debug. It is correct. I don't know what is wrong. Any suggestions for help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
固定的。
在 GetInsertionPoint() 的 AppendText 之前命名另一个变量。否则,它将到达文件末尾。
Fixed.
Name another variable before AppendText for the GetInsertionPoint(). Otherwise, it will get the end of the file.