【问题标题】:Find text dialog with wxpython使用 wxpython 查找文本对话框
【发布时间】:2011-04-19 03:51:42
【问题描述】:

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

提前致谢。

【问题讨论】:

    标签: python wxpython wxwidgets


    【解决方案1】:

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

    例如,这是一个显示对话框的图,其中包含一个要查找的字符串和一个文本控件,其中找到的字符串是彩色的。

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

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

    要充分利用小部件,您可以检查wx.FindReplaceDialogwx.FindReplaceData 以及它们发出的events 的属性和方法。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多