如何在我的编辑器中增加边距?

时间:2018-10-28 22:43:21

标签: wxpython

我用行号制作了一个文本编辑器,它的工作原理很好,但是每当例如行数达到数千行时,它就不会适合页边空白。如何使页边距以与数字相同的长度开始,并在需要时增加?Example

编辑器:

import wx
import wx.stc as stc

class Window(wx.Frame):
    def __init__(self, parent, title):
        self.leftMarginWidth = 35
        wx.Frame.__init__(self, parent, title=title, size=(500, 500))
        self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
        self.clipboard = wx.Clipboard()
        self.Margins()
    def Margins(self):
        self.control.SetViewWhiteSpace(False)
        self.control.SetMargins(5, 0)
        self.control.SetMarginType(1, stc.STC_MARGIN_NUMBER)
        self.control.SetMarginWidth(1, self.leftMarginWidth)

def main():
    app = wx.App()
    frame = Window(None, "Text Editor")
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试对控件进行更改时设置页边距宽度,并绑定到EVT_STC_UPDATEUI

import wx
import wx.stc as stc

class Window(wx.Frame):
    def __init__(self, parent, title):
        self.leftMarginWidth = 16
        wx.Frame.__init__(self, parent, title=title, size=(500, 500))
        self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
        self.clipboard = wx.Clipboard()
        self.Margins()
        self.control.Bind(stc.EVT_STC_UPDATEUI, self.Scroll)

    def Margins(self):
        self.control.SetViewWhiteSpace(False)
        self.control.SetMargins(5, 0)
        self.control.SetMarginType(1, stc.STC_MARGIN_NUMBER)

    def Scroll(self,event):
        x = self.control.GetFirstVisibleLine()
        y = self.control.LinesOnScreen()
        x = x+y
        x = len(str(x)) #calculate the number of digits in the highest visible line number
        self.control.SetMarginWidth(1, x*16) #Reset the margin width

def main():
    app = wx.App()
    frame = Window(None, "Text Editor")
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()