searchctrl EVT_LEFT_DOWN事件无效(wxpython)

时间:2015-01-14 01:58:49

标签: click wxpython mouseevent textctrl

当我点击searchctrl输入一些代码时,我尝试使用searchctrl创建一个鼠标事件。 我的目标是当我单击searchctrl(编辑为文本或wirte)时,将启动虚拟键盘(板载)。我发现searchctrl是低类别textctrl。但是wx.EVT_LEFT_DOWN不起作用 我怎样才能使它工作(我认为这是可能的。因为当我点击searchctrl(文本)时,value =“”(提示)消失了

完整代码

# -*- coding: utf-8 -*-

import wx

[wxID_FRAME1, wxID_FRAME1TEXTCTRL1,
] = [wx.NewId() for _init_ctrls in range(2)]

class MyFrame1 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY,
            title = wx.EmptyString, pos = wx.DefaultPosition,
            size = wx.Size( 700,400 ),
          #  style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL
        )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        # Create controls
        sb = wx.StaticBox(self, -1, "Options")
        searchBtnOpt = wx.CheckBox(self, -1, "Search button")
        searchBtnOpt.SetValue(True)
        cancelBtnOpt = wx.CheckBox(self, -1, "Cancel button")
        menuBtnOpt   = wx.CheckBox(self, -1, "Search menu")

        self.search = wx.SearchCtrl(self,size=(200,-1), style=wx.TE_PROCESS_ENTER)
        #self.search.SetHint(self, love)
        # Setup the layout
        self.Bind(wx.EVT_LEFT_DOWN, self.OnClick, self.search)
        box = wx.StaticBoxSizer(sb, wx.VERTICAL)
        box.Add(searchBtnOpt, 0, wx.ALL, 5)
        box.Add(cancelBtnOpt, 0, wx.ALL, 5)
        box.Add(menuBtnOpt,   0, wx.ALL, 5)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(box, 0, wx.ALL, 15)
        sizer.Add((15,15))
        sizer.Add(self.search, 0, wx.ALL, 15)

##         self.tc = wx.TextCtrl(self)  # just for testing that heights match...
##         sizer.Add(self.tc, 0, wx.TOP, 15)

        self.SetSizer(sizer)

        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
              parent=self, pos=wx.Point(120, 80), size=wx.Size(100, 21),
              style=0, value='textCtrl1')
        self.textCtrl1.Bind(wx.EVT_LEFT_DOWN, self.OnClick)


        # Set event bindings
        self.Bind(wx.EVT_CHECKBOX, self.OnToggleSearchButton, searchBtnOpt)
        self.Bind(wx.EVT_CHECKBOX, self.OnToggleCancelButton, cancelBtnOpt)
        self.Bind(wx.EVT_CHECKBOX, self.OnToggleSearchMenu,   menuBtnOpt)
        self.search.Bind(wx.EVT_LEFT_DOWN, self.OnClick)
        self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, self.OnSearch, self.search)
        self.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, self.OnCancel, self.search)
        self.Bind(wx.EVT_TEXT_ENTER, self.OnDoSearch, self.search)
        ##self.Bind(wx.EVT_TEXT, self.OnDoSearch, self.search)


    def OnToggleSearchButton(self, evt):
        self.search.ShowSearchButton( evt.GetInt() )

    def OnToggleCancelButton(self, evt):
        self.search.ShowCancelButton( evt.GetInt() )

    def OnToggleSearchMenu(self, evt):
        if evt.GetInt():
            self.search.SetMenu( self.MakeMenu() )
        else:
            self.search.SetMenu(None)

    def OnClick(self, event):
        print "333"

    def OnSearch(self, evt):
        print "555"

    def OnCancel(self, evt):
        self.log.write("OnCancel")

    def OnDoSearch(self, evt):
        self.log.write("OnDoSearch: " + self.search.GetValue())
        print "555"


    def MakeMenu(self):
        menu = wx.Menu()
        item = menu.Append(-1, "Recent Searches")
        item.Enable(False)
        for txt in [ "You can maintain",
                     "a list of old",
                     "search strings here",
                     "and bind EVT_MENU to",
                     "catch their selections" ]:
            menu.Append(-1, txt)
        return menu
    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def searchCtrlOnSearchButton( self, event ):
        print("SearchCtrl SEARCH_BTN clicked")
        event.Skip()

    def searchCtrlOnTextEnter( self, event ):
        print("SearchCtrl ENTER pressed")
        event.Skip()


if __name__ == '__main__':
    app = wx.PySimpleApp( 0 )
    frame = MyFrame1( None )
    frame.Show()
    app.MainLoop()

这部分是问题

    self.search = wx.SearchCtrl(self,size=(200,-1), style=wx.TE_PROCESS_ENTER)
    self.Bind(wx.EVT_LEFT_DOWN, self.OnClick, self.search)

    self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
          parent=self, pos=wx.Point(120, 80), size=wx.Size(100, 21),
          style=0, value='textCtrl1')
    self.textCtrl1.Bind(wx.EVT_LEFT_DOWN, self.OnClick)

    self.search.Bind(wx.EVT_LEFT_DOWN, self.OnClick)

def OnClick(self, event):
    print "333"

1 个答案:

答案 0 :(得分:2)

据推测,从更高级别的角度来看,在搜索控件上捕获鼠标事件并不是一个好主意,但最好绑定到文档中排列的内容(文本输入,按Enter键,按搜索/清除按钮,...)。

这就是说,我使用wxPython WIT(小部件检查工具)来获取可视化的内容。它通过在主循环之前添加以下内容来激活:

    ...
    import wx.lib.inspection
    wx.lib.inspection.InspectionTool().Show()

    app.MainLoop()

如果您在左侧窗格中浏览窗口小部件树,则可以看到SearchCtrl有一个TextCtrl个孩子,名为text。如果您进一步单击窗口小部件树中的TextCtrl并激活工具栏中的“事件”窗口,您会看到它正在获取所有wx.EVT_LEFT_DOWN个事件。我不知道为什么搜索控件不从文本控件继承鼠标事件。收到EVT_TEXT!您可以执行以下操作:

    self.search = wx.SearchCtrl(#...

    search_txtctrl = self.search.FindWindowByName('text')
    search_txtctrl.Bind(wx.EVT_LEFT_DOWN, self.OnClick)

def OnClick(self, evt)
    # do your stuff
    evt.Skip()

此时不要忘记跳过,否则搜索控件中的wx.TextCtrl将不会收到任何鼠标按下事件!