wxPython加速器似乎也会启动。这是错误吗?在这种情况下,不应该不按下加速键吗?
在下面的简短独立示例中,如果右键单击,将显示一个弹出菜单,其中包含一个禁用的菜单项。关联的加速键 CMD S 仍然可以被触发,但是!
import wx
help = """
If you right click on this window, a popup menu will appear with
a single menuitem which is DISABLED.
The associated accelerator key CMD S can still be triggered, however.
Is this a bug?
Shouldn't the accelerator keys NOT fire when its associated menu item
is disabled?
"""
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Accelerators fire even when menu item is disabled", size=(500,500))
panel = wx.Panel(self, wx.ID_ANY)
wx.StaticText(panel, wx.ID_ANY, help, wx.DefaultPosition, wx.DefaultSize, 0)
self.popupmenu = wx.Menu()
self.item: wx.Menuself.item = self.popupmenu.Append(wx.ID_ANY, "Do Something (CMD S)")
self.Bind(wx.EVT_MENU, self.DoSomething, self.item)
accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('S'), self.item.GetId() )])
self.SetAcceleratorTable(accel_tbl)
self.item.Enable(False) # hmm, accelerator still fires :-(
self.Bind(wx.EVT_CONTEXT_MENU, self.OnRight)
def OnRight(self, event):
self.PopupMenu(self.popupmenu)
def DoSomething(self, event):
msg = f"Something is being triggered, even though menuitem enabled is {self.item.Enabled}"
print(msg)
dlg = wx.MessageDialog(self, msg, "Message", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
class MyApp(wx.App):
def OnInit(self):
frame = MyForm()
frame.Show()
self.SetTopWindow(frame)
return True
# Run the program
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
也许我需要显式地检查每个处理程序函数,是否启用了“触发它的菜单”。这听起来有点乏味-理想情况下,事情应该是自动的。有趣的是,this post讨论了这些事情如何真正实现自动化(虽然与wxpython不相关,并且似乎特定于Windows)。
如果加速器与菜单项和菜单具有相同的标识符 项目为灰色或禁用,加速器已禁用且未启用 生成WM_COMMAND或WM_SYSCOMMAND消息。
我正在Python 3.7和wxPython 4.04下运行Mac OSX。
答案 0 :(得分:0)
我认为您可能是正确的。
您可能每次都必须检查或重建加速器表。
您可以使用以下方法暂时禁用所有Accelerators
:
self.SetAcceleratorTable(wx.NullAcceleratorTable)