开发者

Python textctrl setstyle cannot read the insertionpoint correctly

开发者 https://www.devze.com 2023-03-13 04:40 出处:网络
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 charactor

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 e开发者_Go百科ach 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?


Fixed. Name another variable before AppendText for the GetInsertionPoint(). Otherwise, it will get the end of the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消