【问题标题】:How to not allow undo (Ctrl+Z) in Wx.Stc.StyledTextCtrl如何在 Wx.Stc.StyledTextCtrl 中不允许撤消 (Ctrl+Z)
【发布时间】:2019-11-24 20:39:36
【问题描述】:

我在 python-3 中做了一个项目,并用 wxpython 创建了一个 gui。在 gui 中,我使用 wx.stc.StyledTextCtrl 并且我不希望用户无法撤消 (Ctrl + Z)。有一个选项可以做到这一点?如果有人知道如何不允许(Ctrl + V),那也很棒。

感谢回答的人!

这里是创建 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()

【问题讨论】:

    标签: python-3.x user-interface wxpython keyboard-shortcuts wxstyledtextctrl


    【解决方案1】:

    另一种选择是使用stcCmdKeyClear 函数,它允许stc 为您完成工作。

    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()
    

    【讨论】:

      【解决方案2】:

      您可以将 StyledTextCtrl 绑定到 EVT_KEY_DOWN 事件,并在按下控制键时阻止 V 和 Z 键。使用您的示例:

      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()
      

      【讨论】:

        猜你喜欢
        • 2015-11-25
        • 2011-05-11
        • 2016-11-13
        • 2019-09-06
        • 1970-01-01
        • 2017-11-30
        • 2021-03-31
        • 2017-02-09
        • 2017-09-08
        相关资源
        最近更新 更多