如何设置wxPython中RichTextCtrl的背景颜色

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

how to set the background color of a RichTextCtrl in wxPython

问题

  1. class ChatDisplayer(RichTextCtrl):
  2. def __init__(self, *args, **kw):
  3. super().__init__(*args, **kw)
  4. def addMessage(self, message, red, green, blue):
  5. font = Font(12, DEFAULT, NORMAL, NORMAL, False)
  6. text_attr = RichTextAttr()
  7. text_attr.SetTextColour(BLACK)
  8. text_attr.SetFont(font)
  9. bg_color = Colour(red=red, green=green, blue=blue)
  10. text_attr.SetBackgroundColour(bg_color)
  11. self.BeginStyle(text_attr)
  12. self.AppendText(message)
  13. self.EndStyle()
  14. # The main class is complex but looks like this:
  15. class ChatMessageGrid(FlexGridSizer):
  16. def __init__(self, panel):
  17. super().__init__(6, 2, 9, 25)
  18. chat_message_apikey_label = StaticText(panel, label="API key")
  19. chat_message_apikey_input = TextCtrl(panel)
  20. chat_displayer_label = StaticText(panel, label="Chat", pos=(1, 0))
  21. chat_displayer_input = ChatDisplayer(panel, ID_ANY, style=TE_MULTILINE|TE_READONLY)
  22. chat_message_system_label = StaticText(panel, label="System")
  23. chat_message_system_input = TextCtrl(panel)
  24. chat_message_label = StaticText(panel, label="your message")
  25. chat_message_text = TextCtrl(panel, style=TE_MULTILINE)
  26. chat_message_button = Button(panel, label="send")
  27. model_choice_label = StaticText(panel, label="model")
  28. model_choice_box = ComboBox(panel, value="bbb", choices=model_choices)
  29. self.AddMany(
  30. [
  31. chat_message_apikey_label,
  32. (chat_message_apikey_input, 1, EXPAND),
  33. chat_message_system_label,
  34. (chat_message_system_input, 1, EXPAND),
  35. (chat_displayer_label, 1, EXPAND),
  36. (chat_displayer_input, 1, EXPAND),
  37. (chat_message_label, EXPAND),
  38. (chat_message_text, 1, EXPAND),
  39. (model_choice_label, EXPAND),
  40. (model_choice_box, EXPAND),
  41. (chat_message_button, EXPAND),
  42. ]
  43. )
  44. self.AddGrowableRow(2, 1)
  45. self.AddGrowableRow(3, 1)
  46. self.AddGrowableCol(1, 1)
  47. chat_message_button.Bind(
  48. EVT_BUTTON,
  49. lambda event: self.onClick(
  50. event,
  51. chat_message_system_input,
  52. chat_message_apikey_input,
  53. chat_displayer_input,
  54. chat_message_text,
  55. model_choice_box,
  56. chat_message_button
  57. ),
  58. )
  59. def completeRequest(self, messages, model, apiKey, chat_displayer_input, message, button):
  60. # call the service
  61. userMessage = "User:\n"
  62. aiMessage = "Assistant:\n"
  63. # update the UI
  64. CallAfter(chat_displayer_input.addMessage, userMessage, 176, 210, 167)
  65. #CallAfter(chat_displayer_input.addMessage, aiMessage, 176, 210, 167)
  66. # re enable the button
  67. button.Enable()
  68. def onClick(
  69. self,
  70. event,
  71. chat_message_system_input: TextCtrl,
  72. chat_message_apikey_input: TextCtrl,
  73. chat_message_historic_input: ChatDisplayer,
  74. chat_message_text: TextCtrl,
  75. model_choice_box: ComboBox,
  76. button: Button
  77. ):
  78. if not len(chat_message_apikey_input.GetValue().strip()):
  79. MessageBox("API key is empty!", "Error", OK | ICON_ERROR)
  80. elif not len(chat_message_text.GetValue().strip()):
  81. MessageBox("type something please!", "Error", OK | ICON_ERROR)
  82. else:
  83. apiKey = chat_message_apikey_input.GetValue().strip()
  84. message = chat_message_text.GetValue().strip()
  85. button.Disable()
  86. # create a thread to run the API call
  87. t = threading.Thread(target=self.completeRequest, args=(messages,
  88. model_choice_box.GetValue(), apiKey, chat_message_historic_input, message,
  89. button))
  90. t.start()
英文:

I have a chatbox and I'm trying to colour every message with a different colour

  1. class ChatDisplayer(RichTextCtrl):
  2. def __init__(self, *args, **kw):
  3. super().__init__(*args, **kw)
  4. def addMessage(self, message, red, green, blue):
  5. font = Font(12, DEFAULT, NORMAL, NORMAL, False)
  6. text_attr = RichTextAttr()
  7. text_attr.SetTextColour(BLACK)
  8. text_attr.SetFont(font)
  9. bg_color = Colour(red=red, green=green, blue=blue)
  10. text_attr.SetBackgroundColour(bg_color)
  11. self.BeginStyle(text_attr)
  12. self.AppendText(message)
  13. self.EndStyle()

this does append the text but it does not change the colour of the text of the background colour.
I can find the SetBackgroundColor attribute in the source code but not in the doc.

The main class is complex but looks like this:

  1. class ChatMessageGrid(FlexGridSizer):
  2. def __init__(self, panel):
  3. super().__init__(6, 2, 9, 25)
  4. chat_message_apikey_label = StaticText(panel, label="API key")
  5. chat_message_apikey_input = TextCtrl(panel)
  6. chat_displayer_label = StaticText(panel, label="Chat", pos=(1, 0))
  7. chat_displayer_input = ChatDisplayer(panel, ID_ANY, style=TE_MULTILINE|TE_READONLY)
  8. chat_message_system_label = StaticText(panel, label="System")
  9. chat_message_system_input = TextCtrl(panel)
  10. chat_message_label = StaticText(panel, label="your message")
  11. chat_message_text = TextCtrl(panel, style=TE_MULTILINE)
  12. chat_message_button = Button(panel, label="send")
  13. model_choice_label = StaticText(panel, label="model")
  14. model_choice_box = ComboBox(panel, value="bbb", choices=model_choices)
  15. self.AddMany(
  16. [
  17. chat_message_apikey_label,
  18. (chat_message_apikey_input, 1, EXPAND),
  19. chat_message_system_label,
  20. (chat_message_system_input, 1, EXPAND),
  21. (chat_displayer_label, 1, EXPAND),
  22. (chat_displayer_input, 1, EXPAND),
  23. (chat_message_label, EXPAND),
  24. (chat_message_text, 1, EXPAND),
  25. (model_choice_label, EXPAND),
  26. (model_choice_box, EXPAND),
  27. (chat_message_button, EXPAND),
  28. ]
  29. )
  30. self.AddGrowableRow(2, 1)
  31. self.AddGrowableRow(3, 1)
  32. self.AddGrowableCol(1, 1)
  33. chat_message_button.Bind(
  34. EVT_BUTTON,
  35. lambda event: self.onClick(
  36. event,
  37. chat_message_system_input,
  38. chat_message_apikey_input,
  39. chat_displayer_input,
  40. chat_message_text,
  41. model_choice_box,
  42. chat_message_button
  43. ),
  44. )
  45. def completeRequest(self, messages, model, apiKey, chat_displayer_input, message,
  46. button):
  47. # call the service
  48. userMessage = "User:\n"
  49. aiMessage = "Assistant:\n"
  50. # update the UI
  51. CallAfter(chat_displayer_input.addMessage, userMessage, 176, 210,167)
  52. #CallAfter(chat_displayer_input.addMessage, aiMessage, 176, 210,167)
  53. # re enable the button
  54. button.Enable()
  55. def onClick(
  56. self,
  57. event,
  58. chat_message_system_input: TextCtrl,
  59. chat_message_apikey_input: TextCtrl,
  60. chat_message_historic_input: ChatDisplayer,
  61. chat_message_text: TextCtrl,
  62. model_choice_box: ComboBox,
  63. button: Button
  64. ):
  65. if not len(chat_message_apikey_input.GetValue().strip()):
  66. MessageBox("API key is empty!", "Error", OK | ICON_ERROR)
  67. elif not len(chat_message_text.GetValue().strip()):
  68. MessageBox("type something please!", "Error", OK | ICON_ERROR)
  69. else:
  70. apiKey = chat_message_apikey_input.GetValue().strip()
  71. message = chat_message_text.GetValue().strip()
  72. button.Disable()
  73. # create a thread to run the API call
  74. t = threading.Thread(target=self.completeRequest, args=(messages,
  75. model_choice_box.GetValue(), apiKey, chat_message_historic_input, message,
  76. button))
  77. t.start()

答案1

得分: 1

I assume it is because you are appending the text and it is using the attributes that are currently in force at that position.
你假设是因为你正在“添加”文本,并且它使用当前位置上生效的属性。

Your options would be to change the attributes at the current position or insert the text with a write and new attributes.
你的选择是更改当前位置的属性或使用write和新属性插入文本。

e.g.
例如。

import wx
导入 wx
import wx.richtext
导入 wx.richtext

class MainFrame(wx.Frame):
类 MainFrame(wx.Frame):

  1. def __init__(self):
  2. def __init__(self):
  3. wx.Frame.__init__(self, None, title='Test RichTextCtrl')
  4. wx.Frame.__init__(self, None, title='测试 RichTextCtrl')
  5. self.panel = wx.Panel(self)
  6. self.panel = wx.Panel(self)
  7. self.text1 = wx.richtext.RichTextCtrl(self.panel, value="Rolf is not a surfer,\nbut he likes to the beach", pos=(10,10), style = wx.TE_MULTILINE | wx.VSCROLL, size=(300, 90))
  8. self.text1 = wx.richtext.RichTextCtrl(self.panel, value="Rolf 不是冲浪者,\n但他喜欢去海滩", pos=(10,10), style = wx.TE_MULTILINE | wx.VSCROLL, size=(300, 90))
  9. self.button = wx.Button(self.panel, -1, "Insert",pos=(10,110))
  10. self.button = wx.Button(self.panel, -1, "插入",pos=(10,110))
  11. self.button.Bind(wx.EVT_BUTTON, self.On_Button)
  12. self.button.Bind(wx.EVT_BUTTON, self.On_Button)
  13. self.text1.Enable(False)
  14. self.text1.Enable(False)
  15. self.Show()
  16. self.Show()
  17. def On_Button(self, event):
  18. def On_Button(self, event):
  19. self.text1.SetInsertionPoint(34)
  20. self.text1.SetInsertionPoint(34)
  21. text_attr = wx.richtext.RichTextAttr()
  22. text_attr = wx.richtext.RichTextAttr()
  23. text_attr.SetTextColour(wx.GREEN)
  24. text_attr.SetTextColour(wx.GREEN)
  25. bg_color = wx.Colour(red=255, green=0, blue=0)
  26. bg_color = wx.Colour(red=255, green=0, blue=0)
  27. text_attr.SetBackgroundColour(bg_color)
  28. text_attr.SetBackgroundColour(bg_color)
  29. self.text1.BeginStyle(text_attr)
  30. self.text1.BeginStyle(text_attr)
  31. self.text1.WriteText(" to go")
  32. self.text1.WriteText(" 去")
  33. self.text1.EndStyle()
  34. self.text1.EndStyle()
  35. self.text1.SetInsertionPoint(-1)
  36. self.text1.SetInsertionPoint(-1)
  37. bg_color = wx.Colour(red=255, green=255, blue=0)
  38. bg_color = wx.Colour(red=255, green=255, blue=0)
  39. text_attr.SetBackgroundColour(bg_color)
  40. text_attr.SetBackgroundColour(bg_color)
  41. text_attr.SetTextColour(wx.RED)
  42. text_attr.SetTextColour(wx.RED)
  43. self.text1.BeginStyle(text_attr)
  44. self.text1.BeginStyle(text_attr)
  45. self.text1.WriteText(" on hot days")
  46. self.text1.WriteText(" 在炎热的天气上")
  47. self.text1.EndStyle()
  48. self.text1.EndStyle()

if name == 'main':
如果 name == 'main':
app = wx.App()
app = wx.App()
frame = MainFrame()
frame = MainFrame()
app.MainLoop()
app.MainLoop()

英文:

I assume it is because you are appending the text and it is using the attributes that are currently in force at that position.
Your options would be to change the attributes at the current position or insert the text with a write and new attributes.
e.g.

  1. import wx
  2. import wx.richtext
  3. class MainFrame(wx.Frame):
  4. def __init__(self):
  5. wx.Frame.__init__(self, None, title='Test RichTextCtrl')
  6. self.panel = wx.Panel(self)
  7. self.text1 = wx.richtext.RichTextCtrl(self.panel, value="Rolf is not a surfer,\nbut he likes to the beach", pos=(10,10), style = wx.TE_MULTILINE | wx.VSCROLL, size=(300, 90))
  8. self.button = wx.Button(self.panel, -1, "Insert",pos=(10,110))
  9. self.button.Bind(wx.EVT_BUTTON, self.On_Button)
  10. self.text1.Enable(False)
  11. self.Show()
  12. def On_Button(self, event):
  13. self.text1.SetInsertionPoint(34)
  14. text_attr = wx.richtext.RichTextAttr()
  15. text_attr.SetTextColour(wx.GREEN)
  16. bg_color = wx.Colour(red=255, green=0, blue=0)
  17. text_attr.SetBackgroundColour(bg_color)
  18. self.text1.BeginStyle(text_attr)
  19. self.text1.WriteText(" to go")
  20. self.text1.EndStyle()
  21. self.text1.SetInsertionPoint(-1)
  22. bg_color = wx.Colour(red=255, green=255, blue=0)
  23. text_attr.SetBackgroundColour(bg_color)
  24. text_attr.SetTextColour(wx.RED)
  25. self.text1.BeginStyle(text_attr)
  26. self.text1.WriteText(" on hot days")
  27. self.text1.EndStyle()
  28. if __name__ == '__main__':
  29. app = wx.App()
  30. frame = MainFrame()
  31. app.MainLoop()

huangapple
  • 本文由 发表于 2023年4月10日 22:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978062.html
匿名

发表评论

匿名网友

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

确定