在以下示例中,我为带有恢复选项的菜单列表编码了一个过滤器。它被放置在面板中。
我的问题是是否可以在菜单栏中放置 wx.TextCtrl 字段?
这是近似代码。也许有更好的解决方案?我想制作一个用户友好且直观的小部件。
#!/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()