PropertyGrid 事件的鼠标位置

Mouse position of PropertyGrid event

我想为 PropertyGrid 实现一个 context/popup 菜单并提供 属性 特定命令,例如“出口项目”。单击名称字段时会弹出菜单。我不打算更改为值字段提供的弹出菜单,例如复制和粘贴功能。

PropertyGrid 提供了一个事件wx.propgrid.EVT_PG_RIGHT_CLICK。这让我可以使用

提取 PGProperty 信息 (name/value)
def OnRightClick(event):
    selectedName = event.GetProperty().GetName()
    selectedValue = event.GetProperty().GetValue()

但是,这不会像 wx.MouseEvent 那样提供鼠标事件的任何位置:

self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)  
def OnRightDown(event):
    self.PopupMenu(self, event.GetPosition())

如何确定 X/Y-location 在 PropertyGrid 的名称字段上打开弹出菜单?

菜单本质上定义为

class PopMenu(wx.Menu):
    def __init__(self, parent):
        super(PopMenu, self).__init__()
        self.parent = parent
        popmenu = wx.MenuItem(self, wx.NewId(), 'item')
        self.Append(popmenu)

似乎没有获取此信息的直接方法,因此您必须即兴发挥。
尝试使用 wx.GetMousePosition 的组合,然后使用 windows ScreenToClient 函数,如果您需要相对于 window.

的位置
import wx
import wx.propgrid as wxpg
import datetime

class TestPanel(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent)
        self.SetSizer(wx.BoxSizer(wx.VERTICAL))
        self.pg = wxpg.PropertyGridManager(self,
                    style=wxpg.PG_SPLITTER_AUTO_CENTER |
                          wxpg.PG_TOOLBAR)

        # Show help as tooltips
        mychoices = ["1","2","3","4","5"]
        self.pg.SetExtraStyle(wxpg.PG_EX_HELP_AS_TOOLTIPS)
        self.pg.Append(wxpg.IntProperty("Number"))
        self.pg.Append(wxpg.StringProperty("Word", value="Some text"))
        self.pg.Append(wxpg.BoolProperty("Boolean"))
        self.pg.Append(wxpg.ColourProperty("Colour"))
        self.pg.Append(wxpg.MultiChoiceProperty("Choose","Choose",mychoices,value=["1"]))
        now_date = wx.DateTime.Now()
        self.pg.Append( wxpg.DateProperty("Date",value=now_date))
        self.pg.Bind(wxpg.EVT_PG_RIGHT_CLICK, self.OnRightClick)
        self.GetSizer().Add(self.pg,proportion=1,flag=wx.EXPAND)

        #define popup menu
        self.popupmenu = wx.Menu()
        self.top = wx.MenuItem(self.popupmenu, wx.ID_ANY, "Title")
        self.popupmenu.Append(self.top)
        self.popupmenu.AppendSeparator()        
        for text in "one two three four five".split():
            item = self.popupmenu.Append(-1, text)

    def OnRightClick(self, event):
        scr = wx.GetMousePosition()
        rel = self.ScreenToClient(scr)
        #print("screen Pos", scr, "Client", rel)
        #open popup menu at right click location - identifying the item clicked
        self.popupmenu.Delete(self.top)
        self.top = wx.MenuItem(self.popupmenu, wx.ID_ANY, event.GetProperty().GetName())
        self.popupmenu.Prepend(self.top)
        self.PopupMenu(self.popupmenu, rel)

class wxFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title)
        self.panel = TestPanel(self)
        self.Show(True)

if __name__ == "__main__":
    app = wx.App(False)
    frame = wxFrame(None, 'Property Grid Test')
    app.MainLoop()