问题
Does anyone have a very simple example of using a find dialog with a text component in wxpython?
Thanks in advance.
回答1:
The use of wx.FindReplaceDialog
is not so straighforward as we could expect from its name.
This dialog gives you a dialog widget with parameters and entries for a search (or replace) action, You can read these parameters and the string to find from the dialog (actually from the event or from the wx.FindReplaceData
object). However reading, searching and/or replacing on a target text and the process to visualize the hit must be implemented separately.
This is for example a figure showing the dialog with a string to find and a text control where the string found is coloured.
The figure has been produced with the code below. Note however that this code is not fully functional. As it is, it only works for the first search. For a next search you must perform a new string.find()
from the current position and you also may want to 'clean' the previously found string giving it its original style. Also the script doesn't make use of the other parameters (search direction, force match case, etc).
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.tc = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_RICH2)
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()
To make full use of the widget you can check the properties and methods of wx.FindReplaceDialog, wx.FindReplaceData as well as for the events they emit.
Alternatively, you could check stani's python editor code. The GUI is wxPython and has a plugin for finding files containing a given text at different deepness of the directory tree. You could get a good hint from there. However it is not an wx.Dialog
as you want.
回答2:
Use the wiki
import wx
class MyDialog(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title)
class MyApp(wx.App):
def OnInit(self):
dia = MyDialog(None, -1, "simpledialog.py")
dia.ShowModal()
dia.Destroy()
return True
app = MyApp(0)
app.MainLoop()
来源:https://stackoverflow.com/questions/3827587/find-text-dialog-with-wxpython