1

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

完整代码

# -*- 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"
4

1 回答 1

4

从更高级别的角度来看,在搜索控件上捕获鼠标事件可能不是一个好主意,但最好按照文档中的说明绑定到内容(文本输入、按 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搜索控件中的将不会收到任何鼠标按下事件!

于 2015-01-14T10:31:05.393 回答