英文:
wxpython is there any way to change the textcolor according to if statements?
问题
你可以根据onsend
函数中输入的条件来格式化self.verify
框中的文本。如果满足第一个条件(答案为true),则将self.verify
框中的文本设为蓝色,如果满足第二个条件,则将文本设置为红色。
在代码中,你可以使用self.verify.SetForegroundColour
方法来设置文本颜色,将它放在if
语句的相应条件下,如下所示:
if self.result == self.letter:
self.verify.write(f'true {self.letter}')
self.verify.SetForegroundColour(wx.BLUE)
# 其他代码...
elif self.result != self.letter:
self.verify.write(f'no')
self.verify.SetForegroundColour(wx.RED)
# 其他代码...
这将根据条件改变self.verify
框中文本的颜色,蓝色表示答案为true,红色表示答案不正确。
英文:
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()
答案1
得分: 1
你可以通过更改文本属性wx.TextAttr
,使用文本控件的SetDefaultStyle
方法来实现这一点。
这里我已经变换了颜色选择。
import wx
class Quote(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, None, -1)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
self.text = wx.TextCtrl(panel, pos=(20,20), size=(250,220),
style=wx.TE_MULTILINE|wx.TE_READONLY)
self.text.write('"Have more than')
self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('showest,\n\nSpeak less than')
self.text.SetDefaultStyle(wx.TextAttr(wx.RED))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('knowest,\n\nLend less than')
self.text.SetDefaultStyle(wx.TextAttr('#0000ff'))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('owest,\n\nRide more than')
self.text.SetDefaultStyle(wx.TextAttr('dark green'))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('goest,\n\nLearn more than')
self.text.SetDefaultStyle(wx.TextAttr(wx.Colour(0,0,255)))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('trowest,\n\nSet less than')
self.text.SetDefaultStyle(wx.TextAttr('violet'))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('throwest."\n\n-The Fool in King Lear Act 1, Scene 4')
self.SetSize((300,300))
self.Centre()
self.Show(True)
def main():
app = wx.App()
Quote()
app.MainLoop()
if __name__ == '__main__':
main()
英文:
You can achieve this by varying the text attributes wx.TextAttr
using the textctrl's SetDefaultStyle
.
Here I've varied the colour selection.
import wx
class Quote(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, None, -1)
self.InitUI()
def InitUI(self):
panel=wx.Panel(self)
self.text=wx.TextCtrl(panel, pos=(20,20), size=(250,220),
style=wx.TE_MULTILINE|wx.TE_READONLY)
self.text.write('"Have more than')
self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('showest,\n\nSpeak less than')
self.text.SetDefaultStyle(wx.TextAttr(wx.RED))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('knowest,\n\nLend less than')
self.text.SetDefaultStyle(wx.TextAttr('#0000ff'))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('owest,\n\nRide more than')
self.text.SetDefaultStyle(wx.TextAttr('dark green'))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('goest,\n\nLearn more than')
self.text.SetDefaultStyle(wx.TextAttr(wx.Colour(0,0,255)))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('trowest,\n\nSet less than')
self.text.SetDefaultStyle(wx.TextAttr('violet'))
self.text.write(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.write('throwest."\n\n-The Fool in King Lear Act 1, Scene 4')
self.SetSize((300,300))
self.Centre()
self.Show(True)
def main():
app=wx.App()
Quote()
app.MainLoop()
if __name__ == '__main__':
main()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论