我正在尝试实现下划线位于第 1 行和第 5 行的空间,以在行号和代码之间提供一点填充,因此它看起来不会那么混乱。
我将如何实现这一目标?
使用.SetMarginLeft(width)
. 例子:
import wx
import wx.stc as stc
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.text = stc.StyledTextCtrl(self.panel)
self.text.SetWindowStyle(self.text.GetWindowStyle() | wx.DOUBLE_BORDER)
self.text.StyleSetSpec(stc.STC_STYLE_DEFAULT, "size:15,face:Courier New")
self.text.SetWrapMode(stc.STC_WRAP_WORD)
self.text.SetMarginLeft(50)
self.menu = wx.Menu()
self.menu.Append(wx.ID_ABOUT, "&About", "Information about this program")
self.menu_bar = wx.MenuBar()
self.menu_bar.Append(self.menu, "&File")
self.SetMenuBar(self.menu_bar)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.text, 1, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizerAndFit(self.sizer)
self.text.Bind(stc.EVT_STC_CHANGE, self.OnChange)
self.Show()
self.text.SetText("Line!\n"*98)
def OnChange(self, e):
lines = self.text.GetLineCount()
width = self.text.TextWidth(stc.STC_STYLE_LINENUMBER, str(lines)+" ")
self.text.SetMarginWidth(0, width)
if __name__ == "__main__":
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()