我正在构建一个GUI,我正在使用wxribbon进行wxpython。我希望我的用户单击功能区栏上的任何选项卡并重命名。问题是我无法通过右键单击功能区栏来获取对象(在本例中为选项卡)。我使用以下代码,但它无法正常工作。我无法获得右键单击的页面,我得到的是当前活动的页面。任何帮助都会很棒。谢谢!。示例代码如下:
self.Bind(RB.EVT_RIBBONBAR_TAB_RIGHT_DOWN, self.RightClickRibbonTabs, id = wx.ID_ANY )
def RightClickRibbonTabs(self,event):
index, tab = self._ribbon.HitTestTabs(wx.GetMousePosition())
page = self._ribbon._pages[index].page
pos = wx.GetMousePosition()
x, y = pos.x, pos.y
self.popupmenu = wx.Menu()
RenameTab = self.popupmenu.Append(-1, "Rename Tab")
self.Bind(wx.EVT_MENU, lambda event : self.RenameTab(event, x, y, page), RenameTab)
pos = wx.GetMousePosition()
x, y = pos.x, pos.y
pos = self.ScreenToClient(pos)
self.PopupMenu(self.popupmenu, pos)
def RenameTab(self, event, x, y, page):
text = wx.TextEntryDialog(None, "Rename Tab :", "Rename Tab", page.GetLabel())
text.CenterOnParent()
answer = text.ShowModal()
text.Destroy()
if answer == wx.ID_OK:
newname = text.GetValue()
page.SetLabel(newname)
self._ribbon.Realize()
else:
pass
答案 0 :(得分:0)
以下是解决方案:
def RightClickRibbonTabs(self,event):
pos = self.ScreenToClient(wx.GetMousePosition())
index, tab = self._ribbon.HitTestTabs(pos)
page = self._ribbon._pages[index].page
pos = wx.GetMousePosition()
x, y = pos.x, pos.y
self.popupmenu = wx.Menu()
RenameTab = self.popupmenu.Append(-1, "Rename Tab")
self.Bind(wx.EVT_MENU, lambda event : self.RenameTab(event, x, y, page), RenameTab)
pos = wx.GetMousePosition()
x, y = pos.x, pos.y
pos = self.ScreenToClient(pos)
self.PopupMenu(self.popupmenu, pos)