How to not allow undo (Ctrl+Z) in Wx.Stc.StyledTextCtrl

192 Views Asked by At

I do a project in python-3 and I create a gui with wxpython. In the gui, I use wx.stc.StyledTextCtrl and I wan't that the user won't be able to do undo (Ctrl + Z). There is an option to do that? It is also be awesome if someone knows how to not allow either (Ctrl + V).

Thanks to those who answer!

Here is a basic code of creating a wx.stc.StyledTextCtrl:

import wx
from wx.stc import StyledTextCtrl

app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(100 * 3, 100),
                            style=wx.TE_MULTILINE, name="File")

app.SetTopWindow(frame)
app.MainLoop()
2

There are 2 best solutions below

0
On BEST ANSWER

Another option is to use stc's CmdKeyClear function, which allows stc to do the work for you.

import wx
from wx.stc import StyledTextCtrl

app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(100 * 3, 100),
                            style=wx.TE_MULTILINE, name="File")

messageTxt.CmdKeyClear(ord('V'), wx.stc.STC_SCMOD_CTRL)
messageTxt.CmdKeyClear(ord('Z'), wx.stc.STC_SCMOD_CTRL)

app.SetTopWindow(frame)
app.MainLoop()
0
On

You can bind your StyledTextCtrl to the EVT_KEY_DOWN event and block the V and Z keys when the control key is pressed. Using your example:

import wx
from wx.stc import StyledTextCtrl

app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(100 * 3, 100),
                            style=wx.TE_MULTILINE, name="File")


def on_key_down(evt):
    """
    :param evt:
    :type evt: wx.KeyEvent
    :return:
    :rtype:
    """

    if evt.CmdDown() and evt.GetKeyCode() in (ord("Z"), ord("V")):
        print("vetoing control v/z")
        return
    # allow all other keys to proceed
    evt.Skip()


messageTxt.Bind(wx.EVT_KEY_DOWN, on_key_down)

app.SetTopWindow(frame)
app.MainLoop()