1

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?

James the Great
  • 941
  • 2
  • 14
  • 46

2 Answers2

1
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 ...

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • It does not work. I have tried replacing my_wx_search_ctrl with self, self.panel, and self.search. – James the Great Dec 04 '13 at 22:31
  • edited to give focus to specific widget ... you could popup your menu from EVT_RIGHT_DOWN also and get rid of the binding to contextmenu event – Joran Beasley Dec 05 '13 at 00:15
  • I started using the wxPython Widget Inspection Tool, and it shows that whenever I right-click on TextCtrl or SearchCtrl, wx.EVT_CONTEXT_MENU is activated. No sign of EVT_RIGHT_DOWN anywhere – James the Great Dec 05 '13 at 04:37
  • I am using: wxPython 2.8.10.1, Windows 7, Python 2.4 – James the Great Dec 05 '13 at 04:38
  • I tried to skip the wx.EVT_CONTEXT_MENU event with event.skip(), hopefully wx.EVT_RIGHT_DOWN shows up, but still nothing. Reference: https://groups.google.com/forum/#!topic/wxpython-users/TMPWQjiikx4 – James the Great Dec 05 '13 at 05:31
  • hmmm well Im at a loss :/ If you can put up a full (minimal) working example on pastebin or something I could take a look – Joran Beasley Dec 05 '13 at 16:45
  • Please look into wx.EVT_RIGHT_DOWN & wx.EVT_CONTEXT_MENU. I have tried using search.Bind etc also: http://pastebin.com/YnLPe6hC – James the Great Dec 05 '13 at 17:07
  • hmmm ... when I tried it, it worked fine just the way you have it... data was pasted in to the search field, what OS are you on (btw your interface looks very nice) – Joran Beasley Dec 05 '13 at 17:12
  • Both Windows and Linux. Paste works with the default right-click option (I can use the self.rightClickMenu to pop-up my custom made right-click menu, but so far it only works if the interface recognizes wx.EVT_CONTEXT_MENU) The problem is wx.EVT_RIGHT_DOWN is not recognized ("hi" is not printed) – James the Great Dec 05 '13 at 17:19
  • I started learning Python 3 months ago, and wxPython a month ago. Took me awhile to get this interface :/ – James the Great Dec 05 '13 at 17:20
  • event.GetEventObject().SetFocus() did not set focus ... it returns None – James the Great Dec 05 '13 at 17:56
  • I think SetFocus() is called after I have selected Paste – James the Great Dec 05 '13 at 17:58
  • event.GetEventObject() will get whatever control you right clicked on ... and trigger it with the EVT_CONTEXT_MENU ... get rid of the ON_RIGHT_DOWN binding. SetFocus will set focus on the item but yes it returns None ... – Joran Beasley Dec 05 '13 at 17:59
  • Not for Linux. btw I used: self.panel.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup) – James the Great Dec 05 '13 at 17:59
  • yeah thats exactly what I used ... maybe try printing `event.GetEventObject()` (I am using py2.6 ... but that shouldnt matter) – Joran Beasley Dec 05 '13 at 18:00
  • event.GetEventObject() gives out an object. but event.GetEventObject().SetFocus() returns None – James the Great Dec 05 '13 at 18:01
  • of coarse it returns None ... it never returns anything ... it just gives focus to what its called on ... there is no return – Joran Beasley Dec 05 '13 at 18:11
  • that code you posted seems to work fine for windows 7 (explicitly setting to self.search) ... I cant really tell you much about linux though :/ – Joran Beasley Dec 05 '13 at 18:14
  • If I edit the OnPaste function with "print self.FindFocus()", it prints out None. That means by the time Paste is selected nothing is focus. – James the Great Dec 05 '13 at 18:16
  • Alright thanks for your help so far. I will test out Windows when I get home. I will keep you updated – James the Great Dec 05 '13 at 18:17
  • if i print self.FindFocus() in onPast it says just fyi what Im seeing ... why not just use `self.search` instead of `self.FindFocus()` will you be needing other text boxes to get pastes? – Joran Beasley Dec 05 '13 at 18:19
  • I might need other boxes to get paste later. But anyways I just figured another way :) – James the Great Dec 05 '13 at 18:28
  • Joran please help: http://stackoverflow.com/questions/20431669/wxpython-mouse-left-click-evt-left-down-on-searchctrl – James the Great Dec 06 '13 at 19:19
1

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()
James the Great
  • 941
  • 2
  • 14
  • 46