Question

My GUI currently has right-click options (Cut, Copy, Paste)

However, I realize when I right-click over my search bar (wx.SearchCtrl) to try and paste, focus is not set onto the search bar, therefore I cannot paste.

self.panel.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup)

def OnShowPopup(self, event):
   """ Obtain right-click selection """
   pos = event.GetPosition()
   pos = self.panel.ScreenToClient(pos)
   self.panel.PopupMenu(self.rightClickMenu, pos)

def OnPopupItemSelected(self, event):
   """ Display right-click menu """
   item = self.rightClickMenu.FindItemById(event.GetId())
   text = item.GetText()
   elif text == "Paste":
      self.OnPaste()

def OnPaste(self, event = None):
   """ Paste content from clipboard """
   text = self.FindFocus()
   if text:
      if isinstance(text, wx.TextCtrl):
         text.Paste()

Here is my idea: Get the position of mouse when right-clicked. Then use that position to set focus on the Ctrl that holds that position.

Is this possible? Or is there a better solution?

Was it helpful?

Solution 2

Save the object of where right-clicked was performed, then setFocus after selecting Paste. The reason why event.GetEventObject().SetFocus() does not work is most likely because after selecting Paste from the PopupMenu, the TextCtrl loses focus. So text would not print there

def OnShowPopup(self, event):
   """ Obtain right-click selection """
   pos = event.GetPosition()
   pos = self.panel.ScreenToClient(pos)
   self.rightClickSelected = event.GetEventObject()
   self.panel.PopupMenu(self.rightClickMenu, pos)

def OnPaste(self, event = None):
   """ Paste content from clipboard """
   self.rightClickSelected.SetFocus()
   if isinstance(self.rightClickSelected, wx.TextCtrl):
      self.rightClickSelected.Paste()

OTHER TIPS

def OnShowPopup(self, event):
      """ Obtain right-click selection """

      pos = event.GetPosition()
      pos = self.panel.ScreenToClient(pos)
      event.GetEventObject().SetFocus()
      self.panel.PopupMenu(self.rightClickMenu, pos)

would work fine I think ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top