gpt4 book ai didi

python - 即使关联的菜单项被禁用,wxpython 加速器也会触发

转载 作者:太空宇宙 更新时间:2023-11-03 21:21:52 25 4
gpt4 key购买 nike

即使关联的菜单项被禁用,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)。

If an accelerator has the same identifier as a menu item and the menu item is grayed or disabled, the accelerator is disabled and does not generate a WM_COMMAND or WM_SYSCOMMAND message.

我在 Python 3.7 和 wxPython 4.04 下运行 Mac OSX。

最佳答案

我认为你可能是对的。
您可能每次都必须检查或重建加速器表。
您可以通过以下方式暂时禁用所有加速器:

self.SetAcceleratorTable(wx.NullAcceleratorTable)

关于python - 即使关联的菜单项被禁用,wxpython 加速器也会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54175692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com