Sie müssen wahrscheinlich auf lesen RichTextCtrl
https://wxpython.org/Phoenix/docs/html/richtextctrl_overview.html
Nur TextCtrl
verwenden, können Sie
Set-Stil nach der Tat Attribute (Danke Robin für deinen Kommentar) oder
wenden Sie Stil-Attribute auf den Text, wie Sie gehen.
Rahmen Stilattribute nach der Tat re
mit allen Vorkommen des Wortes finden vorher:
import wx
import re
string='''"Have more than thou showest,
Speak less than thou knowest,
Lend less than thou owest,
Ride more than thou goest,
Learn more than thou trowest,
Set less than thou throwest."
-The Fool in King Lear'''
class Quote(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, None, -1)
self.InitUI()
def InitUI(self):
panel=wx.Panel(self)
word = 'thou'
word_colour = wx.TextAttr(wx.BLUE)
word_occurs = self.find_str(word,string)
self.text=wx.TextCtrl(panel, pos=(20,20), size=(250,220),
style=wx.TE_MULTILINE|wx.TE_READONLY)
self.text.AppendText(string)
for i in word_occurs:
#SetStyle(start pos, end pos, style)
self.text.SetStyle(i,i+len(word),word_colour)
self.SetSize((300,300))
self.Centre()
self.Show(True)
def find_str(self,sub,sent): #return positions of the word
return [x.start() for x in re.finditer(sub,sent)]
def main():
app=wx.App()
Quote()
app.MainLoop()
if __name__ == '__main__':
main()
Die lange umständliche Art und Weise, dies zu tun, Stil anwenden Attribute, wie Sie gehen:
import wx
string1='''"Have more than'''
string2='''showest,
Speak less than'''
string3='''knowest,
Lend less than'''
string4='''owest,
Ride more than'''
string5='''goest,
Learn more than'''
string6='''trowest,
Set less than'''
string7='''throwest."
-The Fool in King Lear'''
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.AppendText(string1)
self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
self.text.AppendText(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.AppendText(string2)
self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
self.text.AppendText(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.AppendText(string3)
self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
self.text.AppendText(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.AppendText(string4)
self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
self.text.AppendText(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.AppendText(string5)
self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
self.text.AppendText(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.AppendText(string6)
self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
self.text.AppendText(" thou ")
self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
self.text.AppendText(string7)
self.SetSize((300,300))
self.Centre()
self.Show(True)
def main():
app=wx.App()
Quote()
app.MainLoop()
if __name__ == '__main__':
main()
Sie können auch 'SetStyle' verwenden, um den Stil für einen vorhandenen Textbereich zu ändern. Sie können also die ganze Zeichenfolge in einem Schritt in das Steuerelement einfügen und dann die Stile an den entsprechenden Start-/Endpositionen festlegen. – RobinDunn
@RobinDunn Ich lerne mehr Antworten auf Fragen als je, indem ich einen frage! :) –
Danke, Rolf von Sachsen. Ihre Antwort ist sehr nützlich. –