1

I want to lock my wx.stc.StyledTextCtrl and to not allow writing into it. Someone knows a function that can do that? something like messagetxt.Lock() In addition, I want to add text where the insertion point, from the code when it is in ReadOnly

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()
Yuval Sharon
  • 159
  • 8

1 Answers1

1

Use SetReadOnly(True) is one way,
as in:

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.SetText("This styled text is read only")
messageTxt.SetReadOnly(True)
app.SetTopWindow(frame)
app.MainLoop()

Edit: Toggling the SetReadOnly flag will allow the program to alter the text, rather than the user.
i.e.:

import wx
from wx.stc import StyledTextCtrl

class MyFrame(wx.Frame):
    def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition, size=(400,500), style=wx.DEFAULT_FRAME_STYLE, name="MyFrame"):
        super(MyFrame, self).__init__(parent, id, title, pos, size, style, name)

        self.messageTxt = StyledTextCtrl(self, id=wx.ID_ANY, pos=(10,10), size=(300,100), style=wx.TE_MULTILINE)
        self.messageTxt.SetText("This styled text is read only")
        self.messageTxt.SetReadOnly(True)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)
        self.timer.StartOnce(5000)


    def onTimer(self, event):
        print("timer")
        self.messageTxt.SetReadOnly(False)
        self.messageTxt.AppendText("\nThis styled text was briefly read only")
        self.messageTxt.AppendText("\nNow it's read only again!")
        self.messageTxt.SetReadOnly(True)

app = wx.App()
frame = MyFrame(None, title="The Main Frame")
frame.Show(True)
app.MainLoop()
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Do you know how can I add text from the python (not the user) when it is on ReadOnly? I tried also to use messageTxt.SetEditable(False) however, I didn't find a function that can add text from the python while they are like that. – Yuval Sharon Nov 22 '19 at 17:18
  • I meant that I need a function that adds text to the messageTxt while it is in ReadOnly. It is important to my program to add text in this situation. – Yuval Sharon Nov 23 '19 at 11:56
  • That is what my code example does, within a millisecond or two. It has to be `NOT` readonly however briefly, to add text and then set readonly again. It is either `readonly` or `not` at any given time. – Rolf of Saxony Nov 23 '19 at 17:48