2014-11-11 5 views
1

누군가 wxPython을 사용하여 TextCtrl 객체에 사용자 정의 컨텍스트 메뉴를 만드는 방법을 알고 있습니까? wx.TextCtrl의 삽입 점 위치에 팝업 메뉴

순간, 나는 사용자 지정 메뉴를 만들 수 있지만, [메뉴] 키 enter image description here 타격 할 때 원래의 텍스트 커서 위치 enter image description here에 나타날 때이 사람은 마우스 커서 위치 enter image description here에서 나타납니다.

답변

1
좋아

,

트릭 하지만이 작동

않습니다 어떻게 든 수 만

글꼴 (아래 코드 참조) 고정 폭됩니다
  1. ,
  2. 에는 표 문자가없는
  3. (" \ t ") ...

간단히 : 텍스트 커서 enter image description here의 위치를 ​​가져 오는 중입니다. 행/열 좌표에 문자 크기를 곱합니다. 이것은 원하는 팝업 메뉴 위치를 제공합니다.

import wx 

# Test context menu that should be displayed at the insertion point position 
# in the TextCtrl object 
class CustomMenu(wx.Menu): 

    def __init__(self): 
     wx.Menu.__init__(self) 

     # Add some items in the menu 
     self.AppendItem(wx.MenuItem(self, wx.NewId(), 'This should be at the')) 
     self.AppendItem(wx.MenuItem(self, wx.NewId(), 'insertion point')) 
     self.AppendItem(wx.MenuItem(self, wx.NewId(), 'position...')) 

# Text area from which to open the custom context menu 
class TestTextCtrl(wx.TextCtrl): 

    def __init__(self, parent): 
     wx.TextCtrl.__init__(self, parent, style=wx.TE_MULTILINE) 

     self.parent = parent 

     # Set a monospaced font 
     font = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL) 
     self.SetFont(font) 

     # Get the character size (width and height) 
     self.pixelSize = font.GetPixelSize() 

     # Display some text 
     self.SetValue("[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n" 
         "[Ctrl] key = custom popup menu \n" 
         "[Menu] key = original context menu \n") 

     # Activate the [Ctrl] key stroke detection (1/2) 
     self.Bind(wx.EVT_KEY_DOWN, self.OnKeyStroke) 

    def OnKeyStroke(self, event): 

     # Activate the [Ctrl] key stroke detection (2/2) 
     if event.GetUnicodeKey() == wx.WXK_CONTROL: 

      ####################################### 
      ##### Here is the interesting code #### 
      ####################################### 

      # Get the scroll position in pixels 
      scrollPosition = [ 
       self.GetScrollPos(wx.HORIZONTAL), 
       self.GetScrollPos(wx.VERTICAL), 
       ] 

      # Get the text cursor position in the text area (int) 
      insertionPointPosition = self.GetInsertionPoint() 

      # Convert it into a row/column position (2D tuple) 
      insertionPointRowColumnPosition = self.PositionToXY(insertionPointPosition) 

      # Calculate the popup menu position in pixels 
      insertionPointPositionInPixels = [] 
      for i in range(2): 
       insertionPointPositionInPixels.append(
      #  ( row/column position + offset ) * size of character - position of scroll in pixels) 
        (insertionPointRowColumnPosition[i] + i) * self.pixelSize[i] - scrollPosition[i] 
       ) 

      # Convert the position into a wx.Point object 
      popupMenuPoint = wx.Point(
       insertionPointPositionInPixels[0], 
       insertionPointPositionInPixels[1] 
      ) 

      # Display the context menu at the given position 
      self.PopupMenu(CustomMenu(), popupMenuPoint) 

      ### We did it ! :) ### 

      ####################################### 
      ####################################### 
      ####################################### 

     else: 
      event.Skip() 

# Test frame 
class TestFrame(wx.Frame): 

    def __init__(self): 
     wx.Frame.__init__(self, None) 

     # Create a text area 
     TestTextCtrl(self) 

     # Display the window 
     self.Show(True) 


if __name__ == "__main__": 

    # Create an application 
    application = wx.App(False) 

    # Create a frame 
    TestFrame() 

    # Launch the application 
    application.MainLoop() 

업데이트 # 1은 - font.GetPixelSize()

업데이트 #에서 가져온 문자> 크기 2 - 계정에서 촬영 스크롤 막대의> 위치가 self.GetScrollPos()

사용