本文介绍了如何在 StyledTextCtrl 中创建查找对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
存在关于此主题的问题:
There is an existing question on this subject: Find text dialog with wxpython, but it's in TextCtrl. So I changed the TextCtrl to StyledTextCtrl, and tested it. But I got this error:
in wxStyledTextCtrl::SetStyle(): not implemented
How do I make the SetStyle a selection instead so that you can click away? Here is my code:
import wx
import wx.stc as stc
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.tc = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
self.bt_find = wx.Button(self, -1, "find")
self.Bind(wx.EVT_BUTTON, self.on_button, self.bt_find)
self.Bind(wx.EVT_FIND, self.on_find)
self.pos = 0
self.size = 0
#
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.tc, 1, wx.EXPAND, 0)
sizer.Add(self.bt_find, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
self.SetSizer(sizer)
sizer.Fit(self)
self.Layout()
def on_button(self, event):
self.txt = self.tc.GetValue()
self.data = wx.FindReplaceData() # initializes and holds search parameters
self.dlg = wx.FindReplaceDialog(self.tc, self.data, 'Find')
self.dlg.Show()
def on_find(self, event):
fstring = self.data.GetFindString() # also from event.GetFindString()
self.pos = self.txt.find(fstring, self.pos)
self.size = len(fstring)
self.tc.SetStyle(self.pos, self.pos+self.size, wx.TextAttr("red", "black"))
if __name__ == "__main__":
app = wx.PySimpleApp(0)
frame_1 = MyFrame(None, wx.ID_ANY, "")
frame_1.Show()
app.MainLoop()
解决方案
The StyledTextCtrl
looks like a highly complex beast and I can only assume that you will have to read the scintilla
documents exhustively http://www.scintilla.org/
The link in my comment was pointing at using SetStyling
function not AddSelection
.
This is what I managed using that:
import wx
import wx.stc as stc
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.tc = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
self.bt_find = wx.Button(self, -1, "find")
self.Bind(wx.EVT_BUTTON, self.on_button, self.bt_find)
self.Bind(wx.EVT_FIND, self.on_find)
self.pos = 0
self.size = 0
#
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.tc, 1, wx.EXPAND, 0)
sizer.Add(self.bt_find, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
self.SetSizer(sizer)
sizer.Fit(self)
self.Layout()
def on_button(self, event):
self.txt = self.tc.GetValue()
self.data = wx.FindReplaceData() # initializes and holds search parameters
dlg = wx.FindReplaceDialog(self.tc, self.data, 'Find')
dlg.Show()
def on_find(self, event):
self.tc.StartStyling(pos=0, mask=0xFF)
self.tc.SetStyling(length=len(self.txt), style=0)
fstring = event.GetFindString()
self.size = len(fstring)
while True:
self.pos = self.txt.find(fstring, self.pos)
if self.pos < 0:
break
self.tc.StyleSetSpec(1, "fore:#FF0000,back:#000000")
self.tc.StartStyling(pos=self.pos, mask=0xFF)
self.tc.SetStyling(length=self.size, style=1)
self.pos += 1
self.pos = 0
if __name__ == "__main__":
app = wx.App()
frame_1 = MyFrame(None, wx.ID_ANY, "")
frame_1.Show()
app.MainLoop()
这篇关于如何在 StyledTextCtrl 中创建查找对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!