java
java

Reputation: 1214

how to set style of text ctrl in wxpython?

I have a text ctrl as follows:

self.abc= wx.TextCtrl(self, -1, "", style=wx.TE_PROCESS_ENTER|wx.TE_MULTILINE)

What I want is the ability to add or remove styles from the text ctrl after it was created.

something like:

if x:
   make abc to style=wx.TE_PROCESS_ENTER|wx.TE_MULTILINE|TE_READONLY
else:
   make abc to style=wx.TE_PROCESS_ENTER|wx.TE_MULTILINE

I saw the function SetStyle however from what I read it only ADD style, it doesn't overwrite existing style.

What can I do?

Upvotes: 2

Views: 3726

Answers (1)

Oliver W.
Oliver W.

Reputation: 13459

According to the documentation, not all of the window styles of a wx.TextCtrl can be changed dynamically: the last paragraph of the wx.TextCtrl documentation on window styles mentions this:

Note that alignment styles (TE_LEFT, TE_CENTRE and TE_RIGHT) can be changed dynamically after control creation on wxMSW and wxGTK. TE_READONLY, TE_PASSWORD and wrapping styles can be dynamically changed under wxGTK but not wxMSW. The other styles can be only set during control creation.

That being said, in your example, the only thing you seem to be interested in changing is the READONLY style, which, as mentioned in the docs, can be changed.

The way to do that is to call the TextCtrl's SetEditable method:

Here's an example showing how to toggle the flag with another button:

import wx

class MyApp(wx.App):
    def __init__(self, redirect=False, filename=None):
        wx.App.__init__(self, redirect, filename)
        self.frame = wx.Frame(None, wx.ID_ANY, title='My Title')
        self.panel = wx.Panel(self.frame, wx.ID_ANY)
        b = wx.Button(self.panel, -1, "Toggle the read-only flag", (50,50))
        self.abc= wx.TextCtrl(self.panel, -1, "", (30, 70), size=(410,90), style=wx.TE_MULTILINE)

        self.Bind(wx.EVT_BUTTON, self.OnButton, b)
        self.cnt = 0
        self.frame.Show()

    def OnButton(self, evt):
        self.cnt += 1
        print(self.abc.IsEditable()) # for debugging
        self.abc.SetEditable((True, False)[self.cnt%2])

if __name__ == '__main__':
   app = MyApp()
   app.MainLoop()

Upvotes: 3

Related Questions