Is there any way to change the textcolor according to if statements?

hi and welcome.
How can I format the text in the self.verify box according to the conditions entered in the onsend function where if the first condition is met (the answer is true) the text in the self.verify box is written in blue, and if the second condition is met the text is written in red? Are there additional values ​​in self.verify.write that can be used for this purpose or what?
Code:

import wx
import winsound

enletters= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

class lten(wx.Frame):
    def __init__(self, parent, school):
        super(lten,self).__init__(parent,-1, title= 'Education system, english letters')
        self.p = wx.Panel(self,-1)
        self.Center()
        self.lettern= (0)
        self.letter= (enletters[self.lettern])
        wx.StaticText(self.p, -1, "answers: ")
        self.verify = wx.TextCtrl(self.p, -1, style=wx.TE_READONLY+wx.TE_MULTILINE+wx.HSCROLL)

        self.wt= wx.StaticText(self.p, -1, f"type  {self.letter}  ")
        self.write = wx.TextCtrl(self.p, -1)
        self.write.SetFocus()

        self.send= wx.Button(self.p, -1, "Submit the answer")
        self.send.Bind(wx.EVT_BUTTON, self.onsend)

        self.Show()

    def onsend(self, event):
        self.result= self.write.GetValue()
        if self.result == self.letter:
            self.verify.write(f'''true {self.letter}
''')
            winsound.PlaySound('data\\yes.wav', 1)
            if self.lettern == 25:
                self.lettern= (0)
            else:
                self.lettern= self.lettern+1

            self.letter= (enletters[self.lettern])
            self.wt.SetLabel(f"type {self.letter} ")
            self.write.SetFocus()
            self.write.SetValue("")

        elif self.result != self.letter:
            self.verify.write(f'''no
''')
            winsound.PlaySound('data\\no.wav', 1)
            self.write.SetFocus()
            self.write.SetValue("")

app = wx.App()
lten(None, None)
app.MainLoop()

Hi,

Call the TextCtrl’s SetDefaultStyle() method before calling write().

You need to pass it a wx.TextAttr object which specifies the attributes to be set.

Example:

self.verify.SetDefaultStyle(wx.TextAttr(wx.RED, wx.YELLOW))

The first parameter is the text colour and the second parameter is the background colour. If you don’t want to change the background colour, you can just omit it.

When I tried to run your code on my linux PC all the controls appeared on top of each other, so I had to rewrite it using sizers, therefore the layout may not be the same as what you intended. (I also had to comment out the winsound references).

Screenshot at 2023-02-15 19-02-10

import wx
# import winsound

enletters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

class lten(wx.Frame):
    def __init__(self, parent, school):
        super(lten,self).__init__(parent,-1, title= 'Education system, english letters')
        self.p = wx.Panel(self,-1)
        self.Center()
        self.lettern = 0
        self.letter = enletters[self.lettern]

        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        label_1 = wx.StaticText(self.p, -1, "answers: ")
        sizer_2.Add(label_1)
        self.verify = wx.TextCtrl(self.p, -1, style=wx.TE_READONLY|wx.TE_MULTILINE|wx.HSCROLL)
        sizer_2.Add(self.verify, 1, wx.EXPAND)
        sizer_1.Add(sizer_2, 1, wx.EXPAND)

        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        self.wt = wx.StaticText(self.p, -1, f"type  {self.letter}  ")
        sizer_3.Add(self.wt)
        self.write = wx.TextCtrl(self.p, -1)
        self.write.SetFocus()
        sizer_3.Add(self.write)
        sizer_1.Add(sizer_3)

        self.send = wx.Button(self.p, -1, "Submit the answer")
        sizer_1.Add(self.send)
        self.p.SetSizer(sizer_1)

        self.send.Bind(wx.EVT_BUTTON, self.onsend)
        self.Layout()
        self.Show()

    def onsend(self, event):
        self.result = self.write.GetValue()
        if self.result == self.letter:
            self.verify.SetDefaultStyle(wx.TextAttr(wx.BLUE, wx.WHITE))
            self.verify.write(f'true {self.letter}\n')
            # winsound.PlaySound('data\\yes.wav', 1)
            if self.lettern == 25:
                self.lettern = 0
            else:
                self.lettern += 1

            self.letter = (enletters[self.lettern])
            self.wt.SetLabel(f"type {self.letter} ")
            self.write.SetFocus()
            self.write.SetValue("")

        elif self.result != self.letter:
            self.verify.SetDefaultStyle(wx.TextAttr(wx.RED, wx.YELLOW))
            self.verify.write(f'no\n')
            # winsound.PlaySound('data\\no.wav', 1)
            self.write.SetFocus()
            self.write.SetValue("")

app = wx.App()
lten(None, None)
app.MainLoop()
1 Like

thank you.
have a great day.