使用 wxpython 查找文本对话框 [英] Find text dialog with wxpython

查看:24
本文介绍了使用 wxpython 查找文本对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个非常简单的例子,可以在 wxpython 中使用带有文本组件的查找对话框?

Does anyone have a very simple example of using a find dialog with a text component in wxpython?

提前致谢.

推荐答案

wx.FindReplaceDialog 的使用并不像我们从它的名字所期望的那样直接.此对话框为您提供了一个对话框小部件,其中包含搜索(或替换)操作的参数和条目,您可以读取这些参数和要从对话框中查找的字符串(实际上是从事件或从 wx.FindReplaceData 对象).然而,阅读、搜索和/或替换目标文本以及将命中可视化的过程必须单独实施.

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.

该图已使用以下代码生成.但是请注意,此代码的功能并不完整.事实上,它只适用于第一次搜索.对于下一次搜索,您必须从当前位置执行一个新的 string.find() 并且您可能还想清理"先前找到的字符串,使其具有原始样式.此外,该脚本不使用其他参数(搜索方向、强制匹配大小写等).

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() 

要充分利用widget可以查看的属性和方法wx.FindReplaceDialog, wx.FindReplaceData 以及 事件他们发出.

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.

或者,您可以检查stani 的 python 编辑器代码.GUI 是 wxPython 并且有一个插件,用于在目录树的不同深度查找包含给定文本的文件.你可以从那里得到一个很好的提示.但是,它不是您想要的 wx.Dialog.

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.

这篇关于使用 wxpython 查找文本对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆