开发者

wxpython disable Context Menu in TextCtrl

开发者 https://www.devze.com 2023-01-22 03:10 出处:网络
Hey. I am trying to disable the Context Menu in a TextCtrl widget under wxpython (prevent the context menu from coming up when right clicked). If I create my on Menu and bind it to the right mouse cli

Hey. I am trying to disable the Context Menu in a TextCtrl widget under wxpython (prevent the context menu from coming up when right clicked). If I create my on Menu and bind it to the right mouse click it will always show my menu but if I dont create menu under right mouse click event it automatically creates a standard conext menu, even if I dont call event.Skip() (see sample code). Is this a bug? Any ideas for a way around this standard context menu?

    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:
  开发者_StackOverflow      paste.Enable(False)
    delete.Enable(False)
    selectall.Enable(False)

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

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


Try this (I'm on 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()

Hard to tell without seeing the context of your code, but it could be that you didn't explicitly return, or that there is some other event that is also being processed (like evt_right_up)

0

精彩评论

暂无评论...
验证码 换一张
取 消