问题描述
我正在尝试用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 (将#修改为@)