如何解决wxpython textctrl style 动态文字颜色
我正在尝试用wxpython(textCtrl)做一个高亮编辑器,问题是当我改变每个单词的颜色时它很慢,有时它不带颜色
import wx
from threading import Thread
import time
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(
self,None,title='Variable TextCtrl style text color')
self.panel = wx.Panel(self)
self.tc0 = wx.TextCtrl(
self.panel,-1,pos=(10,10),size=(350,100),style=wx.TE_RICH | wx.TE_MULTILINE)
self.tc0.Bind(wx.EVT_TEXT,self.ChangeText)
self.tc0.SetFont(wx.Font(28,wx.MODERN,wx.norMAL,False,u'Consolas'))
self.SetDoubleBuffered(True)
self.styleThread = StyleThread(self.tc0)
self.Show()
def ChangeText(self,event):
self.styleThread.setApplyStyle(True)
class StyleThread(Thread):
def __init__(self,txtCtrl):
Thread.__init__(self)
self.applyStyle = False
self.setDaemon(True)
self.start()
self.tc0 = txtCtrl
self.lastStyled = 0
def run(self):
while True:
if self.applyStyle:
self.style()
#Even if I change the time the program start to require more resources
time.sleep(0.5)
def style(self):
try:
endLine = self.tc0.GetInsertionPoint()
startLine = 0 if endLine < self.lastStyled else self.lastStyled
word = ""
unitary = False
while startLine < endLine:
character = self.tc0.GetValue()[startLine]
if not character.isdigit() and character != "." and not character in ["+","-"]:
word += character
if character in ["+","-"]:
self.tc0.SetStyle(self.tc0.GetInsertionPoint(
)-1,self.tc0.GetInsertionPoint(),wx.TextAttr(wx.RED))
word = ""
elif word.strip() in ["Derivar","Integrar"] or word.strip() in ["Pi","Grados"]:
self.tc0.SetStyle(self.tc0.GetInsertionPoint(
)-len(word),wx.TextAttr(wx.GREEN))
else:
self.tc0.SetStyle(self.tc0.GetInsertionPoint(
),self.tc0.GetInsertionPoint()+1,wx.TextAttr(wx.BLACK))
unitary = True
if unitary:
self.tc0.SetStyle(self.tc0.GetInsertionPoint(
)-len(word),wx.TextAttr(wx.BLACK))
unitary = False
self.tc0.SetDefaultStyle(wx.TextAttr(wx.BLACK))
startLine += 1
self.lastStyled = endLine
except:
print("ERROR")
def setApplyStyle(self,applyStyle):
self.applyStyle = applyStyle
if __name__ == '__main__':
app = wx.App()
frame = MainFrame()
app.MainLoop()
我读了很多,但我找不到为此目的的特殊事件,即使我使用了 StyledTextCtrl 并且我工作得很好,不幸的是屏幕阅读器不能完全访问它,因为它是应用程序。我想知道是否有任何方法可以做到这一点?谢谢:D
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。