0

I created a project in python-3 using wxpython. I would like to know if there is a function that prevents marking of text (in grey/blue square) for deletion. For example I want to prevent marking: "bla bla this text is marked bla bla". I don't want to enable the user to mark his text and then press delete or another key that will cause the marked text to be deleted. Another option if someone knows, is how to identify if there is text that is currently marked or maybe the length of the marked text, and I'll do the other things.

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()
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
Yuval Sharon
  • 159
  • 8
  • Welcome to SO! In order to post a question you should bring a minimum information as a input sample and expected output sample (if needed), what did you try and your research, in order to show some effort. What did you try? – David García Bodego Nov 25 '19 at 11:08
  • I searched in the documentation of wx.stc.StyledTextCtrl a function that can do that, but I didn't find one. maybe I didn't noticed that there is one? – Yuval Sharon Nov 25 '19 at 14:19

1 Answers1

0

If there is not a native function to perform the task, you will have to break it down into a group of functions/tasks that will perform the composite task.
I am unaware of such a function in StyledTextCtrl, so we must identify the component tasks, namely:

a key has been pressed
has a selection been made
if not skip
if so prevent the deletion by cancelling the selection
`SetEmptySelection(position) is one way to achieve this

import wx
from wx.stc import StyledTextCtrl

def On_KeyDown(evt):
    x, y = messageTxt.GetSelection()
    # If something is selected, de-select it
    if x != y:
        messageTxt.SetEmptySelection(y)
    else:
        evt.Skip()

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.Bind(wx.EVT_KEY_DOWN, On_KeyDown)

app.SetTopWindow(frame)
app.MainLoop()

This example works by essentially refusing selections.
If, however, you need to allow selections for other purposes, changing a style for example, then you will need to check the key that was pressed, before proceeding.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Thank you very much. Tell me, how did you find the SetEmptySelection() and GetSelection functions. I tried to search for them but I didn't succeed. Can you tell me what is your technique? – Yuval Sharon Nov 25 '19 at 18:06
  • I simply read the manual. I've never personally had the need to use a `StyledTextCtrl`. I linked to the online manual in a previous comment but some officious admin deleted the comment, Heaven knows why! If the're reading this, prehaps they should consider putting it back. – Rolf of Saxony Nov 25 '19 at 19:04