i saw from this forum on how to make line number in python but where should i paste the code into???i pasted the code in my gui code and no numbers appear out??Can anyone help me???i am a beginner in this.
Thanks.

import wx,os,re

ID_OPEN=102
ID_SAVE=103
ID_SaveAS=104

class MainWindow(wx.Frame):
    
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent,wx.ID_ANY, title)

        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
        self.CreateStatusBar()

        filemenu= wx.Menu()
        filemenu.Append(ID_OPEN, "&Open", "Open a file to edit")
        filemenu.AppendSeparator()
        filemenu.Append(ID_SAVE, "&Save", "Saving the file")
        filemenu.Append(ID_SaveAS, "Save &As", "Saving file")
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File")
        self.SetMenuBar(menuBar)

        wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
        wx.EVT_MENU(self, ID_SAVE, self.OnSave)
        wx.EVT_MENU(self, ID_SaveAS, self.OnSaveAS)

        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        button = wx.Button(self, 1,"Parse HTML")
        self.sizer2.Add(button,1,wx.EXPAND)
        self.sizer=wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.control,1,wx.EXPAND)
        self.sizer.Add(self.sizer2,0,wx.EXPAND)

        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)

        self.Bind(wx.EVT_BUTTON, self.checker, button)

        self.Show(1)

    def checker(self,e):

        f = open(self.filename, 'r')
        information = self.control.GetValue()
        b = re.findall('<\/?[^>]*>',information)
        a = ['<bml>','</bml>','<head>','</head>']
        b = information.split('\n')
        count = 0
        count1 = 0
        for line in b:
            count = count + 1
            if '<head>' in line:
                count1 = count1 + 1
            if count1 > 1 and '<head>' in line:
                box = wx.MessageDialog(None, "HTML Script can only contain 1 '<head>' tag." + '\n' + "Repeated <head> tag found in line: " + str(count), "Title",wx.OK)
                box = box.ShowModal()
                

    def OnSaveAS(self,e):
        dlg = wx.FileDialog(self, "Choose a file", self.filename, "", "*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
        if dlg.ShowModal() == wx.ID_OK:
            grab = self.control.GetValue()
            f = open(self.filename, 'w')
            f.write(grab)
            f.close()
        dlg.DESTROY()


    def OnSave(self,e):
        grab = self.control.GetValue()
        f = open(self.filename, 'w')
        f.write(grab)
        f.close()
        box = wx.MessageDialog(None, "File Save Finish", "Title",wx.OK)
        box.ShowModal()
        box.Destroy
        
    def OnOpen(self, event):
        dlg = wx.FileDialog(self, "Open file...",style=wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetPath()
            f = open(self.filename, 'r')
            self.control.SetValue(f.read())
            self.SetTitle('Editing.....' + self.filename)
        line_number = len( self.control.GetRange( 0, self.control.GetInsertionPoint() ).split("\n") )
        dlg.Destroy()



app = wx.PySimpleApp()
frame = MainWindow(None, "Sample Editor")
frame.Show()
app.MainLoop()

Recommended Answers

All 9 Replies

If I understand you find out the insertion points line_numberin line 87, but never use it for anything:

line_number = len( self.control.GetRange( 0, self.control.GetInsertionPoint() ).split("\n") )
print line_number

I added print and it printed 1 to console.

i want it to display out inside of the wx.textctrl. Any idea how to do it?

That is the line where the cursor is, normally it is displayed at the bottom of the window info area (current row and column, MS Word shows Ri 1 Sar 1 in Finnish, maybe Row 1 Col 1 in English, for example)

oh,what i wanted is those normal number line that appear at the side of like a programming language compiler????Example like this current forum code tag frame indicates the number line out???isit possible for me to do that in wxpython and show it in the wx.Textctrl box?

You can replace line 84 with code to put linenumber and lines one by one to text. Those numbers will be editable though so probably you should disallow user to move cursor there and you should also remove them before saving.

How am i suppose to do what you have say?can you give me some hint or which part of wx.python can i look for?

I know next to nothing about wxPython, but I googled little and found one post from famous DaniWeb long time ago:
http://www.daniweb.com/forums/post281331.html#post281331
and played with it, generating line numbers at beginning of line and using tab before the real line.

The problem of this kind of numbering is that they must be regenerated if lines are copy pasted tms. and they would be every time removed when copy region yms.

Better would be to find something like we found in earlier thread for tkinter, which creates another widget beside textctrl and syncronizes scrolling with it:
http://www.daniweb.com/forums/thread291657.html

I did not know how to remove the second widget from the window even I did not use it.

import wx
class DisFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.SetSize(wx.Size(600,400))
        self.SetTitle('Temperamental TextCtrl Tabs')

        # make a panel with a button / textctrl / listbox
        self.panel = wx.Panel(self)

        self.panel.SetFont(wx.Font(12, wx.NORMAL, wx.NORMAL, wx.NORMAL))

        bs = wx.BoxSizer(wx.VERTICAL)

        self.button = wx.Button(self.panel, -1,
                                'Set Tabs and then Add Text')
        self.button.Bind(wx.EVT_BUTTON, self.ButtonClicked)
        bs.Add(self.button,0,wx.TOP|wx.LEFT|wx.RIGHT|wx.EXPAND,30)

        bs.AddSpacer(wx.Size(10,10))

        self.editor = wx.TextCtrl(self.panel,
                                  style=wx.TE_MULTILINE|wx.TE_PROCESS_TAB|wx.TE_RICH)
        
        bs.Add(self.editor,1,wx.EXPAND|wx.LEFT|wx.RIGHT,30)

        bs.AddSpacer(wx.Size(10,30))

        self.lb = wx.ListBox(self.panel, wx.NewId(), style=wx.LB_SINGLE)
        bs.Add(self.lb,2,wx.EXPAND|wx.BOTTOM|wx.LEFT|wx.RIGHT,30)

        self.panel.SetSizer(bs)

    def ButtonClicked(self, event):

        # change the default style of the textctrl,
        # set a few tab positions and then add
        # some text

        dastyle = wx.TextAttr()
        dastyle.SetTabs([200, 390, 800])
        self.editor.SetDefaultStyle(dastyle)

        # add text to the textctrl
        self.editor.Clear()
        self.editor.AppendText('\ttab1\ttab2\ttab3\n')

        x = 1
        for i in range(1,100):
            self.editor.AppendText(str(i)+'\t'+ ## line number adding and tab
                               5*('line ' + str(i)+'\t')+'\n')

        event.Skip()

if __name__ == '__main__':
    app = wx.App()
    daframe = DisFrame(None)
    daframe.Show()
    app.MainLoop()

Further Googling brought me this:

I also looked at wx.StyledTextCtrl which handles displaying line
numbers but doesn't seem to handle hyperlinks.

http://osdir.com/ml/wxpython-users/2009-10/msg00480.html

So looks like you should be able to show line number with wx.StyledTextCtrl
http://www.nullege.com/codes/search/StyledTextCtrl_2.PythonSTC/all
This link is showing in web quite a nice way line numbers while showing the demo code source for StyledTextCtrl.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.