wxpython禁用TextCtrl中的上下文菜单

时间:2010-10-28 00:47:34

标签: python wxpython

嘿。我试图在wxpython下的TextCtrl小部件中禁用上下文菜单(防止在右键单击时出现上下文菜单)。如果我创建我的菜单并将其绑定到鼠标右键,它将始终显示我的菜单,但如果我不在右键单击事件下创建菜单,它会自动创建一个标准的conext菜单,即使我不调用event.Skip()(见示例代码)。这是一个错误吗?围绕这个标准上下文菜单的任何想法?

    self.Bind(wx.EVT_RIGHT_DOWN, self.OnMouseRightDown)

def OnMouseRightDown(self, event):
    pt = event.GetPosition()
    self.RightClickContext(event, pt, True)

def RightClickContext(self, event, pt, enable):

    menu = wx.Menu()
    undo = menu.Append(ID_UNDO, 'Undo')
    menu.AppendSeparator()
    cut = menu.Append(ID_CUT, 'Cut')
    copy = menu.Append( ID_COPY, 'Copy' )        
    paste = menu.Append( ID_PASTE, 'Paste' )
    menu.AppendSeparator()
    delete = menu.Append( ID_DELETE, 'Delete' )
    selectall = menu.Append( ID_SELECTALL, 'Select All' )
    undo.Enable(False)
    cut.Enable(False)
    copy.Enable(False)
    if enable:
        paste.Enable(True)
    else:
        paste.Enable(False)
    delete.Enable(False)
    selectall.Enable(False)

    wx.EVT_MENU(menu, ID_PASTE,  self.MenuPaste)

    self.PopupMenu(menu, pt)
    menu.Destroy() 

1 个答案:

答案 0 :(得分:2)

试试这个(我在Python 2.7上):

import wx

class Test(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title='Test', size = (700,500))

        self.screen = wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE)
        self.screen.Bind(wx.EVT_CONTEXT_MENU, self.skip)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.screen, 1, wx.GROW | wx.ALL)
        self.SetSizer(self.sizer)

    def skip(self, evt):
        return

app = wx.App(False)
frame = Test(None)
frame.Show()
app.MainLoop()

很难说没有看到你的代码的上下文,但可能是你没有显式返回,或者还有一些其他事件也在被处理(如evt_right_up)