python - 如何在 StyledTextCtrl 中创建查找对话框?
问题描述
关于这个主题有一个现有问题:Find text dialog with wxpython,但它在 TextCtrl 中。于是我把TextCtrl改成了StyledTextCtrl,并测试了一下。但我得到了这个错误:
in wxStyledTextCtrl::SetStyle(): not implemented
如何使 SetStyle 成为选择,以便您可以点击离开?这是我的代码:
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()
解决方案
看起来像一个高度复杂的StyledTextCtrl
野兽,我只能假设您必须详尽地阅读scintilla
文档http://www.scintilla.org/
我评论中的链接指向 using SetStyling
function not AddSelection
。
这就是我使用它管理的:
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()
推荐阅读
- javascript - 如何设置带有可选“(”和“)”的正则表达式,在使用时需要两者?
- java - Hashmap并排比较
- jquery - 仅在具有特定功能时才使用 jquery 更改 CSS 属性值
- c# - 线程仍在尝试访问已处理的表单?
- c - 无法使用 write() 写入标准输出
- django - django-tables2:添加模型中未找到的新自定义列
- playframework - 部署 Play 应用程序时找不到 javac 错误
- angular5 - Angular 5/6:保护路由(路由保护)而不重定向到错误路由
- python - 如何使用字典更新而不是创建列表并传递值
- c# - uwp中的应用中心导致服务器执行失败0x80080005