使用wxpython时如何在StyledTextCtrl中创建可用的多个光标?

时间:2018-08-02 16:58:38

标签: python wxpython wxstyledtextctrl

我想在我的wxpython应用程序中添加多个光标功能。我使用StyledTextCtrl作为文字控件。我已经使它看起来像多个光标,请参见下图。但是,当我键入文本时,前两个光标消失了,文本被插入了最后一个光标位置。有人可以帮我吗?我已经附上了我的代码。

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

更新:

我要实现的是多光标编辑功能,许多流行的编辑器中都存在该功能。见下图:

enter image description here

0 个答案:

没有答案