1
누군가 wxPython을 사용하여 TextCtrl 객체에 사용자 정의 컨텍스트 메뉴를 만드는 방법을 알고 있습니까? wx.TextCtrl의 삽입 점 위치에 팝업 메뉴
순간, 나는 사용자 지정 메뉴를 만들 수 있지만, [메뉴] 키![enter image description here](https://i.stack.imgur.com/IFyQv.png)
![enter image description here](https://i.stack.imgur.com/iogtV.png)
![enter image description here](https://i.stack.imgur.com/Xsik5.png)
누군가 wxPython을 사용하여 TextCtrl 객체에 사용자 정의 컨텍스트 메뉴를 만드는 방법을 알고 있습니까? wx.TextCtrl의 삽입 점 위치에 팝업 메뉴
순간, 나는 사용자 지정 메뉴를 만들 수 있지만, [메뉴] 키,
트릭 하지만이 작동않습니다 어떻게 든 수 만
글꼴 (아래 코드 참조) 고정 폭됩니다간단히 : 텍스트 커서 의 위치를 가져 오는 중입니다. 행/열 좌표에 문자 크기를 곱합니다. 이것은 원하는 팝업 메뉴 위치를 제공합니다.
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()