How to create usable multiple cursor in StyledTextCtrl when using wxpython?

164 views Asked by At

I want to add multiple cursor feature to my wxpython application. I'm using StyledTextCtrl as my text control. I already make it look like multiple cursor, see below image. However, when I type text, the first two cursor just disappeared and the text were inserted into the last cursor position. Can anyone help me on this please? I've attached my code.

C:\Windows\system32>python --version
Python 3.7.0

C:\Windows\system32>pip list
Package     Version
----------- ---------
altgraph    0.15
cffi        1.11.5
future      0.16.0
macholib    1.9
pefile      2017.11.5
pip         18.0
pycparser   2.18
PyInstaller 3.3.1
pypiwin32   223
Pypubsub    4.0.0
PyQt5       5.11.2
PyQt5-sip   4.19.12
pyqtdeploy  2.1
pyqtkeybind 0.0.3
pywin32     223
setuptools  39.0.1
six         1.11.0
wxPython    4.0.3
xcffib      0.6.0

C:\Windows\system32>

enter image description here

import wx
import wx.stc as stc


class MyEditor(stc.StyledTextCtrl):
    def __init__(self, parent, size=wx.DefaultSize, style=0):
        stc.StyledTextCtrl.__init__(self, parent, size=size, style=style)
        self.SetCaretLineVisible(True)

        font = wx.Font(20, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, 'Courier New')
        self.StyleSetFont(stc.STC_STYLE_DEFAULT, font)

        self.SetText(
            # @formatter:off
"""aaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbb
cccccccccccccccccc
dddddddddddddddddd
eeeeeeeeeeeeeeeeee
ffffffffffffffffff
gggggggggggggggggg""")
# @formatter:on
        self.SetCaretWidth(20)
        self.SetScrollWidth(1100)
        self.SetCaretForeground('red')

    def test(self, event):
        self.SetSelection(0, 0)
        self.AddSelection(3, 10)
        self.AddSelection(20, 40)
        self.SetFocus()

class MyFrame(wx.Frame):
    def __init__(self, parent):
        super(MyFrame, self).__init__(parent)
        self.InitUI()

    def InitUI(self):
        panel = wx.Panel(self)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        self._elements_sizer = main_sizer
        self._elements_panel = panel

        editor = MyEditor(panel, style=wx.ALIGN_CENTER)
        main_sizer.Add(editor, 1, wx.EXPAND | wx.ALL, 10)
        self._editor = editor


        ##################################################
        buttons_panel = wx.Panel(panel)
        buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)

        b3 = wx.Button(buttons_panel, label="b3")
        b3.Bind(wx.EVT_BUTTON, editor.test)
        buttons_sizer.Add(b3, 0, wx.ALL & (~wx.TOP) & (~wx.BOTTOM) & (~wx.LEFT), 10)

        buttons_panel.SetSizer(buttons_sizer)

        main_sizer.Add(buttons_panel, 0, wx.ALL & (~wx.TOP) & (~wx.Bottom), 10)
        ###################################################

        self.SetSize(1200, 600)
        panel.SetSizerAndFit(main_sizer)

        self.SetTitle("test")

        self.Show(True)


if __name__ == '__main__':
    app = wx.App()
    MyFrame(None)
    app.MainLoop()

Update:

What I want to implement is a multi-cursor editing feature, which exists in many popular editors. See below image:

enter image description here

0

There are 0 answers