wxPython是否有根据条件语句更改文本颜色的方法?

huangapple go评论68阅读模式
英文:

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()

wxPython是否有根据条件语句更改文本颜色的方法?

英文:

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()

wxPython是否有根据条件语句更改文本颜色的方法?

huangapple
  • 本文由 发表于 2023年2月16日 01:57:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定