wx.TextCtrl field in menubar

417 views Asked by At

In the following example I encoded a filter for the menu list with recovering option. It is placed in the panel.

My question is whether is it possible to place wx.TextCtrl field in menubar?

Here is the approximation code. Maybe there is even better solution? I would like the make a user friendly and intuitive widget.

#!/usr/bin/python
# -*- coding: utf-8 -*-


import wx

mlst=["LT1","LT2","LT3","RT1","RT2","RT3","LF1","LF2","LF3","RF1","RF2","RF3"]

class MainWidget(wx.Frame):

    def __init__(self, parent, title):
        super(MainWidget, self).__init__(parent, title=title)

        panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox=wx.BoxSizer(wx.HORIZONTAL)
        st = wx.StaticText(panel, label='Button filter')
        self.tc=wx.TextCtrl(panel,size=(40,-1))
        hbox.Add(st, flag=wx.RIGHT, border=10)
        hbox.Add(self.tc, proportion=.1)
        vbox.Add(hbox, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        panel.SetSizer(vbox)    

        menubar = wx.MenuBar()
        self.mButton=wx.Menu()
        mr=self.mButton.Append(wx.NewId(),'&Restore')
        self.Bind(wx.EVT_MENU, self.OnRestore,mr)

        for i,item in enumerate(mlst):
            self.mButton.Append(wx.NewId(),item,item)

        self.Bind(wx.EVT_TEXT, self.OnText,self.tc)

        menubar.Append(self.mButton, '&Button')
        self.SetMenuBar(menubar)

        #mtitle="{} {}".format(len(mlst),self.mButton.GetLabel()  )


    def OnRestore(self,event):
        itemnames=[ x.GetText() for x in self.mButton.GetMenuItems() ]
        for item in mlst:
            if item not in itemnames:
                self.mButton.Append(wx.NewId(),item,item)
        self.tc.ChangeValue('')

    def OnText(self, event):
        text = self.tc.GetValue()
        items=self.mButton.GetMenuItems()

        for i,item in enumerate(items):
            if i>0:
                if text not in item.GetText():
                    print 'deleting: ',item.GetText()
                    self.mButton.RemoveItem(item)

        print [ x.GetText() for x in self.mButton.GetMenuItems() ]


if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = MainWidget(None, "Menu items filter")
    frame.Show(True)
    app.MainLoop()
1

There are 1 answers

1
Mike Driscoll On

I don't believe this is possible with the normal Menu widgets as they wrap the native widgets and I've never seen that in a native menu before. You might be able to do it with FlatMenu since it is pure Python. At the very least, you should be able to hack the FlatMenu code to allow it. Otherwise you will need to roll your own.

You can put a TextCtrl in the Toolbar (and in the FlatMenu toolbar implementation) though.