wxpython.org에 PopupMenu 방법 섹션을 읽어
는 " 이 창을 기준으로 지정된 좌표에 지정된 메뉴를 pop-up 표시, 반환은 사용자가 기각 때 제어 메뉴. 메뉴 항목을 선택하면 해당 메뉴 이벤트 생성되고 정상적으로 처리된다. 기본 위치를 마우스 커서 의 현재 위치가 지정되고있는 경우에 사용된다. "
체크 도구의 EVT_MENU 이벤트에 바인딩해야합니다. 도구 버튼이 선택되면 메뉴를 팝업 할 수 있습니다. 팝업의 위치를 지정하지 않으면 마우스의 현재 위치가 사용됩니다. 마우스의 독립적 인 미리 정해진 위치에 팝업 메뉴를 원하는 경우
, 도구 모음의 화면 위치를 얻고 오프셋
가의 코드를 살펴 보자 추가 할 수 있습니다
[편집 : 도구에서 점의 위치를 계산하는 방법을 보여주기 위해 도구를 클릭하면 도구 모음에서 다양한 점을 계산하고 표시하도록 코드를 수정했습니다. 메뉴는 클릭 한 버튼의 오른쪽 하단 모서리에 나타납니다. 그것은 Windows에서 나를 위해 작동합니다. 그것은 다른 플랫폼에서 작동하지 않는 경우 내가 알고 궁금 하군요.] here에서 온이 코드의
import wx
class ViewApp(wx.App):
def OnInit(self):
self.frame = ToolFrame(None, -1, "Test App")
self.frame.Show(True)
return True
class MyPopupMenu(wx.Menu):
def __init__(self, parent):
wx.Menu.__init__(self)
self.parent = parent
minimize = wx.MenuItem(self, wx.NewId(), 'Minimize')
self.AppendItem(minimize)
self.Bind(wx.EVT_MENU, self.OnMinimize, id=minimize.GetId())
def OnMinimize(self, event):
self.parent.Iconize()
class ToolFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(350, 250))
self.toolbar = self.CreateToolBar()
self.tool_id = wx.NewId()
for i in range(3):
tool_id = wx.NewId()
self.toolbar.AddCheckLabelTool(tool_id, 'Tool', wx.EmptyBitmap(10,10))
self.toolbar.Bind(wx.EVT_MENU, self.OnTool, id=tool_id)
self.toolbar.Realize()
self.Centre()
self.Show()
def OnTool(self, event):
if event.IsChecked():
# Get the position of the toolbar relative to
# the frame. This will be the upper left corner of the first tool
bar_pos = self.toolbar.GetScreenPosition()-self.GetScreenPosition()
# This is the position of the tool along the tool bar (1st, 2nd, 3rd, etc...)
tool_index = self.toolbar.GetToolPos(event.GetId())
# Get the size of the tool
tool_size = self.toolbar.GetToolSize()
# This is the upper left corner of the clicked tool
upper_left_pos = (bar_pos[0]+tool_size[0]*tool_index, bar_pos[1])
# Menu position will be in the lower right corner
lower_right_pos = (bar_pos[0]+tool_size[0]*(tool_index+1), bar_pos[1]+tool_size[1])
# Show upper left corner of first tool in black
dc = wx.WindowDC(self)
dc.SetPen(wx.Pen("BLACK", 4))
dc.DrawCircle(bar_pos[0], bar_pos[1], 4)
# Show upper left corner of this tool in blue
dc.SetPen(wx.Pen("BLUE", 4))
dc.DrawCircle(upper_left_pos[0], upper_left_pos[1], 4)
# Show lower right corner of this tool in green
dc.SetPen(wx.Pen("GREEN", 4))
dc.DrawCircle(lower_right_pos[0], lower_right_pos[1], 4)
# Correct for the position of the tool bar
menu_pos = (lower_right_pos[0]-bar_pos[0],lower_right_pos[1]-bar_pos[1])
# Pop up the menu
self.PopupMenu(MyPopupMenu(self), menu_pos)
if __name__ == "__main__":
app = ViewApp(0)
app.MainLoop()
부품.
답장을 보내 주셔서 감사합니다. 문제는 메뉴를 여는 방법이 아니 었습니다. 도구 모음에서 개별 도구의 위치를 얻을 수 없을 때 위치를 지정하는 방법이었습니다 ("오프셋 추가"부분). 이 특정 문제에 대한 해결 방법을 찾았지만, 미래에는 도구 모음에서 도구의 위치 (픽셀 단위)를 얻는 방법이나 가능한 경우를 알고 싶습니다. –
수정 사항을 참조하십시오. 이제 클릭 한 도구의 왼쪽 위 모퉁이와 오른쪽 하단 모서리가 표시됩니다. 메뉴가 오른쪽 하단 모서리에 나타납니다. – Mathieu
Gnome을 사용하여 Mint Linux에서 잘 작동합니다. – Alan